Given a number, find the numbers (smaller than or equal to n) which are both Fibonacci and prime.
Examples:
Input : n = 40 Output: 2 3 5 13 Explanation : Here, range(upper limit) = 40 Fibonacci series upto n is, 1, 1, 2, 3, 5, 8, 13, 21, 34. Prime numbers in above series = 2, 3, 5, 13. Input : n = 100 Output: 2 3 5 13 89 Explanation : Here, range(upper limit) = 40 Fibonacci series upto n are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89. Prime numbers in Fibonacci upto n : 2, 3, 5, 13, 89.
A simple solution is to iterate generate all fibonacci numbers smaller than or equal to n. For every Fibonacci number, check if it is prime or not. If prime, then print it.
An efficient solution is to use Sieve to generate all Prime numbers up to n. After we have generated prime numbers, we can quickly check if a prime is Fibonacci or not by using the property that a number is Fibonacci if it is of the form 5i2 + 4 or in the form 5i2 – 4. Refer this for details.
Below is the implementation of above steps
C++
// CPP program to print prime numbers present
// in Fibonacci series.
#include <bits/stdc++.h>
using namespace std;
// Function to check perfect square
bool isSquare(int n)
{
int sr = sqrt(n);
return (sr * sr == n);
}
// Prints all numbers less than or equal to n that
// are both Prime and Fibonacci.
void printPrimeAndFib(int n)
{
// Using Sieve to generate all primes
// less than or equal to n.
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
// Now traverse through the range and print numbers
// that are both prime and Fibonacci.
for (int i=2; i<=n; i++)
if (prime[i] && (isSquare(5 * i * i + 4) > 0 ||
isSquare(5 * i * i - 4) > 0))
cout << i << " ";
}
// Driver function
int main()
{
int n = 30;
printPrimeAndFib(n);
return 0;
}
Java
// Java program to print prime numbers
// present in Fibonacci series.
class PrimeAndFib
{
// Function to check perfect square
Boolean isSquare(int n)
{
int sr = (int)Math.sqrt(n);
return (sr * sr == n);
}
// Prints all numbers less than or equal
// to n that are both Prime and Fibonacci.
static void printPrimeAndFib(int n)
{
// Using Sieve to generate all
// primes less than or equal to n.
Boolean[] prime = new Boolean[n + 1];
// memset(prime, true, sizeof(prime));
for (int p = 0; p <= n; p++)
prime[p] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
// Now traverse through the range and
// print numbers that are both prime
// and Fibonacci.
for (int i=2; i<=n; i++) {
double sqrt = Math.sqrt(5 * i * i + 4);
double sqrt1 = Math.sqrt(5 * i * i - 4);
int x = (int) sqrt;
int y = (int) sqrt1;
if (prime[i]==true && (Math.pow(sqrt,2) ==
Math.pow(x,2) || Math.pow(sqrt1,2) ==
Math.pow(y,2)))
System.out.print(i+" ");
}
}
// driver program
public static void main(String s[])
{
int n = 30;
printPrimeAndFib(n);
}
}
// This code is contributed by Prerna Saini
Output:
2 3 5 13
This article is contributed by Abhishek Sharma 44. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

