Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got a challenge from www.hackerrank.com, calculate the sum of even Fibonacci number. I have submitted the logic that will calculate even Fibonacci numbers but i have used the loop to calculate the sum. Is there any mathematical logic that will calculate sum of even Fibonacci number without looping. Thanks in advance.
Posted
Comments
Patrice T 9-Oct-15 4:10am    
Show your code, it may be possible to optimize.

No.
Well, that's not true, you could make it a recursive algorithm instead, but...that's even less efficient.

Fibonacci numbers are a sequence - each term depends on the two value of the two previous terms. So in order to evaluate F(n), you need to know both F(n-1) and F(n-2), both of which depend on two earlier terms.

So no, you can't magically find the value of F(n) without a loop being involved. Unless you use that loop to generate all the values in advance and store them for your app to access - but since the sequence is unbounded, you don;t have space for that...:laugh:
 
Share this answer
 
Since the Fibonacci series is easily generated with a loop, I wouldn't even bother if there are alternate algorithms (simplex sigillum veri). However you might try recursion. It is a pity the C# programming language doesn't support tail recursion optimization...Oops, wait... "Tail recursion in C#"[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900