Divisible Fibonacci? Sounds crazy!

The fibonacci sequence is defined as F 0 = 0 , F 1 = 1 F_0=0,F_1=1 and for n 2 n\geq 2 , F n = F n 1 + F n 2 F_n=F_{n-1}+F_{n-2}

Thus, the fibonacci sequence is 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , . . . 0,1,1,2,3,5,8,13,...

Find the sum of all the numbers less than 1 billion \textbf{1 billion} which appear in the Fibonacci Sequence \textbf{Fibonacci Sequence} and are divisible by 3 3 .


The answer is 821223648.

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.

16 solutions

Rindell Mabunga
Aug 28, 2014

There is a certain theorem (I forgot what the name is) saying that F m F n F_m | F_n if and only if m n m | n .

3 3 is the fourth Fibonacci number or F 4 F_4 .

Therefore 3 3 divides all Fibonacci numbers in the form F 4 k F_{4k} where k k is a positive integer.

F 44 F_{44} is the highest Fibonacci number less than 1 1 billion.

Therefore the answer is F 4 + F 8 + F 12 + . . . + F 44 F_4 + F_8 + F_{12} + ... + F_{44} which is equal to 821223648 \boxed{821223648}

Sorry Calculator :)

Rindell Mabunga - 6 years, 8 months ago

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 + . . . F_4+F_8+... and how will you decide which term is the largest term less than 1 billion ?

Aditya Raut - 6 years, 9 months ago

sir,how did u say f44 is the highest

subha jayanthi - 6 years, 8 months ago
Calvin Lin Staff
Aug 27, 2014

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 , 0, 1, 1, 2, 0, 2, 2, 1, 0, 1, 1, 2, 0, 2, 2, 1, 0, \ldots

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 , 21 , 144 , 987 , 6765 , 46368 , 317811 , 2178309 , 14930352 , 102334155 , 701408733 , 0, 3, 21, 144, 987, 6765, 46368, 317811, 2178309,\\ 14930352, 102334155, 701408733, \ldots

Alternatively, if G k G_k is the above sequence, you can prove that G n = 7 G n 1 G n 2 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 \phi^6 - 7 \phi^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 .

mathh mathh - 6 years, 9 months ago
Karthik Kannan
Aug 27, 2014

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
#include <iostream>
using namespace std;
int main()
{
long int prevf,prevprevf,f,sum;
int i;
prevf=0;
prevprevf=1;
sum=0;
for(i=2;f<1000000000;i++)
{
f=prevf+prevprevf;
prevprevf=prevf;
prevf=f;
if(f%3==0){sum=sum+f;}
else{continue;}
}
cout<<"The sum is: "<<sum;
return 0;
} 

Cody Johnson
Sep 12, 2014

First, consider the Fibonacci sequence modulo 3 3 : 0 , 1 , 1 , 2 , 0 , 2 , 2 , 1 , 0 , 1 , 0,1,1,2,0,2,2,1,0,1,\dots . This shows that the sum is simply F 0 + F 4 + + F 44 F_0+F_4+\dots+F_{44} , 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
#include <iostream>
using namespace std;
int main()
{
int Fn_2 = 0;
int Fn_1 = 1;
int Fn,som;
som = 0;
i = 2;
while(Fn < 1000000000){
Fn = Fn_2 + Fn_1;
if(Fn % 3 == 0){
 som += Fn;
}
Fn_2 = Fn_1;
Fn_1 = Fn;
}
cout<<som<<endl;
}

Abhishek DaEvil
Aug 28, 2014

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

Vijesh Balasubramanian - 6 years, 8 months ago

same thang

Aishwary Omkar - 6 years, 7 months ago
Aditya Raut
Aug 27, 2014

I solved this using python. here is my code img img

So the answer is 821223648 \boxed{821223648}

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

Victor Trumper - 6 years, 9 months ago

(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 = ϕ n 5 + 1 2 = 2 ϕ n + 5 2 5 F_n=\lfloor \frac{\phi^n}{\sqrt{5}} + \frac{1}{2} \rfloor=\lfloor \frac{2\phi^n+\sqrt{5}}{2\sqrt{5}} \rfloor

And the fact that F 100 > 1 0 9 F_{100}>10^9 .

1
2
3
4
5
6
7
    int n, sum=0;
        for (n=0;n<=100;n++)
            if (int(floor((2*pow(((1+sqrt(5))/2),n)+sqrt(5))/(2*sqrt(5))))%3==0)
{                   sum=sum+int(floor((2*pow(((1+sqrt(5))/2),n)+sqrt(5))/(2*sqrt(5))));
cout << int(floor((2*pow(((1+sqrt(5))/2),n)+sqrt(5))/(2*sqrt(5)))) << " " << sum << endl;  
}
    return 0; 

mathh mathh - 6 years, 9 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import math
def fib(n):
    return math.floor((1/math.sqrt(5)*((1+math.sqrt(5))**n - (1-math.sqrt(5))**n)/2**n)) #Binet's formula for nth fibonacci number
i=1
s=0
while fib(i)<=10**9:
    if fib(i)%3 == 0:
        s += fib(i)
    i += 1
print(s)

i = 0 i=0 has been excluded because F 0 = 0 F_0 = 0 even being divisible by 3 3 doesn't alter the sum since adding 0 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);
Jaydee Lucero
May 26, 2015

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.

David Holcer
Apr 4, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
fib=[0,1]
n=10**9
while fib[-1]<n:
    test=fib[-1]+fib[-2]
    if test<n:
        fib.append(test)
    else:
        break
summ=0
for i in fib:
    if i%3==0:
        summ+=i
print summ

Spy Mabana
Nov 11, 2014

Brute force algorithm for c++:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
main() {
    int Fprev = 0;
    int Fcurr = 1;
    int sum = 0;
    while(true) {
        Fcurr = Fcurr + Fprev;
        Fprev = Fcurr - Fprev;
        if(Fcurr >= 1000000000) break;
        if(Fcurr % 3 == 0) sum += Fcurr;
    }
    cout << sum << endl;
}

Kartikay Shandil
Oct 11, 2014

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();
}

}

Nj Rafi
Sep 14, 2014

My C code for the problem

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#define limit 1000000000
// Fibonacci Numbers 0 1 1 2 3 5 8 13 21 34 55...
// sum of the Fibonacci numbers less than the limit and are divisible by 3
int main()
{
        long long int i=0,j=1,num=0,sum=0;
        while(num<limit)
        {
                num = i +j;
                i = j;
                j = num;
                if(num%3==0)
                        sum +=num;
        }
        printf("The Sum: %lld", sum);
        return 0;
}

include<bits/stdc++.h>

using namespace std; int main() { long long int sum = 0ll,a,b,c; a=0ll; b=1ll; while(b<1000000000) { c = b + a; a=b; b=c; if(b%3==0) { sum += b; } } cout<<sum<<endl; }

Ketaki Rao - 6 years, 8 months ago
Test 123
Sep 11, 2014

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); }
}








0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...