The fibonacci sequence is defined as F 0 = 0 , F 1 = 1 and for n ≥ 2 , F n = F n − 1 + F n − 2
Thus, the fibonacci sequence is 0 , 1 , 1 , 2 , 3 , 5 , 8 , 1 3 , . . .
Find the sum of all the numbers less than 1 billion which appear in the Fibonacci Sequence and are divisible by 3 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Sorry Calculator :)
How did that happen ? (The sum ?) The previous statements are awesome conclusions but please tell do you have a method of finding that sum F 4 + F 8 + . . . and how will you decide which term is the largest term less than 1 billion ?
sir,how did u say f44 is the highest
There isn't a need to write a C++ code to solve this problem.
First, we consider the Fibonacci sequence modulo 3. It is (starting with 0)
0 , 1 , 1 , 2 , 0 , 2 , 2 , 1 , 0 , 1 , 1 , 2 , 0 , 2 , 2 , 1 , 0 , …
where the pattern repeats every 8 terms. Hence, we want the sum of every 4th term, till the fibonacci sequence hits 1 billion.
We can apply the Fast Fibonacci Transform, to find every 4th term. It starts off as
0 , 3 , 2 1 , 1 4 4 , 9 8 7 , 6 7 6 5 , 4 6 3 6 8 , 3 1 7 8 1 1 , 2 1 7 8 3 0 9 , 1 4 9 3 0 3 5 2 , 1 0 2 3 3 4 1 5 5 , 7 0 1 4 0 8 7 3 3 , …
Alternatively, if G k is the above sequence, you can prove that G n = 7 G n − 1 − G n − 2 , which is an easy way to obtain the next term. (Why does this work? Can you generalize? Hint: ϕ 6 − 7 ϕ 3 + 1 = 0 .)
This gives us the sum of 821223648.
Here's the calculation that calculates the answer using the geometric progression formula and the Fibonacci n'th term formula .
I solved it using C++. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
First, consider the Fibonacci sequence modulo 3 : 0 , 1 , 1 , 2 , 0 , 2 , 2 , 1 , 0 , 1 , … . This shows that the sum is simply F 0 + F 4 + ⋯ + F 4 4 , which is a geometric series by Binet's formula.
Note: this is a mathematician's solution, not a computer scientist's. This can be solved using Wolfram Alpha purely
Easy one :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Precious, brute force and easy. #include <stdio.h> #include <string.h>
main(){
int i = 0, j = 1, k = 0, sum =0;
for (k=0; k<=1000000000; k=i+j){
i = j;
j = k;
if(k%3==0)
sum = sum + k;
}
printf("%d\n", sum);
}
The output is : run: 0 3 3 3 3 24 24 24 24 168 168 168 168 1155 1155 1155 1155 7920 7920 7920 7920 54288 54288 54288 54288 372099 372099 372099 372099 2550408 2550408 2550408 2550408 17480760 17480760 17480760 17480760 119814915 119814915 119814915 119814915 821223648 821223648
same thang
I solved this using python. here is my code
img
So the answer is 8 2 1 2 2 3 6 4 8
Here's MATLAB code that uses the formula x(n+1)=7*x(n)-x(n-1):
function [S,x] = fibo
x(1) = 3;
x(2) = 21;
for i=3:100
term = 7*x(i-1)-x(i-2);
if(term>10^9)
break;
else
x(i) = term;
end
end
S = sum(x)
end
(I know I could've used recursion, but I'm just sharing a different approach)
For my C++ code I used the formula
F n = ⌊ 5 ϕ n + 2 1 ⌋ = ⌊ 2 5 2 ϕ n + 5 ⌋
And the fact that F 1 0 0 > 1 0 9 .
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 10 |
|
i = 0 has been excluded because F 0 = 0 even being divisible by 3 doesn't alter the sum since adding 0 to number doesn't change its value.
long l = 1000000000; long[] ls = new long[60]; Arrays.fill(ls, -1); ls[0] = 0; ls[1] = 1; for (int i = 2; i < ls.length; i++) { ls[i] = ls[i - 1] + ls[i - 2]; } BigInteger bgInt = BigInteger.ZERO; for (long m : ls) { if (m < l && m % 3 == 0) { bgInt = bgInt.add(BigInteger.valueOf(m)); } }
System.out.println(bgInt);
This is a C code.
#include <stdio.h>
int fibonacci(int term)
{
if(term == 0)
return 0 ;
else if(term == 1)
return 1 ;
else
return(fibonacci(term-1)+fibonacci(term-2)) ;
}
int main(void)
{
int number = -1, term = 0, sum = 0 ;
while(number < 1000000000) {
number = fibonacci(term) ;
if(number % 3 == 0)
sum += number ;
term++ ;
}
printf("%d", sum) ;
return 0 ;
}
To see which terms satisfy the conditions, add the line of code
printf("%d, " number) ;
within the if condition.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Brute force algorithm for c++:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
My cute solution for the question :
class q4{ public static void main(String as[]){ int a=0; int b=1; int c=0; long sum=0;
for(;c<1000000000;){
c=a+b;
if(c%3==0){
sum=sum+c;
}
a=b;
b=c;
}
System.out.print(sum);
}
}
I wrote a small Java program for the same: public class Fibo {
int Fn,Fn1,Fn2,tot;
Fibo()
{
Fn=0;
Fn1=1;
Fn2=1;
tot=0;
for(int i=0;i<100000000;i++)
{
if (Fn<1000000000)
{
Fn=Fn1+Fn2;
if(Fn%3==0)
{
tot+=Fn;
}
System.out.println(""+tot);
Fn2=Fn1;
Fn1=Fn;
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Fibo f= new Fibo();
}
}
My C code for the problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
public class fib
{
static double sum;
public static void main(String[] args)
{
sum = 0;
fib(0,1);
System.out.printf("dexp: %.0f\n", sum);
}
public static void fib(double i, double j)
{
if(i % 3 == 0)
{
sum+=i;
}
if(i > 1000000000)
{
return;
}
fib(j, i+j);
}
}
Problem Loading...
Note Loading...
Set Loading...
There is a certain theorem (I forgot what the name is) saying that F m ∣ F n if and only if m ∣ n .
3 is the fourth Fibonacci number or F 4 .
Therefore 3 divides all Fibonacci numbers in the form F 4 k where k is a positive integer.
F 4 4 is the highest Fibonacci number less than 1 billion.
Therefore the answer is F 4 + F 8 + F 1 2 + . . . + F 4 4 which is equal to 8 2 1 2 2 3 6 4 8