def test_evalf_integer_parts(): a = floor(log(8)/log(2) - exp(-1000), evaluate=False) b = floor(log(8)/log(2), evaluate=False) assert a.evalf() == 3 assert b.evalf() == 3 # equals, as a fallback, can still fail but it might succeed as here assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10 assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \ long(11188719610782480504630258070757734324011354208865721592720336800) assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \ long(11188719610782480504630258070757734324011354208865721592720336801) assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(999) assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(1000) assert ceiling(x).evalf(subs={x: 3}) == 3 assert ceiling(x).evalf(subs={x: 3*I}) == 3*I assert ceiling(x).evalf(subs={x: 2 + 3*I}) == 2 + 3*I assert ceiling(x).evalf(subs={x: 3.}) == 3 assert ceiling(x).evalf(subs={x: 3.*I}) == 3*I assert ceiling(x).evalf(subs={x: 2. + 3*I}) == 2 + 3*I assert float((floor(1.5, evaluate=False)+1/9).evalf()) == 1 + 1/9 assert float((floor(0.5, evaluate=False)+20).evalf()) == 20
def test_evalf_integer_parts(): a = floor(log(8) / log(2) - exp(-1000), evaluate=False) b = floor(log(8) / log(2), evaluate=False) assert a.evalf() == 3 assert b.evalf() == 3 # equals, as a fallback, can still fail but it might succeed as here assert ceiling(10 * (sin(1)**2 + cos(1)**2)) == 10 assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \ long(11188719610782480504630258070757734324011354208865721592720336800) assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \ long(11188719610782480504630258070757734324011354208865721592720336801) assert int( floor((GoldenRatio**999 / sqrt(5) + Rational(1, 2))).evalf(1000)) == fibonacci(999) assert int( floor((GoldenRatio**1000 / sqrt(5) + Rational(1, 2))).evalf(1000)) == fibonacci(1000) assert ceiling(x).evalf(subs={x: 3}) == 3 assert ceiling(x).evalf(subs={x: 3 * I}) == 3 * I assert ceiling(x).evalf(subs={x: 2 + 3 * I}) == 2 + 3 * I assert ceiling(x).evalf(subs={x: 3.}) == 3 assert ceiling(x).evalf(subs={x: 3. * I}) == 3 * I assert ceiling(x).evalf(subs={x: 2. + 3 * I}) == 2 + 3 * I assert float((floor(1.5, evaluate=False) + 1 / 9).evalf()) == 1 + 1 / 9 assert float((floor(0.5, evaluate=False) + 20).evalf()) == 20
def _compute_rho(self, k, num_iterations): # The l variable counts backwards whereas k counts forwards. l = num_iterations - (k-1) # Compute rho using Fibonacci numbers result = 1 - sy.fibonacci(l+1) / sy.fibonacci(l+2) return float(result)
def test_linrec(): assert linrec(coeffs=[1, 1], init=[1, 1], n=20) == 10946 assert linrec(coeffs=[1, 2, 3, 4, 5], init=[1, 1, 0, 2], n=10) == 1040 assert linrec(coeffs=[0, 0, 11, 13], init=[23, 27], n=25) == 59628567384 assert linrec(coeffs=[0, 0, 1, 1, 2], init=[1, 5, 3], n=15) == 165 assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=70) == \ 56889923441670659718376223533331214868804815612050381493741233489928913241 assert linrec(coeffs=[0]*55 + [1, 1, 2, 3], init=[0]*50 + [1, 2, 3], n=4000) == \ 702633573874937994980598979769135096432444135301118916539 assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**4) assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**5) assert all( linrec(coeffs=[1, 1], init=[0, 1], n=n) == fibonacci(n) for n in range(95, 115)) assert all( linrec(coeffs=[1, 1], init=[1, 1], n=n) == fibonacci(n + 1) for n in range(595, 615)) a = [S(1) / 2, S(3) / 4, S(5) / 6, 7, S(8) / 9, S(3) / 5] b = [1, 2, 8, S(5) / 7, S(3) / 7, S(2) / 9, 6] x, y, z = symbols('x y z') assert linrec(coeffs=a[:5], init=b[:4], n=80) == \ Rational(1726244235456268979436592226626304376013002142588105090705187189, 1960143456748895967474334873705475211264) assert linrec(coeffs=a[:4], init=b[:4], n=50) == \ Rational(368949940033050147080268092104304441, 504857282956046106624) assert linrec(coeffs=a[3:], init=b[:3], n=35) == \ Rational(97409272177295731943657945116791049305244422833125109, 814315512679031689453125) assert linrec(coeffs=[0]*60 + [S(2)/3, S(4)/5], init=b, n=3000) == \ 26777668739896791448594650497024/S(48084516708184142230517578125) raises(TypeError, lambda: linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4, 5], n=1)) raises(TypeError, lambda: linrec(coeffs=a[:4], init=b[:5], n=10000)) raises(ValueError, lambda: linrec(coeffs=a[:4], init=b[:4], n=-10000)) raises(TypeError, lambda: linrec(x, b, n=10000)) raises(TypeError, lambda: linrec(a, y, n=10000)) assert linrec(coeffs=[x, y, z], init=[1, 1, 1], n=4) == \ x**2 + x*y + x*z + y + z assert linrec(coeffs=[1, 2, 1], init=[x, y, z], n=20) == \ 269542*x + 664575*y + 578949*z assert linrec(coeffs=[0, 3, 1, 2], init=[x, y], n=30) == \ 58516436*x + 56372788*y assert linrec(coeffs=[0]*50 + [1, 2, 3], init=[x, y, z], n=1000) == \ 11477135884896*x + 25999077948732*y + 41975630244216*z assert linrec(coeffs=[], init=[1, 1], n=20) == 0
def test_linrec(): assert linrec(coeffs=[1, 1], init=[1, 1], n=20) == 10946 assert linrec(coeffs=[1, 2, 3, 4, 5], init=[1, 1, 0, 2], n=10) == 1040 assert linrec(coeffs=[0, 0, 11, 13], init=[23, 27], n=25) == 59628567384 assert linrec(coeffs=[0, 0, 1, 1, 2], init=[1, 5, 3], n=15) == 165 assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=70) == \ 56889923441670659718376223533331214868804815612050381493741233489928913241 assert linrec(coeffs=[0]*55 + [1, 1, 2, 3], init=[0]*50 + [1, 2, 3], n=4000) == \ 702633573874937994980598979769135096432444135301118916539 assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**4) assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**5) assert all(linrec(coeffs=[1, 1], init=[0, 1], n=n) == fibonacci(n) for n in range(95, 115)) assert all(linrec(coeffs=[1, 1], init=[1, 1], n=n) == fibonacci(n + 1) for n in range(595, 615)) a = [S(1)/2, S(3)/4, S(5)/6, 7, S(8)/9, S(3)/5] b = [1, 2, 8, S(5)/7, S(3)/7, S(2)/9, 6] x, y, z = symbols('x y z') assert linrec(coeffs=a[:5], init=b[:4], n=80) == \ Rational(1726244235456268979436592226626304376013002142588105090705187189, 1960143456748895967474334873705475211264) assert linrec(coeffs=a[:4], init=b[:4], n=50) == \ Rational(368949940033050147080268092104304441, 504857282956046106624) assert linrec(coeffs=a[3:], init=b[:3], n=35) == \ Rational(97409272177295731943657945116791049305244422833125109, 814315512679031689453125) assert linrec(coeffs=[0]*60 + [S(2)/3, S(4)/5], init=b, n=3000) == \ 26777668739896791448594650497024/S(48084516708184142230517578125) raises(TypeError, lambda: linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4, 5], n=1)) raises(TypeError, lambda: linrec(coeffs=a[:4], init=b[:5], n=10000)) raises(ValueError, lambda: linrec(coeffs=a[:4], init=b[:4], n=-10000)) raises(TypeError, lambda: linrec(x, b, n=10000)) raises(TypeError, lambda: linrec(a, y, n=10000)) assert linrec(coeffs=[x, y, z], init=[1, 1, 1], n=4) == \ x**2 + x*y + x*z + y + z assert linrec(coeffs=[1, 2, 1], init=[x, y, z], n=20) == \ 269542*x + 664575*y + 578949*z assert linrec(coeffs=[0, 3, 1, 2], init=[x, y], n=30) == \ 58516436*x + 56372788*y assert linrec(coeffs=[0]*50 + [1, 2, 3], init=[x, y, z], n=1000) == \ 11477135884896*x + 25999077948732*y + 41975630244216*z
def test_evalf_integer_parts(): a = floor(log(8)/log(2) - exp(-1000), evaluate=False) b = floor(log(8)/log(2), evaluate=False) raises(PrecisionExhausted, "a.evalf()") assert a.evalf(chop=True) == 3 assert a.evalf(maxprec=500) == 2 raises(PrecisionExhausted, "b.evalf()") raises(PrecisionExhausted, "b.evalf(maxprec=500)") assert b.evalf(chop=True) == 3 assert int(floor(factorial(50)/E,evaluate=False).evalf()) == \ 11188719610782480504630258070757734324011354208865721592720336800L assert int(ceiling(factorial(50)/E,evaluate=False).evalf()) == \ 11188719610782480504630258070757734324011354208865721592720336801L assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1,2))).evalf(1000)) == fibonacci(999) assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1,2))).evalf(1000)) == fibonacci(1000)
def test_evalf_integer_parts(): a = floor(log(8)/log(2) - exp(-1000), evaluate=False) b = floor(log(8)/log(2), evaluate=False) raises(PrecisionExhausted, "a.evalf()") assert a.evalf(chop=True) == 3 assert a.evalf(maxn=500) == 2 raises(PrecisionExhausted, "b.evalf()") raises(PrecisionExhausted, "b.evalf(maxn=500)") assert b.evalf(chop=True) == 3 assert int(floor(factorial(50)/E,evaluate=False).evalf()) == \ 11188719610782480504630258070757734324011354208865721592720336800L assert int(ceiling(factorial(50)/E,evaluate=False).evalf()) == \ 11188719610782480504630258070757734324011354208865721592720336801L assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1,2))).evalf(1000)) == fibonacci(999) assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1,2))).evalf(1000)) == fibonacci(1000)
def test_find_simple_recurrence_vector(): assert find_simple_recurrence_vector([fibonacci(k) for k in range(12)]) == [ 1, -1, -1, ]
def test_evalf_integer_parts(): a = floor(log(8)/log(2) - exp(-1000), evaluate=False) b = floor(log(8)/log(2), evaluate=False) raises(PrecisionExhausted, "a.evalf()") assert a.evalf(chop=True) == 3 assert a.evalf(maxn=500) == 2 assert b.evalf() == 3 # equals, as a fallback, can still fail but it might succeed as here assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10 assert int(floor(factorial(50)/E,evaluate=False).evalf()) == \ 11188719610782480504630258070757734324011354208865721592720336800L assert int(ceiling(factorial(50)/E,evaluate=False).evalf()) == \ 11188719610782480504630258070757734324011354208865721592720336801L assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1,2))).evalf(1000)) == fibonacci(999) assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1,2))).evalf(1000)) == fibonacci(1000)
def test_tenth_fibonacci_tuple(): """Ensure that the request for the tenth Fibonacci number returns the correct tuple of numbers.""" number = 10 result = fibonacci.fibonacci_tuple(number) assert len(result) == number assert result == (1, 1, 2, 3, 5, 8, 13, 21, 34, 55) assert sympy.fibonacci(10) == result[-1]
def test_second_fibonacci_tuple(): """Ensure that the request for the second Fibonacci number returns same number twice in a tuple.""" number = 2 result = fibonacci.fibonacci_tuple(number) assert len(result) == number assert result == (1, 1) assert sympy.fibonacci(2) == result[-1]
def test_first_fibonacci_singleton_tuple(): """Ensure that the request for first Fibonacci number returns same number in a tuple.""" number = 1 result = fibonacci.fibonacci_tuple(number) assert len(result) == number assert result == (1, ) assert sympy.fibonacci(1) == result[-1]
def test_evalf_integer_parts(): a = floor(log(8)/log(2) - exp(-1000), evaluate=False) b = floor(log(8)/log(2), evaluate=False) raises(PrecisionExhausted, lambda: a.evalf()) assert a.evalf(chop=True) == 3 assert a.evalf(maxn=500) == 2 assert b.evalf() == 3 # equals, as a fallback, can still fail but it might succeed as here assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10 assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \ long(11188719610782480504630258070757734324011354208865721592720336800) assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \ long(11188719610782480504630258070757734324011354208865721592720336801) assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(999) assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(1000)
def test_evalf_near_integers(): # Binet's formula f = lambda n: ((1 + sqrt(5))**n)/(2**n * sqrt(5)) assert NS(f(5000) - fibonacci(5000), 10, maxn=1500) == '5.156009964e-1046' # Some near-integer identities from # http://mathworld.wolfram.com/AlmostInteger.html assert NS('sin(2017*2**(1/5))', 15) == '-1.00000000000000' assert NS('sin(2017*2**(1/5))', 20) == '-0.99999999999999997857' assert NS('1+sin(2017*2**(1/5))', 15) == '2.14322287389390e-17' assert NS('45 - 613*E/37 + 35/991', 15) == '6.03764498766326e-11'
def test_evalf_near_integers(): # Binet's formula f = lambda n: ((1 + sqrt(5)) ** n) / (2 ** n * sqrt(5)) assert NS(f(5000) - fibonacci(5000), 10, maxprec=1500) == "5.156009964e-1046" # Some near-integer identities from # http://mathworld.wolfram.com/AlmostInteger.html assert NS("sin(2017*2**(1/5))", 15) == "-1.00000000000000" assert NS("sin(2017*2**(1/5))", 20) == "-0.99999999999999997857" assert NS("1+sin(2017*2**(1/5))", 15) == "2.14322287389390e-17" assert NS("45 - 613*E/37 + 35/991", 15) == "6.03764498766326e-11"
def test_guess_generating_function(): x = Symbol('x') assert guess_generating_function([fibonacci(k) for k in range(5, 15)]) == ((3*x + 5)/(-x**2 - x + 1)) assert guess_generating_function( [1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919]) == ( (1/(x**4 + 2*x**2 - 4*x + 1))**(sympify("1/2"))) assert guess_generating_function(sympify( "[3/2, 11/2, 0, -121/2, -363/2, 121, 4719/2, 11495/2, -8712, -178717/2]") ) == (x + sympify("3/2"))/(11*x**2 - 3*x + 1)
def test_guess_generating_function(): x = Symbol('x') assert guess_generating_function([fibonacci(k) for k in range(5, 15) ]) == ((3 * x + 5) / (-x**2 - x + 1)) assert guess_generating_function( [1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919]) == ((1 / (x**4 + 2 * x**2 - 4 * x + 1))**(sympify("1/2"))) assert guess_generating_function( sympify( "[3/2, 11/2, 0, -121/2, -363/2, 121, 4719/2, 11495/2, -8712, -178717/2]" )) == (x + sympify("3/2")) / (11 * x**2 - 3 * x + 1)
def test_guess_generating_function(): x = Symbol('x') assert guess_generating_function([fibonacci(k) for k in range(5, 15)])['ogf'] == ((3*x + 5)/(-x**2 - x + 1)) assert guess_generating_function( [1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919])['ogf'] == ( (1/(x**4 + 2*x**2 - 4*x + 1))**Rational(1, 2)) assert guess_generating_function(sympify( "[3/2, 11/2, 0, -121/2, -363/2, 121, 4719/2, 11495/2, -8712, -178717/2]") )['ogf'] == (x + Rational(3, 2))/(11*x**2 - 3*x + 1) assert guess_generating_function([factorial(k) for k in range(12)], types=['egf'])['egf'] == 1/(-x + 1) assert guess_generating_function([k+1 for k in range(12)], types=['egf']) == {'egf': (x + 1)*exp(x), 'lgdegf': (x + 2)/(x + 1)}
def test_find_simple_recurrence(): a = Function('a') n = Symbol('n') assert find_simple_recurrence([fibonacci(k) for k in range(12)]) == ( -a(n) - a(n + 1) + a(n + 2)) f = Function('a') i = Symbol('n') a = [1, 1, 1] for k in range(15): a.append(5*a[-1]-3*a[-2]+8*a[-3]) assert find_simple_recurrence(a, A=f, N=i) == ( -8*f(i) + 3*f(i + 1) - 5*f(i + 2) + f(i + 3)) assert find_simple_recurrence([0, 2, 15, 74, 12, 3, 0, 1, 2, 85, 4, 5, 63]) == 0
def test_find_simple_recurrence(): a = Function('a') n = Symbol('n') assert find_simple_recurrence([fibonacci(k) for k in range(12) ]) == (-a(n) - a(n + 1) + a(n + 2)) f = Function('a') i = Symbol('n') a = [1, 1, 1] for k in range(15): a.append(5 * a[-1] - 3 * a[-2] + 8 * a[-3]) assert find_simple_recurrence(a, A=f, N=i) == (-8 * f(i) + 3 * f(i + 1) - 5 * f(i + 2) + f(i + 3)) assert find_simple_recurrence([0, 2, 15, 74, 12, 3, 0, 1, 2, 85, 4, 5, 63]) == 0
def test_evalf_integer_parts(): a = floor(log(8) / log(2) - exp(-1000), evaluate=False) b = floor(log(8) / log(2), evaluate=False) assert a.evalf() == 3 assert b.evalf() == 3 # equals, as a fallback, can still fail but it might succeed as here assert ceiling(10 * (sin(1)**2 + cos(1)**2)) == 10 assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \ int(11188719610782480504630258070757734324011354208865721592720336800) assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \ int(11188719610782480504630258070757734324011354208865721592720336801) assert int(floor(GoldenRatio**999 / sqrt(5) + S.Half).evalf(1000)) == fibonacci(999) assert int(floor(GoldenRatio**1000 / sqrt(5) + S.Half).evalf(1000)) == fibonacci(1000) assert ceiling(x).evalf(subs={x: 3}) == 3 assert ceiling(x).evalf(subs={x: 3 * I}) == 3.0 * I assert ceiling(x).evalf(subs={x: 2 + 3 * I}) == 2.0 + 3.0 * I assert ceiling(x).evalf(subs={x: 3.}) == 3 assert ceiling(x).evalf(subs={x: 3. * I}) == 3.0 * I assert ceiling(x).evalf(subs={x: 2. + 3 * I}) == 2.0 + 3.0 * I assert float((floor(1.5, evaluate=False) + 1 / 9).evalf()) == 1 + 1 / 9 assert float((floor(0.5, evaluate=False) + 20).evalf()) == 20 # issue 19991 n = 1169809367327212570704813632106852886389036911 r = 744723773141314414542111064094745678855643068 assert floor(n / (pi / 2)) == r assert floor(80782 * sqrt(2)) == 114242 # issue 20076 assert 260515 - floor(260515 / pi + 1 / 2) * pi == atan(tan(260515))
def test_fibonacci(): assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3] assert fibonacci(100) == 354224848179261915075 assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7] assert lucas(100) == 792070839848372253127 assert fibonacci(1, x) == 1 assert fibonacci(2, x) == x assert fibonacci(3, x) == x**2 + 1 assert fibonacci(4, x) == x**3 + 2 * x
def test_fibonacci(): assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3] assert fibonacci(100) == 354224848179261915075 assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7] assert lucas(100) == 792070839848372253127 assert fibonacci(1, x) == 1 assert fibonacci(2, x) == x assert fibonacci(3, x) == x**2 + 1 assert fibonacci(4, x) == x**3 + 2*x
def test_approximants(): x, t = symbols("x,t") g = [lucas(k) for k in range(16)] assert [e for e in approximants(g)] == ( [2, -4/(x - 2), (5*x - 2)/(3*x - 1), (x - 2)/(x**2 + x - 1)] ) g = [lucas(k)+fibonacci(k+2) for k in range(16)] assert [e for e in approximants(g)] == ( [3, -3/(x - 1), (3*x - 3)/(2*x - 1), -3/(x**2 + x - 1)] ) g = [lucas(k)**2 for k in range(16)] assert [e for e in approximants(g)] == ( [4, -16/(x - 4), (35*x - 4)/(9*x - 1), (37*x - 28)/(13*x**2 + 11*x - 7), (50*x**2 + 63*x - 52)/(37*x**2 + 19*x - 13), (-x**2 - 7*x + 4)/(x**3 - 2*x**2 - 2*x + 1)] ) p = [sum(binomial(k,i)*x**i for i in range(k+1)) for k in range(16)] y = approximants(p, t, simplify=True) assert next(y) == 1 assert next(y) == -1/(t*(x + 1) - 1)
def estimate_iterations(self): """ Estimates the number of iterations required to find the value of x within a range of the specified uncertainty. """ N = sy.symbols('N') initial_range = self._x_end - self._x_start final_range = self._uncertainty ineq = N >= (1 + 2*self._epsilon) / (final_range / initial_range) interval = sy.solveset(ineq, N, sy.S.Reals) interval_start = int(np.ceil(float(interval.args[0]))) for i in range(2, 10): if interval_start <= sy.fibonacci(i): return i-2 raise ValueError('Could not find the correct Fibonacci number.')
def inverse_secant(p, a, n, x0, x1): l = [fibonacci(i) for i in range(3, 20)] j = 0 i = l[j] while i < n: s = (a * (x1 * x0)) % (p**i) x = x1 + x0 x0 = x1 x1 = x x1 -= s j += 1 i = l[j] s = (a * (x1 * x0)) % (p**n) x = x1 + x0 x0 = x1 x1 = x x1 -= s return (x1 % (p**n))
def test_guess_generating_function(): x = Symbol('x') assert guess_generating_function( [fibonacci(k) for k in range(5, 15)])['ogf'] == ((3 * x + 5) / (-x**2 - x + 1)) assert guess_generating_function( [1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919])['ogf'] == ((1 / (x**4 + 2 * x**2 - 4 * x + 1))**S.Half) assert guess_generating_function( sympify( "[3/2, 11/2, 0, -121/2, -363/2, 121, 4719/2, 11495/2, -8712, -178717/2]" ))['ogf'] == (x + Rational(3, 2)) / (11 * x**2 - 3 * x + 1) assert guess_generating_function([factorial(k) for k in range(12)], types=['egf'])['egf'] == 1 / (-x + 1) assert guess_generating_function([k + 1 for k in range(12)], types=['egf']) == { 'egf': (x + 1) * exp(x), 'lgdegf': (x + 2) / (x + 1) }
def main(): fibo_var = 39 arr_fibo = [] for i in range(1,fibo_var): arr_fibo.append(sp.fibonacci(i)) #base will be a number in arr_fibo #ht will be a number which is +- 1 of base #(L**2) = (ht**2) + ((base*0.5)**2) #print arr_fibo #Calculate the difference betweeen the squares of adjacent numbers arr_base = [] for i in range(1,len(arr_fibo)): temp = ((arr_fibo[i]**2) - (arr_fibo[i-1]**2)) arr_base.append(temp) #Remove zeros from the array for i in range(0,len(arr_base)-1): if(arr_base[i] == 0): arr_base.remove(0) summa = 0 for i in range(0,len(arr_base)): ht = arr_base[i] + 1 base = arr_base[i] var = math.sqrt(((base*0.5)**2) + (ht**2)) if(var.is_integer()): #print int(var) summa += int(var) #################### ht = arr_base[i] - 1 base = arr_base[i] var = math.sqrt(((base*0.5)**2) + (ht**2)) if(var.is_integer()): #print int(var) summa += int(var) print summa
def test_find_linear_recurrence(): assert sequence((0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), \ (n, 0, 10)).find_linear_recurrence(11) == [1, 1] assert sequence((1, 2, 4, 7, 28, 128, 582, 2745, 13021, 61699, 292521, \ 1387138), (n, 0, 11)).find_linear_recurrence(12) == [5, -2, 6, -11] assert sequence(x*n**3+y*n, (n, 0, oo)).find_linear_recurrence(10) \ == [4, -6, 4, -1] assert sequence(x**n, (n, 0, 20)).find_linear_recurrence(21) == [x] assert sequence((1, 2, 3)).find_linear_recurrence(10, 5) == [0, 0, 1] assert sequence(((1 + sqrt(5))/2)**n + \ (-(1 + sqrt(5))/2)**(-n)).find_linear_recurrence(10) == [1, 1] assert sequence(x*((1 + sqrt(5))/2)**n + y*(-(1 + sqrt(5))/2)**(-n), \ (n,0,oo)).find_linear_recurrence(10) == [1, 1] assert sequence((1, 2, 3, 4, 6), (n, 0, 4)).find_linear_recurrence(5) == [] assert sequence((2,3,4,5,6,79),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([], None) assert sequence((2,3,4,5,8,30),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([19/2, -20, 27/2], (-31*x**2 + 32*x - 4)/(27*x**3 - 40*x**2 + 19*x -2)) assert sequence(fibonacci(n)).find_linear_recurrence(30,gfvar=x) \ == ([1, 1], -x/(x**2 + x - 1))
def test_find_linear_recurrence(): assert sequence((0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), \ (n, 0, 10)).find_linear_recurrence(11) == [1, 1] assert sequence((1, 2, 4, 7, 28, 128, 582, 2745, 13021, 61699, 292521, \ 1387138), (n, 0, 11)).find_linear_recurrence(12) == [5, -2, 6, -11] assert sequence(x*n**3+y*n, (n, 0, oo)).find_linear_recurrence(10) \ == [4, -6, 4, -1] assert sequence(x**n, (n,0,20)).find_linear_recurrence(21) == [x] assert sequence((1,2,3)).find_linear_recurrence(10, 5) == [0, 0, 1] assert sequence(((1 + sqrt(5))/2)**n + \ (-(1 + sqrt(5))/2)**(-n)).find_linear_recurrence(10) == [1, 1] assert sequence(x*((1 + sqrt(5))/2)**n + y*(-(1 + sqrt(5))/2)**(-n), \ (n,0,oo)).find_linear_recurrence(10) == [1, 1] assert sequence((1,2,3,4,6),(n, 0, 4)).find_linear_recurrence(5) == [] assert sequence((2,3,4,5,6,79),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([], None) assert sequence((2,3,4,5,8,30),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([19/2, -20, 27/2], (-31*x**2 + 32*x - 4)/(27*x**3 - 40*x**2 + 19*x -2)) assert sequence(fibonacci(n)).find_linear_recurrence(30,gfvar=x) \ == ([1, 1], -x/(x**2 + x - 1))
def test_guess_generating_function_rational(): x = Symbol('x') assert guess_generating_function_rational([fibonacci(k) for k in range(5, 15)]) == ((3*x + 5)/(-x**2 - x + 1))
def apply(self, n, evaluation): 'Fibonacci[n_Integer]' return Integer(sympy.fibonacci(n.get_int_value()))
def test_guess_generating_function_rational(): x = Symbol('x') assert guess_generating_function_rational([ fibonacci(k) for k in range(5, 15) ]) == ((3 * x + 5) / (-x**2 - x + 1))
def fibonacci_numbers(start=0, limit=100, symbolic_keys=True): f = IndexedBase('f') return {(f[i] if symbolic_keys else i):fibonacci(i) for i in range(start, limit)}
def apply(self, n, evaluation): 'Fibonacci[n_Integer]' return Integer(sympy.fibonacci(n.to_sympy()))
""" Created Nov 15, 2012 Author: Spencer Lyon Project Euler Problem 25 """ import sympy as sym import numpy as np fibs = [str(sym.fibonacci(n)) for n in xrange(5000)] lens = np.array([len(fibs[i]) for i in xrange(5000)]) ans = np.where(lens == 1000)[0][0] print 'The answer is: ', ans
from sympy import fibonacci maximum = 4000000 total = 0 n = 0 while (fibonacci(n) < maximum): if (fibonacci(n).is_even): total += fibonacci(n) n += 1 print(total)
def test_issue_10382(): n = Symbol('n', integer=True) assert limit_seq(fibonacci(n+1)/fibonacci(n), n) == S.GoldenRatio
def test_issue_10382(): n = Symbol('n', integer=True) assert limit_seq(fibonacci(n + 1) / fibonacci(n), n) == S.GoldenRatio
def test_find_simple_recurrence_vector(): assert find_simple_recurrence_vector( [fibonacci(k) for k in range(12)]) == [1, -1, -1]
# SOLUTION: 😁 def a(n): return (sp.fibonacci(n + 1) + sp.lucas(n)) / (sp.fibonacci(n + 2) + sp.lucas(n + 1)) cnt = 0 sum = 0 n = 1 while cnt < 30: cnt += 1 print(cnt, (3 - 2 * a(2 * n - 1)) / (1 - a(2 * n - 1) - (a(2 * n - 1) * a(2 * n - 1))) - 3) cnt += 1 print( cnt, 3 * sp.fibonacci(2 * n + 1)**2 - 2 * sp.fibonacci(2 * n) * sp.fibonacci(2 * n + 1) - 3) sum += (3 - 2 * a(2 * n - 1)) / (1 - a(2 * n - 1) - (a(2 * n - 1) * a( 2 * n - 1))) - 3 + 3 * sp.fibonacci(2 * n + 1)**2 - 2 * sp.fibonacci( 2 * n) * sp.fibonacci(2 * n + 1) - 3 n += 1 print("Ans: ", sum) """ 1 2 2 5 3 21 4 42 5 152 6 296 7 1050
def test_fiftieth_fibonacci_tuple(): """Ensure that the request for the 50th Fibonacci number is correct according to sympy function.""" number = 50 result = fibonacci.fibonacci_tuple(number) assert len(result) == number assert sympy.fibonacci(50) == result[-1]
def a(n): return (sp.fibonacci(n + 1) + sp.lucas(n)) / (sp.fibonacci(n + 2) + sp.lucas(n + 1))
from treys import Card, Evaluator import extended as xt import roman import networkx as nx evaluator = Evaluator() getcontext().prec, getcontext().rounding = 2000, ROUND_DOWN #%% Problem 1 #this returns the sum of the list from 0 to 999, divisible by either 3 or 5 np.sum([i for i in range(1000) if i%3==0 or i%5==0]) #%% Problem 2 #the inner brackets do the fibonacci sequence, which is filtered by the outer #brackets. We then find the sum of the filtered list int(np.sum([j for j in [sp.fibonacci(i) for i in range(1000)] if j<4*10**6 and j%2==0])) #%% Problem 3 #simple sympy number theory max(sp.primefactors(600851475143)) #%% Problem 4 #the inner bracket generates the products and the outer checks if the string of #that number is equal to its reverse max([k for k in [i*j for i in range(1,1000) for j in range(1,1000)] \ if str(k)==str(k)[::-1]]) #%% Problem 5 #numpy has this built in np.lcm.reduce(range(1,20))
from sympy import fibonacci maximum = 1000 index = 0 while (len(str(fibonacci(index))) != maximum): index += 1 print(index)