Find the sum of all the Fibonacci numbers less than 1 billion which have their last 2 digits same in decimal representation.
Details and assumptions :
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 , … .
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.
Here is a solution in turbo C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Note:
1.I first found out that fib[45]>1,000,000,000
2.The first seven fibonacci numbers are single digit numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
My cute solution in java to the question : class q5{ 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(check(c)){
sum=sum+c;
}
a=b;
b=c;
}
System.out.print(sum);
}
public static boolean check(long no){
if(no<10)return false;
long a=no%10;
no=no/10;
if(a==(no%10))return true;
return false;
}
}
Java Solution: public class Fibo3 {
int Fn,Fn1,Fn2;
int dig10,dig100;
long tot;
Fibo3()
{
Fn=0;
Fn1=1;
Fn2=1;
tot=0;
for(int i=0;i<100000000;i++)
{
if (Fn<1000000000)
{
Fn=Fn1+Fn2;
dig100=Fn%100;
dig10=dig100%10;
dig100-=dig10;
dig100=dig100/10;
if(dig100-dig10==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
Fibo3 f= new Fibo3();
}
}
run: 0 0 0 0 0 0 0 55 55 199 432 809 809 809 809 809 809 809 809 18520 18520 18520 18520 18520 18520 336331 336331 336331 336331 336331 336331 336331 336331 336331 336331 336331 336331 102670486 102670486 102670486 102670486 804079219 804079219
Here is the java solution:
public class brilliant{
public static void main(String args[]){
long previous=1, current=1, next;
long sum=0;
while(current<1000000000){
next = previous + current;
previous = current;
current = next;
if((current%100)%11==0) sum += current;
}
System.out.println(sum);
}
}
PARI/GP code:
a(n)=m=Vec(Str(n));m[#m]==m[#m-1]
answer=0;for(i=7,44,if(a(fibonacci(i)),answer=answer+fibonacci(i)));return(answer)
This is the solution using Python
As the pic shows, sum(num) , i.e. the answer is 8 0 4 0 7 9 2 1 9
Problem Loading...
Note Loading...
Set Loading...
Wrote this Code on Python 2.7 and Save it as ModFib.py and Ran it
So it Prints on Screen the value of sum = 804079219