fibonaccis numbers

how would i go about Displaying Fibonacci numbers that are prime numbers up to a specific number (ex 100).?



Can you create a function that calculates fibonacci numbers?

If so, please show code.

Can you create a function to determine if a number is prime?

If so, please also show code.
for(int total = 0; total < num; total++)
{
prime = true;
total = first + second ;

first = second;

second = total;
for( int j = 2; j < total / 2 + 1; j++)

if( total % j == 0)

prime = false;
if(prime == true)

cout << total << endl;
}
So that's a no on both questions.

I'm guessing efficiency isn't too much of an issue, so I'd split it up in parts:
a) Generate all Fib numbers up to 100.
b) Generate all Primes up to 100.
c) For each element of Fib, run through Primes and check if there's a match. If not, print.

Once you get it working, you can worry about optimization or combining parts to cut away some double work. Especially Prime generation can be done much more efficiently than a naive implementation. It's not going to matter much for "up to 100", but it's interesting stuff!
Random remarks on the algos involved:

1. There's a close formula for the Fibonacci numbers, check this kindergarten-like coloured site
http://www.math.hmc.edu/funfacts/ffiles/10002.4-5.shtml

2. Primality test is hard stuff. Check the wiki page. The simplest algo is Eratosthenes sieve.

3. Use the Source code button
Last edited on
Topic archived. No new replies allowed.