Finding Prime Fibonacci Numbers
My program is suppose to output the first 8 Prime Fibonacci numbers, My output is as follows:
1 1 2 5 13 89 233 1597 4181 28657 514229
1)Why am I getting two "1"?
2)Why am I missing " 3"?
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 35 36 37 38 39 40
|
#include <iostream>
using namespace std;
bool isPrime(int number)
{
int i;
for (i=2; i<number; i++)
{ if(number % i==0)
return false;
}
return true; //will return true otherwise
}
int fib(int n)
{
if(1 == n || 2 == n)
{
return 1;
}
else
{
return fib(n-1) + fib(n-2);
}
}
int main()
{
for(int i=1; i<=30; i++)
{
if (isPrime(i) == true)
{
cout << fib(i) << " ";
}
}
return 0;
}
|
i think because you check 1 first then you get "1" then you check two "1" and the first two number are one and two.
1 2 3
|
if(1 == n || 2 == n)
{
return 1;
|
Last edited on
Topic archived. No new replies allowed.