fibonaccis numbers

Nov 28, 2011 at 8:35pm
how would i go about Displaying Fibonacci numbers that are prime numbers up to a specific number (ex 100).?



Nov 28, 2011 at 11:12pm
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.
Nov 29, 2011 at 6:35am
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;
}
Nov 29, 2011 at 10:42am
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!
Nov 29, 2011 at 5:19pm
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 Nov 29, 2011 at 5:20pm
Topic archived. No new replies allowed.