Fibonacci sequence is defined as F 0 = 0 , F 1 = 1 and for n ≥ 2 F n = F n − 1 + F n − 2
So the Fibonacci sequence is 0 , 1 , 1 , 2 , 3 , 5 , 8 , 1 3 , . . .
Find the sum of all the terms in the Fibonacci sequence which are less than 1 billion and are prime numbers .
Details and assumptions :-
∙ A prime number is the number which has only 2 positive integer divisors, which are 1 and the number itself. No other positive integer divides the number.
∙ 1 is not a prime number, 2 is the only even prime number.
This problem is a part of the set Crazy Fibonacci
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.
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!!!
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;) ..... :)
Actually you don't need a else branch in your prime test method . When you return a value, your method will be immediately terminated.
Just like all other problems of this set, make a list in python, and then operate the list.
img
We see that in the list, some directly seen primes are 2 , 3 , 5 , 1 3 , 8 9
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 for factorizing the numbers.
We get Prime factorizations of the numbers as 8 9 = prime 2 3 3 = prime 3 7 7 = 1 3 × 2 9 9 8 7 = divisible by 3 1 5 9 7 = prime 4 1 8 1 = 3 7 × 1 1 3 1 7 7 1 1 = 8 9 × 1 9 9 2 8 6 5 7 = prime 1 2 1 3 9 3 = 2 3 3 × 5 2 1 3 1 7 8 1 1 = 3 × 1 3 × 2 9 × 2 8 1 5 1 4 2 2 9 = prime 1 3 4 6 2 6 9 = 5 5 7 × 2 4 1 7 2 1 7 8 3 0 9 = divisible by 3 5 7 0 2 8 8 7 = 1 5 9 7 × 3 5 7 1 2 4 1 5 7 8 1 7 = 7 3 × 1 4 9 × 2 2 2 1 3 9 0 8 8 1 6 9 = 3 7 × 1 1 3 × 9 3 4 9 1 6 5 5 8 0 1 4 1 = 2 7 8 9 × 5 9 3 6 9 4 3 3 4 9 4 4 3 7 = prime 7 0 1 4 0 8 7 3 3 = divisible by 3
Sum of all the primes is 2 + 3 + 5 + 1 3 + 8 9 + 2 3 3 + 1 5 9 7 + 2 8 6 5 7 + 5 1 4 2 2 9 + 4 3 3 4 9 4 4 3 7 = 4 3 4 0 3 9 2 6 5
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
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.
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.
Log in to reply
@Aditya Raut – The code posted by me gives me the result in 0.001 sec. So it's pretty fast.
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 primes. Either of the two will work or both then the best because I have tried much but was unable to accomplish this task.
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.
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 |
|
To check prime, we can consider only 6n+1 and 6n+5 numbers. At least odd numbers and 2 divisibility
In Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Another colourful solution:
Java solution -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Execution time - 0.001983184 seconds
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
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
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();
}
}
}
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 |
|
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;
}
1 2 3 4 5 6 7 8 |
|
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. :(
Mathematica code:
1 2 3 4 |
|
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;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Problem Loading...
Note Loading...
Set Loading...
Java code :
Also I have created a method called isprime which tests whether a number is prime or not. It's code is as follows.
The result appended is as follows :