-1
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++;
    }
ooclay
  • 1
  • 1
  • 3
    Are you sure you want to compute 10000 fibonacci numbers? Those numbers are very big, and won't fit in an `int`. – cigien Nov 01 '20 at 02:30
  • 7
    I'm confident that you completely misread your homework assignment. You are not asked to compute the first 10000 fibonacci numbers. You're asked to compute all fibonacci numbers up to 10000. That's two completely different things. – Sam Varshavchik Nov 01 '20 at 02:32
  • I updated my post with the entire code. I'm asked to compute all the prime fibonacci numbers to 10000 and the program works but does not exit when it hits. – ooclay Nov 01 '20 at 02:34
  • Your `while (f <= 10000)` seems to be the correct approach, but you have to use it correctly (like declare `f` *outside* and *before* the loop, and give it an initial value < 10,000). – Adrian Mole Nov 01 '20 at 02:47
  • The edit is better, but you say "*I tried doing while (f <= 10000) and a few other methods*". Could you show that code? Maybe you're just doing it wrong. – cigien Nov 01 '20 at 02:50
  • You may find additional inspiration at [Test if a number is fibonacci](https://stackoverflow.com/q/2432669/3422102) (20 - Answers) – David C. Rankin Nov 01 '20 at 02:55
  • @DavidC.Rankin Actually, that won't help the OP. They're not testing whether a number is Fibonacci or not. OP is generating them and then testing if they're prime. – cigien Nov 01 '20 at 02:57
  • 1
    you can always add a line like `if (f > 10000) break;`..probably not the best solution though – Ric Nov 01 '20 at 02:58
  • @cigien - thank you. So it is effectively the reverse. – David C. Rankin Nov 01 '20 at 03:01
  • @DavidC.Rankin Yes, so it appears :) – cigien Nov 01 '20 at 03:06
  • Tip: `for (int i = 2; i <= n/i; i++)` is much faster than `for (int i = 2; i < n; i++)` for the prime test. – chux - Reinstate Monica Nov 01 '20 at 03:06
  • Thanks for adding the 2nd attempt :) You don't have to address it to me personally however. Instead, say something like "Here's the partial code, but it's not working". – cigien Nov 01 '20 at 03:10
  • Your attempt is basically correct. However, you need to initialize `f` outside the loop, e.g. to the first fibonacci number. Also, you need braces around the body of the `while` loop. Voting to close as typo. – cigien Nov 01 '20 at 03:13

1 Answers1

1

One way would be to check for condition on every iteration and break the loop when f goes beyond 10000

for (int x = 3; x <= 10000; x++)
    {
        int f = fibonacci(x);
        
        if(f > 10000)
            break;
            
        if (isPrime(f))
        {
            cout << setw(2) << nCounter << setw(18) << f << endl;
            nCounter++;
        }
}

You can also do it by converting it to while loop but for that you have to declare and initialise f and x outside the loop.

    int x = 3;
    int f = fibonacci(x);
    while(f <= 10000)
    {
        if (isPrime(f))
        {
            cout << setw(2) << nCounter << setw(18) << f << endl;
            nCounter++;
        }
        x++;
    }
Arsenic
  • 727
  • 1
  • 8
  • 22