Added 24 and 25 to ipython.
This commit is contained in:
parent
b7e7314e99
commit
ee6e6e6445
@ -645,6 +645,7 @@ If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and e
|
|||||||
<div class="inner_cell">
|
<div class="inner_cell">
|
||||||
<div class="text_cell_render border-box-sizing rendered_html">
|
<div class="text_cell_render border-box-sizing rendered_html">
|
||||||
<p>This is actually really embarrassing. Especially I was aware of the sqrt trick all the way but did not manage to put it into use properly. Another example where greate engineer will leave you far behind compared to great thinking.</p>
|
<p>This is actually really embarrassing. Especially I was aware of the sqrt trick all the way but did not manage to put it into use properly. Another example where greate engineer will leave you far behind compared to great thinking.</p>
|
||||||
|
<p><strong>Supplement:</strong> Also I have discovered that my sum of proper divisors function has a bug when n is a square, because in that case <em>i == (n // 1)</em> which adds this value two times. So there is no amicable number under 10000 which is also a square because otherwise we would have failed.</p>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -390,7 +390,9 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"This is actually really embarrassing. Especially I was aware of the sqrt trick all the way but did not manage to put it into use properly. Another example where greate engineer will leave you far behind compared to great thinking."
|
"This is actually really embarrassing. Especially I was aware of the sqrt trick all the way but did not manage to put it into use properly. Another example where greate engineer will leave you far behind compared to great thinking.\n",
|
||||||
|
"\n",
|
||||||
|
"**Supplement:** Also I have discovered that my sum of proper divisors function has a bug when n is a square, because in that case _i == (n // 1)_ which adds this value two times. So there is no amicable number under 10000 which is also a square because otherwise we would have failed."
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -188,21 +188,124 @@ div#notebook {
|
|||||||
<p>As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.</p>
|
<p>As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.</p>
|
||||||
<p>Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.</p>
|
<p>Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.</p>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing text_cell rendered">
|
||||||
|
<div class="prompt input_prompt">
|
||||||
|
</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="text_cell_render border-box-sizing rendered_html">
|
||||||
|
<p>We reuse the sum of proper divisors function and use it to tell whether a number is abundant or not.</p>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="cell border-box-sizing code_cell rendered">
|
<div class="cell border-box-sizing code_cell rendered">
|
||||||
<div class="input">
|
<div class="input">
|
||||||
<div class="prompt input_prompt">In [ ]:</div>
|
<div class="prompt input_prompt">In [20]:</div>
|
||||||
<div class="inner_cell">
|
<div class="inner_cell">
|
||||||
<div class="input_area">
|
<div class="input_area">
|
||||||
<div class=" highlight hl-ipython3"><pre><span></span>
|
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">sum_of_proper_divisors</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||||
|
<span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">sqrt</span>
|
||||||
|
<span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
|
||||||
|
<span class="k">return</span> <span class="mi">0</span>
|
||||||
|
<span class="n">s</span> <span class="o">=</span> <span class="mi">1</span>
|
||||||
|
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
|
||||||
|
<span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
|
||||||
|
<span class="n">s</span> <span class="o">+=</span> <span class="n">i</span>
|
||||||
|
<span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="n">n</span> <span class="o">//</span> <span class="n">i</span><span class="p">)</span>
|
||||||
|
<span class="k">if</span> <span class="n">x</span> <span class="o">!=</span> <span class="n">i</span><span class="p">:</span>
|
||||||
|
<span class="n">s</span> <span class="o">+=</span> <span class="n">x</span>
|
||||||
|
<span class="k">return</span> <span class="n">s</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">is_abundant</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||||
|
<span class="k">return</span> <span class="n">sum_of_proper_divisors</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">></span> <span class="n">n</span>
|
||||||
|
|
||||||
|
<span class="k">assert</span><span class="p">(</span><span class="n">is_abundant</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="o">==</span> <span class="kc">False</span><span class="p">)</span>
|
||||||
|
<span class="k">assert</span><span class="p">(</span><span class="n">is_abundant</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span> <span class="o">==</span> <span class="kc">True</span><span class="p">)</span>
|
||||||
|
|
||||||
|
<span class="n">abundant_numbers_smaller_30000</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">30001</span><span class="p">)</span> <span class="k">if</span> <span class="n">is_abundant</span><span class="p">(</span><span class="n">n</span><span class="p">)]</span>
|
||||||
|
<span class="n">abundant_numbers_smaller_30000_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">abundant_numbers_smaller_30000</span><span class="p">)</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing text_cell rendered">
|
||||||
|
<div class="prompt input_prompt">
|
||||||
|
</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="text_cell_render border-box-sizing rendered_html">
|
||||||
|
<p>Now that we have all abundant numbers in sorted order we can write a function which tests whether a number can be written as a sum of two abundant numbers.</p>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing code_cell rendered">
|
||||||
|
<div class="input">
|
||||||
|
<div class="prompt input_prompt">In [21]:</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="input_area">
|
||||||
|
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">is_sum_of_two_abundants</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||||
|
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">abundant_numbers_smaller_30000</span><span class="p">:</span>
|
||||||
|
<span class="k">if</span> <span class="n">a</span> <span class="o">></span> <span class="p">(</span><span class="n">n</span> <span class="o">//</span> <span class="mi">2</span><span class="p">):</span>
|
||||||
|
<span class="k">return</span> <span class="kc">False</span>
|
||||||
|
<span class="n">r</span> <span class="o">=</span> <span class="n">n</span> <span class="o">-</span> <span class="n">a</span>
|
||||||
|
<span class="k">if</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">abundant_numbers_smaller_30000_set</span><span class="p">:</span>
|
||||||
|
<span class="k">return</span> <span class="kc">True</span>
|
||||||
|
<span class="k">return</span> <span class="kc">False</span>
|
||||||
|
|
||||||
|
<span class="k">assert</span><span class="p">(</span><span class="n">is_sum_of_two_abundants</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span> <span class="o">==</span> <span class="kc">True</span><span class="p">)</span>
|
||||||
|
<span class="k">assert</span><span class="p">(</span><span class="n">is_sum_of_two_abundants</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span> <span class="o">==</span> <span class="kc">False</span><span class="p">)</span>
|
||||||
|
<span class="k">assert</span><span class="p">(</span><span class="n">is_sum_of_two_abundants</span><span class="p">(</span><span class="mi">28123</span><span class="p">)</span> <span class="o">==</span> <span class="kc">True</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing text_cell rendered">
|
||||||
|
<div class="prompt input_prompt">
|
||||||
|
</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="text_cell_render border-box-sizing rendered_html">
|
||||||
|
<p>Now we try to brute force.</p>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing code_cell rendered">
|
||||||
|
<div class="input">
|
||||||
|
<div class="prompt input_prompt">In [34]:</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="input_area">
|
||||||
|
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">s</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">30000</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">is_sum_of_two_abundants</span><span class="p">(</span><span class="n">i</span><span class="p">)])</span>
|
||||||
|
<span class="nb">print</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
|
||||||
|
<span class="k">assert</span><span class="p">(</span><span class="n">s</span> <span class="o">==</span> <span class="mi">4179871</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="output_wrapper">
|
||||||
|
<div class="output">
|
||||||
|
|
||||||
|
|
||||||
|
<div class="output_area"><div class="prompt"></div>
|
||||||
|
<div class="output_subarea output_stream output_stdout output_text">
|
||||||
|
<pre>4179871
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -15,14 +15,100 @@
|
|||||||
"Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers."
|
"Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"We reuse the sum of proper divisors function and use it to tell whether a number is abundant or not."
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 20,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": [
|
||||||
|
"def sum_of_proper_divisors(n):\n",
|
||||||
|
" from math import sqrt\n",
|
||||||
|
" if n == 1:\n",
|
||||||
|
" return 0\n",
|
||||||
|
" s = 1\n",
|
||||||
|
" for i in range(2, int(sqrt(n)) + 1):\n",
|
||||||
|
" if n % i == 0:\n",
|
||||||
|
" s += i\n",
|
||||||
|
" x = (n // i)\n",
|
||||||
|
" if x != i:\n",
|
||||||
|
" s += x\n",
|
||||||
|
" return s\n",
|
||||||
|
"\n",
|
||||||
|
"def is_abundant(n):\n",
|
||||||
|
" return sum_of_proper_divisors(n) > n\n",
|
||||||
|
"\n",
|
||||||
|
"assert(is_abundant(7) == False)\n",
|
||||||
|
"assert(is_abundant(12) == True)\n",
|
||||||
|
"\n",
|
||||||
|
"abundant_numbers_smaller_30000 = [n for n in range(1, 30001) if is_abundant(n)]\n",
|
||||||
|
"abundant_numbers_smaller_30000_set = set(abundant_numbers_smaller_30000)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now that we have all abundant numbers in sorted order we can write a function which tests whether a number can be written as a sum of two abundant numbers."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 21,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def is_sum_of_two_abundants(n):\n",
|
||||||
|
" for a in abundant_numbers_smaller_30000:\n",
|
||||||
|
" if a > (n // 2):\n",
|
||||||
|
" return False\n",
|
||||||
|
" r = n - a\n",
|
||||||
|
" if r in abundant_numbers_smaller_30000_set:\n",
|
||||||
|
" return True\n",
|
||||||
|
" return False\n",
|
||||||
|
"\n",
|
||||||
|
"assert(is_sum_of_two_abundants(24) == True)\n",
|
||||||
|
"assert(is_sum_of_two_abundants(23) == False)\n",
|
||||||
|
"assert(is_sum_of_two_abundants(28123) == True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now we try to brute force."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 34,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"4179871\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"s = sum([i for i in range(1, 30000) if not is_sum_of_two_abundants(i)])\n",
|
||||||
|
"print(s)\n",
|
||||||
|
"assert(s == 4179871)"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@ -46,7 +132,8 @@
|
|||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
"perfect number",
|
"perfect number",
|
||||||
"abundant"
|
"abundant",
|
||||||
|
"factors"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
277
ipython/EulerProblem024.html
Normal file
277
ipython/EulerProblem024.html
Normal file
File diff suppressed because one or more lines are too long
109
ipython/EulerProblem024.ipynb
Normal file
109
ipython/EulerProblem024.ipynb
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Euler Problem 24 \n",
|
||||||
|
"\n",
|
||||||
|
"A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:\n",
|
||||||
|
"\n",
|
||||||
|
"012 021 102 120 201 210\n",
|
||||||
|
"\n",
|
||||||
|
"What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"I tried to solve this by thinking and failed to be hones. So we implement a generator and get the millionth element."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from collections import deque\n",
|
||||||
|
"\n",
|
||||||
|
"def permutation_generator(xs):\n",
|
||||||
|
" if not xs:\n",
|
||||||
|
" yield deque()\n",
|
||||||
|
" r = []\n",
|
||||||
|
" for i in range(len(xs)):\n",
|
||||||
|
" x = xs[i]\n",
|
||||||
|
" ys = permutation_generator(xs[:i] + xs[i+1:])\n",
|
||||||
|
" for y in ys:\n",
|
||||||
|
" y.appendleft(x)\n",
|
||||||
|
" yield y\n",
|
||||||
|
" raise StopIteration\n",
|
||||||
|
"\n",
|
||||||
|
"assert(list(map(lambda s: \"\".join(s), permutation_generator(\"012\"))) == ['012', '021', '102', '120', '201', '210'])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"All right, now we reuse the function to get the nth element from problem 7 and give ourselves a solution."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 26,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"2783915460\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def get_nth(generator, n):\n",
|
||||||
|
" import itertools\n",
|
||||||
|
" return next(itertools.islice(generator, n - 1, n))\n",
|
||||||
|
"\n",
|
||||||
|
"ps = permutation_generator(\"0123456789\")\n",
|
||||||
|
"s = int(\"\".join(get_nth(ps, 1000000)))\n",
|
||||||
|
"assert(s == 2783915460)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"completion_date": "Thu, 5 Nov 2015, 16:04",
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.5.4"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"permutation"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
262
ipython/EulerProblem025.html
Normal file
262
ipython/EulerProblem025.html
Normal file
File diff suppressed because one or more lines are too long
100
ipython/EulerProblem025.ipynb
Normal file
100
ipython/EulerProblem025.ipynb
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Euler Problem 25\n",
|
||||||
|
"\n",
|
||||||
|
"The Fibonacci sequence is defined by the recurrence relation:\n",
|
||||||
|
"\n",
|
||||||
|
"$F_n = F_{n−1} + F_{n−2}$, where $F_1 = 1$ and $F_2 = 1$.\n",
|
||||||
|
"Hence the first 12 terms will be:\n",
|
||||||
|
"\n",
|
||||||
|
"~~~\n",
|
||||||
|
"F1 = 1\n",
|
||||||
|
"F2 = 1\n",
|
||||||
|
"F3 = 2\n",
|
||||||
|
"F4 = 3\n",
|
||||||
|
"F5 = 5\n",
|
||||||
|
"F6 = 8\n",
|
||||||
|
"F7 = 13\n",
|
||||||
|
"F8 = 21\n",
|
||||||
|
"F9 = 34\n",
|
||||||
|
"F10 = 55\n",
|
||||||
|
"F11 = 89\n",
|
||||||
|
"F12 = 144\n",
|
||||||
|
"~~~\n",
|
||||||
|
"\n",
|
||||||
|
"The 12th term, $F_{12}$, is the first term to contain three digits.\n",
|
||||||
|
"\n",
|
||||||
|
"What is the index of the first term in the Fibonacci sequence to contain 1000 digits?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Reuse fibonacci generator and then straight forward."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"4782\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def fibonacci_generator():\n",
|
||||||
|
" a = 0\n",
|
||||||
|
" b = 1\n",
|
||||||
|
" yield 1\n",
|
||||||
|
" while True:\n",
|
||||||
|
" yield a + b\n",
|
||||||
|
" a, b = b, (a + b)\n",
|
||||||
|
" \n",
|
||||||
|
"for i, f in enumerate(fibonacci_generator()):\n",
|
||||||
|
" if len(str(f)) >= 1000:\n",
|
||||||
|
" break\n",
|
||||||
|
"\n",
|
||||||
|
"s = i + 1\n",
|
||||||
|
"assert(s == 4782)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"completion_date": "Fri, 5 Sep 2014, 14:57",
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.5.4"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"fibonacci"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user