Given an number A, I want to find the Ath Fibonacci number that is multiple of 3 or if the number representation has at least a 3 on it.
Example:
Fibonacci > 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
Input: 1, Output: 3;
3 is the first Fibonacci number that is multiple of 3 or has an 3 on it.
Input: 3, Output: 21;
21 is the third Fibonacci number that is multiple of 3 or has an 3 on it.
Edit: Variable type changed to unsigned long long int and ajust on Fibonacci generator. Thanks @rcgldr and @Jarod42 for the help!
My code:
#include<bits/stdc++.h>
using namespace std;
int tem(unsigned long long int i){
while(i != 0){
if(i%10 == 3){
return true;
}
i = i/10;
}
return false;
}
int main(){
int a, count = 0;
unsigned long long int f1 = 1, f2 = 1;
while(scanf("%d", &a) != EOF){
for(unsigned long long int i = 2; i > 0; i++){
i = f1 + f2;
f1 = f2;
f2 = i;
if((i%3 == 0) || tem(i)){
count++;
if(count == a){
cout << i << endl;
break;
}
}
}
}
}
When A > 20, it starts to slow down. Makes sense because it tends to be exponecial. My code is not very efficient, but I didn't find an better logic to use.
I looked into these links, but didn't find an conclusion:
Any ideas? Thanks for the help!