fibonacci - Project Euler #2 in "Python" -


I'm starting a whole new one here, I was giving a try in Python to a query on Project Oller. Can you please tell me where my code is wrong?

Question) By adding the last two words each new word is generated in the Fibonacci sequence. Starting with 1 and 2, the first 10 words will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Fibonacci By considering the words which do not value more than four million in the sequence, also find the worth-quotient conditions. Def fib (a): if ((a == 0) (1 = 1)): 1 and return: return (fiber (A-1)) + (fiber (A) -2)) R = 0 bir 0 while (FIB (R)) <4000000: if (= (Fib (r))% 2) == 0: sum + = fib (r) print (amount)

Your code is not incorrect , it is very slow project To solve more problems, not only should your code be correct, but your algorithm should be efficient.

Your Fibonacci computing is extremely expensive - A In time, o (2 ^ N) time trying to achieve the next Fibonacci number - is too long when you want to add the number to a limit of four million.

A more efficient implementation in Python Type is:

  x = 1 y = 1 z = 0 result = 0 while z <4000000: z = (x + y) if z% 2 == 0: result = result + Z #next iteration x = yy = z Print result    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -