Prompt:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
I'd like advice on:
- Using mathematical formulas instead of loops to calculate this
- Making the code more efficient
This is my solution:
function EvenFibonacciNumbers(){
var value=parseInt(document.getElementById("input").value);
var sum=0;
var temp=0;
var num2=1;
var num1=1;
while(num2<value){
temp=num1+num2;
num1=num2;
num2=temp;
if(num2%2==0){
sum+=temp;
}
}
//document.getElementById("output").value=sum;
alert("result ="+sum);
}
-
2\$\begingroup\$ For mathematical solutions, I suggest reading this answer of mine to a different question. (Skip to the \$\LaTeX\$.) \$\endgroup\$Veedrac– Veedrac2015年06月25日 16:22:46 +00:00Commented Jun 25, 2015 at 16:22
2 Answers 2
Naming
var num1
That doesn't tell me what the variable is and does. Is it the smallest Fibonacci number in the two needed to calculate the next?
Operators
Using spaces around your operators helps readability, which helps prevent bugs.
Scope
temp
is only used in the while()
loop, so you can reduce its scope.
Algorithm
The Fibonacci numbers go even/odd/odd/even/odd/odd... There is a good reason for this - they start with an even number (0), and an odd number (1). Even numbers plus odd numbers are odd numbers, so we get another odd number (1). Odd numbers plus odd numbers are even numbers, so we know this pattern will continue. With this knowledge, we only need to calculate every 3rd number.
-
\$\begingroup\$ OK, so should I delete this answer? \$\endgroup\$user34073– user340732015年06月25日 16:36:55 +00:00Commented Jun 25, 2015 at 16:36
-
\$\begingroup\$ @Hosch250 Can you give me a reference to equation that you have mention. \$\endgroup\$menaka– menaka2015年06月25日 18:17:16 +00:00Commented Jun 25, 2015 at 18:17
-
\$\begingroup\$ @menaka It is in the link provided, just lower down. \$\endgroup\$user34073– user340732015年06月25日 18:17:42 +00:00Commented Jun 25, 2015 at 18:17
-
\$\begingroup\$ You can also do a much less expensive check for integer oddness with the bitwise & operator. This will evaluate to
true
:if (num2 & 1) // if num2 is odd
\$\endgroup\$jperl– jperl2015年06月25日 20:38:56 +00:00Commented Jun 25, 2015 at 20:38
You don't need to check for even fibonacci , because in fibonacci sequence every third number is even(previous two odd numbers sum is even). Below is simple Python implementation.
fib =[]
fib.append(1);fib.append(2)
for i in range(2,50):
fib.append(fib[i-1]+fib[i-2])
N=input()
i=1;sum=0
while(fib[i]<=N):
sum+=fib[i]
i+=3
print sum
-
\$\begingroup\$ What is the point of creating an array ? \$\endgroup\$JaDogg– JaDogg2015年07月04日 14:14:03 +00:00Commented Jul 4, 2015 at 14:14
-
\$\begingroup\$ @Bhathiya Because if there are multiple queries we don't need to compute them again and again. \$\endgroup\$Lakshman– Lakshman2015年07月13日 16:37:30 +00:00Commented Jul 13, 2015 at 16:37
Explore related questions
See similar questions with these tags.