Tried to solve 55 till 57.
This commit is contained in:
@@ -34,6 +34,87 @@
|
||||
"In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_digit_count(n):\n",
|
||||
" c = 1\n",
|
||||
" while n:\n",
|
||||
" n += 1\n",
|
||||
" n //= 10\n",
|
||||
" return d"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def gcd(a, b):\n",
|
||||
" if b > a:\n",
|
||||
" a, b = b, a\n",
|
||||
" while a % b != 0:\n",
|
||||
" a, b = b, a % b\n",
|
||||
" return b\n",
|
||||
" \n",
|
||||
"assert(gcd(100, 35) == 5)\n",
|
||||
"\n",
|
||||
"def add_fractions(n1, d1, n2, d2):\n",
|
||||
" d = d1 * d2\n",
|
||||
" n1 = n1 * (d // d1)\n",
|
||||
" n2 = n2 * (d // d2)\n",
|
||||
" n = n1 + n2\n",
|
||||
" p = gcd(n, d)\n",
|
||||
" return (n // p, d // p)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 54,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def next_expension(n, d):\n",
|
||||
" n, d = add_fractions(1, 1, n, d)\n",
|
||||
" return add_fractions(1, 1, d, n)\n",
|
||||
"\n",
|
||||
"c = 0\n",
|
||||
"\n",
|
||||
"n, d = (3, 2)\n",
|
||||
"for i in range(1000):\n",
|
||||
" if get_digit_count(n)> get_digit_count(d):\n",
|
||||
" c += 1\n",
|
||||
" n, d = next_expension(n, d)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 55,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"153\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"s = c\n",
|
||||
"print(s)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
|
||||
Reference in New Issue
Block a user