3 3 |
Why is recursion dangerous? Let's compute fibonacci numbers recursively.
|
0 0 |
1 1 |
55 55 |
'n needs to be non-negative' 'n needs to be non-negative' |
'n needs to be an integer' 'n needs to be an integer' |
832040 1.834607 832040 1.834607 |
|
55 0.000287999999999 55 0.000287999999999 |
6765 0.021829 6765 0.021829 |
832040 1.81965 832040 1.81965 |
Traceback (click to the left of this block for traceback) ... __SAGE__ ^CTraceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_19.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dD1jcHV0aW1lKCkKcHJpbnQobXlfZmliX3IoNDApKQpwcmludChjcHV0aW1lKCktdCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpzAMndY/___code___.py", line 4, in <module> print(my_fib_r(_sage_const_40 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 12, in my_fib_r return(my_fib_r(n-_sage_const_1 )+my_fib_r(n-_sage_const_2 )) File "/tmp/tmpvYMn2b/___code___.py", line 3, in my_fib_r def my_fib_r(n): # recursive computation of fib(n) File "sage/ext/interrupt/interrupt.pyx", line 203, in sage.ext.interrupt.interrupt.sage_python_check_interrupt (/usr/local/sage-6.10/src/build/cythonized/sage/ext/interrupt/interrupt.c:1891) File "sage/ext/interrupt/interrupt.pyx", line 88, in sage.ext.interrupt.interrupt.sig_raise_exception (/usr/local/sage-6.10/src/build/cythonized/sage/ext/interrupt/interrupt.c:925) KeyboardInterrupt __SAGE__ |
|
55 0.000261999999992 177 55 0.000261999999992 177 |
6765 0.026749 21891 6765 0.026749 21891 |
832040 2.227804 2692537 832040 2.227804 2692537 |
9227465 22.169936 29860703 9227465 22.169936 29860703 |
102334155 102334155 |
$F_n$ is the closest integer to $\frac{1}{\sqrt{5}} \left( \frac{1+\sqrt{5}}{2} \right)^n$.
0.447213595499958 0.723606797749979 1.17082039324994 1.89442719099992 3.06524758424985 4.95967477524977 8.02492235949962 12.9845971347494 21.0095194942490 33.9941166289984 0.447213595499958 0.723606797749979 1.17082039324994 1.89442719099992 3.06524758424985 4.95967477524977 8.02492235949962 12.9845971347494 21.0095194942490 33.9941166289984 |
How big is $\frac{log(\phi)}{log(10)}$?
0.208987640249979 0.208987640249979 |
208987.290764977 0.009503 208987.290764977 0.009503 |
2.08987605301478e6 0.263528 2.08987605301478e6 0.263528 |
2.08987636755129e7 4.428707 2.08987636755129e7 4.428707 |
2.08987639900494e8 45.316107 2.08987639900494e8 45.316107 |
12094869620734133568 73554788881298750625 85649658502032884193 59204447383331634818 44854105885364519011 4058553268696153829 48912659154060672840 52971212422756826669 1883871576817499509 54855083999574326178 56738955576391825687 11594039575966151865 68332995152357977552 79927034728324129417 48260029880682106969 28187064609006236386 76447094489688343355 4634159098694579741 81081253588382923096 85715412687077502837 12094869620734133568 73554788881298750625 85649658502032884193 59204447383331634818 44854105885364519011 4058553268696153829 48912659154060672840 52971212422756826669 1883871576817499509 54855083999574326178 56738955576391825687 11594039575966151865 68332995152357977552 79927034728324129417 48260029880682106969 28187064609006236386 76447094489688343355 4634159098694579741 81081253588382923096 85715412687077502837 |
[1, 1, 4, 4, 9, 9, 2, 1, 6, 2, 2, 0, 7, 9, 7, 2, 1, 1, 5, 4] [1, 8, 5, 2, 6, 3, 6, 2, 3, 5, 3, 0, 4, 7, 3, 1, 7, 3, 1, 0] [2, 9, 9, 7, 6, 2, 8, 3, 9, 7, 5, 1, 2, 7, 0, 3, 8, 4, 6, 4] [4, 8, 5, 0, 2, 6, 4, 6, 3, 2, 8, 1, 7, 4, 3, 5, 5, 7, 7, 5] [7, 8, 4, 7, 8, 9, 3, 0, 3, 0, 3, 3, 0, 1, 3, 9, 4, 2, 3, 9] [1, 2, 6, 9, 8, 1, 5, 7, 6, 6, 3, 1, 4, 7, 5, 7, 5, 0, 0, 1] [2, 0, 5, 4, 6, 0, 5, 0, 6, 9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117322375295462212094869620734133568 11449921622079721154497199899966677907638995896622219388843656194870204440338362332943924986323117322375295462212094869620734133568 |
Computing $F_n$ via $A^n$ where
\[ A= \begin{pmatrix} 1 & 1 \\ 1 & 0\end{pmatrix} \]
[89 55] [55 34] [89 55] [55 34] |
This does indeed have entries $F_{11}, F_{10}, F_{10}, F_{9}$.
[1454489111232772683678306641953 898923707008479989274290850145] [ 898923707008479989274290850145 555565404224292694404015791808] [1454489111232772683678306641953 898923707008479989274290850145] [ 898923707008479989274290850145 555565404224292694404015791808] |
What if we only want to keep track of the final 20 digits?
Use sage's built in modular arithmetic.
1 <type 'sage.rings.finite_rings.integer_mod.IntegerMod_gmp'> 1 <type 'sage.rings.finite_rings.integer_mod.IntegerMod_gmp'> |
<type 'sage.rings.integer.Integer'> <type 'sage.rings.integer.Integer'> |
True True |
[32772683678306641953 8479989274290850145] [ 8479989274290850145 24292694404015791808] [32772683678306641953 8479989274290850145] [ 8479989274290850145 24292694404015791808] |
|