Note that there are some explanatory texts on larger screens.

plurals
  1. PONon Brute Force Solution to Project Euler 25
    primarykey
    data
    text
    <p><a href="https://projecteuler.net/problem=25" rel="nofollow noreferrer">Project Euler problem 25</a>:</p> <blockquote> <p>The Fibonacci sequence is defined by the recurrence relation:</p> <p>F<sub>n</sub> = F<sub>n−1</sub> + F<sub>n−2</sub>, where F<sub>1 = 1</sub> and F<sub>2</sub> = 1. Hence the first 12 terms will be F<sub>1</sub> = 1, F<sub>2</sub> = 1, F<sub>3</sub> = 2, F<sub>4</sub> = 3, F<sub>5</sub> = 5, F<sub>6</sub> = 8, F<sub>7</sub> = 13, F<sub>8</sub> = 21, F<sub>9</sub> = 34, F<sub>10</sub> = 55, F<sub>11</sub> = 89, F<sub>12</sub> = 144</p> <p>The 12th term, F<sub>12</sub>, is the first term to contain three digits.</p> <p>What is the first term in the Fibonacci sequence to contain 1000 digits?</p> </blockquote> <p>I made a brute force solution in Python, but it takes absolutely forever to calculate the actual solution. Can anyone suggest a non brute force solution?</p> <pre><code>def Fibonacci(NthTerm): if NthTerm == 1 or NthTerm == 2: return 1 # Challenge defines 1st and 2nd term as == 1 else: # recursive definition of Fib term return Fibonacci(NthTerm-1) + Fibonacci(NthTerm-2) FirstTerm = 0 # For scope to include Term in scope of print on line 13 for Term in range(1, 1000): # Arbitrary range FibValue = str(Fibonacci(Term)) # Convert integer to string for len() if len(FibValue) == 1000: FirstTerm = Term break # Stop there else: continue # Go to next number print "The first term in the\nFibonacci sequence to\ncontain 1000 digits\nis the", FirstTerm, "term." </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload