for (int x = 3; x <= 10000; x++)
{
int f = fibonacci(x);
if (isPrime(f))
{
cout << setw(2) << nCounter << setw(18) << f << endl;
nCounter++;
}
}
cout << "Enter any character to quit: ";
cin.get();
As the title says, I've been stuck trying to find a suitable way to exit my for loop, but also use my functions in the correct way. I tried doing while (f <= 10000)
and a few other methods, but the answers always vary.
The program is designed to run through the Fibonacci sequence and checks to see if the numbers in the sequence are a "Prime" number, until the Fibonacci gets to 10000 or whatnot.
Currently when it runs it just goes on until it reaches a large negative number.
I CANNOT USE VECTORS
entire code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
bool isPrime(long long n);
long long fibonacci(int n);
int main()
{
double nCounter = 1;
cout << "Fibonacci Primes by Luke" << endl;
cout << endl;
cout << setw(2) << "n" << setw(18) << "Fibonacci Prime" << endl;
cout << "==" << setw(18) << "===============" << endl;
for (int x = 3; x <= 10000; x++)
{
int f = fibonacci(x);
if (isPrime(f))
{
cout << setw(2) << nCounter << setw(18) << f << endl;
nCounter++;
}
}
cout << "Enter any character to quit: ";
cin.get();
}
bool isPrime(long long n)
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
long long fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
here is the while I tried using, but the long long fibonaci always returns '2' and creates an infinite loop.
int x = 3;
long long f;
while (f <= 10000)
int f = fibonacci(x);
if (isPrime(f))
{
cout << setw(2) << nCounter << setw(18) << f << endl;
nCounter++;
}