↑ arvau

Stack Overflow / C++ program to find prime numbers in a fibonacci series

Can someone help me in this? This is a c++ program that I need to find prime numbers of fibonacci series. the question says that after you enter the n ( the number of fibonacci series ) the program has to extract the prime numbers from it, and then, if the sum of those prime numbers is an odd number, it has to show 'A' and if it's even, it should show 'D'. The problem is I know how to find both fibonacci series and prime numbers, but I can't merge them. And I need to keep the code as simple as possible Can someone help me in this?

This one is for fibonacci series:

#include <iostream>
#include <conio.h>
using namespace std;

int main(){
    int n, a = 1, b = 1, c;
    cin >> n;
    cout << a << endl;
    cout << b << endl;
    int i = 2;
    while (i < n)
    {
        c = a + b;
        cout << c << endl; 
        a = b; 
        b = c;   
        i++;
    }
    getch();
}

And this one is for prime numbers :

#include<iostream>
#include<conio.h>
using namespace std;

int main(){

    int a, b = 1, r = 1, c = 0, x, m;
    cout << "please enter number :";
    cin >> a;
    while (b <= a) {
    m = a;
    x = b;
    while (x != 0) {
        r = m % x;
        m = x;
        x = r;
    }
    if (m == 1){
        c++;
        b++;
    }
    cout << c;
    getch();
}

I have to put them together so i can extract the prime numbers in the fibonacci, but I dont know how. And, I need it to show the prime numbers but my code shows how many numbers are prime

Stack Overflow / C++ program to find prime numbers in a fibonacci series

One solution could be to make a function to find the n'th fibonacci number, another function to test if a number is prime, and then loop over the fibonacci number to find the primes

#include <iostream>

int fib(int n) { /* code */ }
bool is_prime(int n) { /* code */ }

int sum_of_prime_in_fib(int lim)
{
    int sum = 0;

    // loop over the fibonacci number to sum the primes
    for (int i = 1; i <= lim; ++i) {
        int f = fib(i);
        if (is_prime(f))
            sum += f;
    }
    return sum;
}

int main()
{
    int n;
    std::cout << "please enter number :";
    std::cin >> n; //remember to check for errors, which I wont do

    std::cout << "the sum of primes is: " << sum_of_prime_in_fib(n) << "\n";
} 

I have left the implementation of the two functions as an exercise

Stack Overflow / C++ program to find prime numbers in a fibonacci series

Since you did not include conio.h content, I will pose a solution from bare-bone.

This question revolves around several stages.

  1. get all fibonacci sequence in a list

  2. use fibonacci sequence to find if any prime numbers exist (put them in a list)

  3. calculate the sum of the prime numbers from that list

  4. if the sum is even, print "A" otherwise print "D".

The code can be broken down to several functions to make it easier and more intuitive.

#include <iostream>
#include <conio.h>
#include <vector>
using namespace std;


vector<int> generate_fibonacci(int n){
    int a = 1, b = 1, c;
    //cout << a << endl;
    //cout << b << endl;
    int i = 2; 
    vector<int> f;
    f.push_back(a);
    f.push_back(b);//assuming that the a,b are part of the fibonacci
    
    while (i < n)
    {
        c = a + b;
        f.push_back(c);
        a = b; 
        b = c;   
        i++;
    }
    return f;
}

bool is_prime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
 
    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}

vector<int> find_prime_from_f(vector<int> v){
    vector<int> fp;
    for(int i: v){
        if(is_prime(i))
        {
            fp.push_back(i);
        }
    }
    return fp;
}

int main(){
    cout << "please enter the number of fibonacci" << endl;
    int n;
    cin >> n;
    vector<int> fibonacciNumber = generate_fibonacci(n);
    vector<int> fibonacciPrime = find_prime_from_f(fibonacciNumber);
    int sum;
    for(int i: fibonacciPrime)
    {
        sum += i;
    }
    if(sum %2 != 0) cout << "A" <<endl; //if it is odd
    else cout << "D" <<endl; //if it is even
    
}

You can of course tailor some of the details of these functions, including adding some bound checking for negative numbers, and optimize the algorithm by using array, doing the calculation and checking on the fly, or simplify the code a little. However, this should serve as a starting point for your further implementation.