'How to extract prime numbers from a fibonacci series in C++?
Please help my code is as follows. I have found out the Fibonacci series within a range successfully but I am finding it difficult to extract the prime numbers from the Fibonacci series. There seems to be a lot of bugs in the prime number part. Please help. Thanks
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, c, first = 0, second = 1, next, flag;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
for (int j = 2; j<next; j++)
{
if (next%j == 0)
{
flag++;
}
if (flag == 0)
{
cout << "Prime numbers are" << next << "\t";
}
}
getch();
}
}
Solution 1:[1]
Updated as Rolland mentions, i edited so that sqrt is computed only once, and oh 1 is not prime, silly me
Check for prime
#include <math.h>
bool isPrime(int n)
{
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
int checkRange = sqrt(n);
for (int i = 2; i <= checkRange; i++)
{
if (n % i == 0)
return false;
}
return true;
}
You then store them in a collection and use another loop to print it out
#include <vector> //Include this - vector is a standard c++ container
//Declare in your function
std::vector<int> primes;
//In your code where you get your next fibbonacci
if (isPrime(next))
primes.push_back(next);
//After you finished looping for fibbonacci
cout << endl << "Prime numbers are" << endl;
for (int i = 0; i < primes.size(); i++)
cout << primes[i] << " ";
Update as OP requested: Fullcode, no vector
Note: when extracting the primes, in order to output them separately with the fibbonacci you have to store them somewhere. Since you dont use vector, i'm gonna implement this with array.
The problem with array is, you have to declare them with a size, but you dont know how many primes you'll get at the moment you declare the it yet. So you'll have to declare the array with size large enough to contain all the primes in your fibbonacci
#include <iostream>
#include <math.h>
#define MAX 100
using namespace std;
bool isPrime(int n)
{
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
int checkRange = sqrt(n);
for (int i = 2; i <= checkRange; i++)
{
if (n % i == 0)
return false;
}
return true;
}
void main()
{
int n, c, first = 0, second = 1, next, primeCount = 0;
int primes[MAX];
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
if (isPrime(next)) //Reuse the above isPrime
{
primes[primeCount] = next;
primeCount++;
}
}
for (c = 0; c < primeCount; c++)
cout << primes[c] << " ";
}
Solution 2:[2]
You might want to set flag
to 0 first.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | 54skyxenon |