Prime Fibonacci? Sounds Crazy!

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}

So 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 terms in the Fibonacci sequence which are less than 1 billion \textbf{1 billion} and are prime numbers \textbf{prime numbers} .


Details and assumptions :-

\bullet A prime number is the number which has only 2 2 positive integer divisors, which are 1 1 and the number itself. No other positive integer divides the number.

\bullet 1 is not a prime number, 2 is the only even prime number.


This problem is a part of the set Crazy Fibonacci


The answer is 434039265.

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.

14 solutions

Ronak Agarwal
Sep 5, 2014

Java code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
long stime = System.currentTimeMillis() ;
    int i = 0,sum=0,j=1 ;
    for(;j<1000000000;){
    i=i+j ;
    j=i+j ;
    if(isprime(i))
       {if(isprime(j))
            sum=sum+i+j ;
        else
            sum=sum+i ;}
    else
        {if(isprime(j))
            sum=sum+j ;}
    }
    System.out.print(sum) ;
    long etime = System.currentTimeMillis() ;
    long time = etime-stime ;
    System.out.print(",Time taken is "+time+"ms");

Also I have created a method called isprime which tests whether a number is prime or not. It's code is as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 public static boolean isprime(long n){
   if(n==2)
       return true ;  // checking whether the number is 2 or not
   else        // else 1
      {if(n==1)
          return false ; // checking whether the number is 1 or not
       else  // else 2
         {int k = (int) Math.pow(n,0.5) ;  // taking square root of number
          if(n%2==0)
          return false ;
          else      //else 3
             {for(int s=3;s<k+1;s=s+2){
              if(n%s==0)   // checking by dividing against all odd numbers
              return false ;} // bracket of for loop
              return true ;}  // bracket of else 3

         } //brakcet of else 2
       }  // brakcet of else 1
   } // bracket of method 

The result appended is as follows :

1
434039265,Time taken is 1ms

I appreciate everybody who can program. Very good, your solution uses only programming ! I upvote though I don't know Java :D

BTW, you might like to see the newest problem in the set of Mistakes give rise to Problems, the 17th part of the set!!!

Aditya Raut - 6 years, 9 months ago

See my code and please try run both and tell me the run time as i found your to be more than 1 ms which you propose .... The statements i used reduce the run time have a look.... And the funny thing : we both used for(;x<1000000000;) ..... :)

Kartikay Shandil - 6 years, 8 months ago

Actually you don't need a else branch in your prime test method . When you return a value, your method will be immediately terminated.

Aryan Gaikwad - 6 years, 2 months ago
Aditya Raut
Sep 4, 2014

Just like all other problems of this set, make a list in python, and then operate the list. img img

We see that in the list, some directly seen primes are 2 , 3 , 5 , 13 , 89 2,3,5,13,89


For the other ones, we'll try factorizing the suspicable elements of the list, i.e. those who are not even, or end with 5 , not divisible by 3.

I used G e o G e b r a GeoGebra for factorizing the numbers.

We get Prime factorizations of the numbers as 89 = prime 233 = prime 377 = 13 × 29 987 = divisible by 3 1597 = prime 4181 = 37 × 113 17711 = 89 × 199 28657 = prime 121393 = 233 × 521 317811 = 3 × 13 × 29 × 281 514229 = prime 1346269 = 557 × 2417 2178309 = divisible by 3 5702887 = 1597 × 3571 24157817 = 73 × 149 × 2221 39088169 = 37 × 113 × 9349 165580141 = 2789 × 59369 433494437 = prime 701408733 = divisible by 3 \displaystyle 89=\textbf{prime}\\ 233=\textbf{prime}\\ 377=13\times 29 \\987=\textbf{divisible by 3}\\ 1597=\textbf{prime}\\ 4181=37\times 113\\ 17711=89\times 199\\ 28657=\textbf{prime}\\ 121393=233\times 521 \\ 317811=3\times 13\times 29\times 281 \\514229=\textbf{prime}\\ 1346269=557\times 2417 \\2178309=\textbf{divisible by 3}\\ 5702887=1597\times 3571 \\24157817=73\times 149\times 2221 \\39088169=37\times 113 \times 9349\\165580141=2789\times 59369\\ 433494437=\textbf{prime}\\ 701408733=\textbf{divisible by 3}

Sum of all the primes is 2 + 3 + 5 + 13 + 89 + 233 + 1597 + 28657 + 514229 + 433494437 = 434039265 2+3+5+13+89+233+1597+28657+514229+433494437 =\boxed{434039265}

Sir, isn't this a good level problem? Please give at least level 4.

And I really don't know how to solve it without using GeoGebra factorization. Please provide a way in Python, thank you very much in advance. @Calvin Lin

Aditya Raut - 6 years, 9 months ago

Log in to reply

Why use Geogebra factorization. Use can write a code to check whether a number is prime or not. I had the same code and I created a method in java to check whether a number is prime or not.

Ronak Agarwal - 6 years, 9 months ago

Log in to reply

But that is a long process in so big numbers, like i tried import math and check till ceil of square-root, and still it took a long time ! So i aborted it and thought of GeoGebra.

Aditya Raut - 6 years, 9 months ago

Log in to reply

@Aditya Raut The code posted by me gives me the result in 0.001 sec. So it's pretty fast.

Ronak Agarwal - 6 years, 9 months ago

Log in to reply

@Ronak Agarwal Lovely ! Awesome job buddy !

Aditya Raut - 6 years, 9 months ago

BTW can you help me to create a program in PYTHON which tests if a number is a prime or not , or displays the first n n primes. Either of the two will work or both then the best because I have tried much but was unable to accomplish this task.

Utkarsh Dwivedi - 6 years, 9 months ago

Log in to reply

The check the scheme of finding whether a number is prime or not in my java code. Then you can build the same in python.

Ronak Agarwal - 6 years, 9 months ago
Swapnil Bhargava
Sep 28, 2014

Here's my C++ code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<cmath>
#define num 1000000000
using namespace std;
bool checkPrime(long long int n)
{
    if(n==1)
        return false;
    long long int i=sqrt(n);
    for(;i>=2;i--)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    unsigned long long int a=0,b=1,sum=0,c;
    c=a+b;
    while(c<=num)
    {
        if(checkPrime(c))
            sum+=c;
        a=b;
        b=c;
        c=a+b;
    }
    cout<<sum;
    return 0;
}

To check prime, we can consider only 6n+1 and 6n+5 numbers. At least odd numbers and 2 divisibility

Hasmik Garyaka - 3 years, 9 months ago
Thaddeus Abiy
Sep 24, 2014

In Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import java.math.BigInteger;
public class main {  
  public static void main (String[] args) {
      BigInteger a = new BigInteger("1");
      BigInteger b = new BigInteger("1");
      BigInteger c;
      BigInteger Sum = BigInteger.ZERO;
      while (b.compareTo(new BigInteger("1000000000"))==-1){
          c = a.add(b);  a = b ;  b = c;
          if (b.isProbablePrime(10))
              Sum = Sum.add(b);  
      }
      System.out.println(Sum);
     }  
}    

Another colourful solution:

Aryan Gaikwad
Mar 5, 2015

Java solution -

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 public static void main(String args[]){
    double a = 2, b = 3, sum = 0; 
    final double bil = 1_000_000_000;
    do{
        a += b; b += a;
        if( prime(a) ) sum += a;
        if( prime(b) ) sum += b;
    }while(a < bil && b < bil);
    System.out.println(sum + 2 + 3); 
}

static boolean prime(double n) {
    if (n%2==0) return false;
    for(int i=3;i*i<=n;i+=2)
        if(n%i==0) return false;
    return true;
} 

Execution time - 0.001983184 seconds

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def fibo():
    a, b = 0,1
    yield a
    yield b
    while True:
        a,b = b, a+b
        yield b

def good_ones():
    for n in fibo():
        if n > 10**9:
            break
        if is_prime(n):
            yield n

print(sum(n for n in good_ones()))

You Kad - 4 years, 10 months ago
Joe Pauly
Jan 8, 2015

Conditions

Numbers must be in Fibonacci series Fibonacci series should not go beyond 1 billion The sum should be calculated only for numbers that are prime

C# Program

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int sum,a,b,c,f; a=1; b=1; sum =0; c = a + b; while(c<1000000000) { c=a+b; f=0; for (int j = 2; j < (c / 2); j++) { if (c % j == 0) { f = 1; break; } }

            if (f == 0)
            { sum = sum + c; }
            a = b;
            b = c;

        }

        Console.WriteLine("sum is "+sum);
        Console.ReadLine();
            }



}

}

Ratnadip Kuri
Jan 1, 2015

Using Java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class value {
    static boolean isprime(int x)
    {
        boolean t=true;
        for(int i=2;i<Math.sqrt(x);i++)
        {
            if(x%i==0){
                t=false;
                break;
            }else
                t=true;
        }

        return t;
    }
    public static void main(String[] args) {
        int f1=1,f2=1;
        int sum=0;

        int fn=f1+f2;
        for(;fn<1000000000;)
        {
            if(isprime(fn))
            {
                sum+=fn;
            }
            f1=f2;
            f2=fn;
            fn=f1+f2;
        }

        System.out.println("\nThe sum is: "+sum);
    }
} 

Youssef Alba
Nov 22, 2014

java implementation:

public static int crazyfibo() { int sum=0; int a=0,b=1; int c=0;

    for (int i=0;i<45;i++)
    {

        c=a+b;

        if (isPrime(c))
        {
            sum+=c;
        }

        a=b;
        b=c;
    }


    return sum;

}

public static boolean isPrime(int n)
{


        if (n <= 3) {
            return n > 1;
        } else if (n % 2 == 0 || n % 3 == 0) {
            return false;
        } else {
            for (int i = 5; i * i <= n; i += 6) {
                if (n % i == 0 || n % (i + 2) == 0) {
                    return false;
                }
            }
            return true;

    }
Mharfe Micaroz
Oct 30, 2014
1
2
3
4
5
6
7
8
from sympy import fibonacci,isprime
A=[]
i=0
while fibonacci(i)<1000000000:
    if isprime(fibonacci(i)) is True:
        A.append(fibonacci(i))
    i+=1
sum(A)

You must install first sympy as a module in python, You can find it in google. Sympy is an elaborated python module that can solve different kinds of mathematics.

I wish I knew this are in this module. I defined is prime and Fibonacci manually. :(

Pranjal Jain - 6 years, 2 months ago
Arthur Komatsu
Oct 12, 2014

Mathematica code:

1
2
3
4
a = 0;
Table[If[PrimeQ[Fibonacci[x]], a = a + Fibonacci[x], o = 1], {x, 1, 
 44}];
Print[a]; 

Kartikay Shandil
Oct 10, 2014

My cute.... or not so cute java code :

class q2{ 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(prime(c)){
        sum=sum+c;
    }
    a=b;
    b=c;
}
System.out.print(sum);

}

public static boolean prime(long no){ if(no==1)return false; if(no==2||no==3||no==3||no==5||no==7)return true; if(no%2==0)return false; if(no%3==0)return false; if(no%5==0)return false; if(no%7==0)return false; long ss=(long)Math.sqrt(no);
for(long i=3;i<ss+1;i=i+2){ if(no%i==0)return false; } return true; }

}

Cf Paul
Sep 26, 2014
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
main()
{
    int fn2=0;
    int fn1=1;
    int j,primecheck;
    int fn=1;
    int sum=0;
    while(fn<1000000000)
    {
        fn2=fn1;
        fn1=fn;
        fn=fn1+fn2;
        primecheck=1;
        for(j=2;j<fn;j++)
        {
            if(((fn%j)==0)||(fn==1))
            primecheck=0;
        }
        if((primecheck==1)&&(fn<1000000000))
                sum=sum+fn;
    }
    printf("sum:%d",sum);
}

Drop TheProblem
Sep 25, 2014
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

int nprimo(int a)
{int b=0;
for(int i=2; i<sqrt(a); i++)
if(a%i == 0)
{b++; break;}
if(b==0) return a; else return 0;
}

int main()
{int fibo[99999],n=2,somma=0; fibo[0]=1; fibo[1]=1;
while(1){fibo[n]=fibo[n-1]+fibo[n-2]; if(fibo[n]>1000000000)break; n++;}
for(int t=2;t<n;t++) {int num=nprimo(fibo[t]); somma+=num;}
cout<<somma;
 system("pause");
return 0;
} 

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...