Program to find Prime Fibonacci Numbers till N
Last Updated :
10 Oct, 2024
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
Python
# Python 3 program to print
# prime numbers present in
# Fibonacci series.
import math
# Function to check perfect square
def isSquare(n) :
sr = (int)(math.sqrt(n))
return (sr * sr == n)
# Prints all numbers less than
# or equal to n that are
# both Prime and Fibonacci.
def printPrimeAndFib(n) :
# Using Sieve to generate all
# primes less than or equal to n.
prime =[True] * (n + 1)
p = 2
while(p * p <= n ):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True) :
# Update all multiples of p
for i in range(p * 2, n + 1, p) :
prime[i] = False
p = p + 1
# Now traverse through the range
# and print numbers that are
# both prime and Fibonacci.
for i in range(2, n + 1) :
if (prime[i] and (isSquare(5 * i * i + 4) > 0 or
isSquare(5 * i * i - 4) > 0)) :
print(i , " ",end="")
# Driver function
n = 30
printPrimeAndFib(n);
# This code is contributed
# by Nikita Tiwari.
C#
// C# program to print prime numbers
// present in Fibonacci series.
using System;
class GFG {
// Function to check perfect square
static bool 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.
bool[] prime = new bool[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)))
Console.Write(i + " ");
}
}
// driver program
public static void Main()
{
int n = 30;
printPrimeAndFib(n);
}
}
// This code is contributed by Anant Agarwal.
JavaScript
<script>
// Javascript program to print prime numbers
// present in Fibonacci series.
// Function to check perfect square
function isSquare(n)
{
let sr = Math.sqrt(n);
return (sr * sr == n);
}
// Prints all numbers less than or equal
// to n that are both Prime and Fibonacci.
function prletPrimeAndFib(n)
{
// Using Sieve to generate all
// primes less than or equal to n.
let prime = [];
// memset(prime, true, sizeof(prime));
for (let p = 0; p <= n; p++)
prime[p] = true;
for (let 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 (let 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 (let i=2; i<=n; i++) {
let sqrt = Math.sqrt(5 * i * i + 4);
let sqrt1 = Math.sqrt(5 * i * i - 4);
let x = Math.floor(sqrt);
let y = Math.floor(sqrt1);
if (prime[i]==true && (Math.pow(sqrt,2) ==
Math.pow(x,2) || Math.pow(sqrt1,2) ==
Math.pow(y,2)))
document.write(i+" ");
}
}
// Driver code
let n = 30;
printPrimeAndFib(n);
</script>
PHP
<?php
// PHP program to print prime numbers
// present in Fibonacci series.
// Function to check perfect square
function isSquare($n)
{
$sr = (int)sqrt($n);
return ($sr * $sr == $n);
}
// Prints all numbers less than or equal
// to n that are both Prime and Fibonacci.
function printPrimeAndFib($n)
{
// Using Sieve to generate all primes
// less than or equal to n.
$prime = array_fill(0, $n + 1, true);
for ($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 ($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 ($i = 2; $i <= $n; $i++)
if ($prime[$i] && (isSquare(5 * $i * $i + 4) > 0 ||
isSquare(5 * $i * $i - 4) > 0))
echo $i . " ";
}
// Driver Code
$n = 30;
printPrimeAndFib($n);
// This code is contributed by mits
?>
Output:
2 3 5 13
Time Complexity: O(nloglogn)
Auxiliary Space: O(n), since n extra space has been taken.
Similar Reads
Program to find Nth odd Fibonacci Number
Given an integer N. The task is to find the Nth odd Fibonacci number.The odd number fibonacci series is as: 1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597.............and so on.Note: In the above series we have omitted even terms from the general fibonacci sequence. Examples: Input: N = 3 Output: 3
3 min read
Program for Fibonacci numbers in PL/SQL
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, â¦â¦.. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-1 with seed values F0= 0 and F1 = 1. Given a number n, print n-
1 min read
Program to find the Nth Prime Number
Given an integer N. The task is to find the Nth prime number. Examples: Input : 5 Output : 11 Input : 16 Output : 53 Input : 1049 Output : 8377 Approach: Find the prime numbers up to MAX_SIZE using Sieve of Eratosthenes.Store all primes in a vector.For a given number N, return the element at (N-1)th
12 min read
Program to find the next prime number
Given an integer N. The task is to find the next prime number i.e. the smallest prime number greater than N. Examples: Input: N = 10 Output: 11 11 is the smallest prime number greater than 10. Input: N = 0 Output: 2 Approach: First of all, take a boolean variable found and initialize it to false.Now
5 min read
Program to Find all Emirp numbers till N
Emirp is the word âprimeâ spelled backwards, and it refers to a prime number that becomes a new prime number when you reverse its digits. Emirps do not include palindromic primes (like 151 or 787) nor 1-digit primes like 7. 107, 113, 149, and 157 â reverse them and youâve got a new prime number on y
9 min read
Program to find last digit of n'th Fibonacci Number
Given a number 'n', write a function that prints the last digit of n'th ('n' can also be a large number) Fibonacci number. Examples : Input : n = 0 Output : 0 Input: n = 2 Output : 1 Input : n = 7 Output : 3 Recommended PracticeThe Nth FibonnaciTry It! Method 1 : (Naive Method) Simple approach is to
13 min read
Program to Find All Mersenne Primes till N
Mersenne Prime is a prime number that is one less than a power of two. In other words, any prime is Mersenne Prime if it is of the form 2k-1 where k is an integer greater than or equal to 2. First few Mersenne Primes are 3, 7, 31 and 127.The task is print all Mersenne Primes smaller than an input po
9 min read
Program to find last two digits of Nth Fibonacci number
Given a number ânâ, write a function that prints the last two digits of n-th (ânâ can also be a large number) Fibonacci number.Examples: Input : n = 65 Output : 65 Input : n = 365 Output : 65 Recommended: Please solve it on âPRACTICEâ first, before moving on to the solution. A simple solution is to
9 min read
Program to check for Twin Prime Numbers
A Twin prime are those numbers which are prime and having a difference of two ( 2 ) between the two prime numbers. In other words, a twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime
6 min read
Program to print prime numbers from 1 to N.
Given a number N, the task is to print the prime numbers from 1 to N.Examples: Input: N = 10Output: 2, 3, 5, 7Explanation : The output "2, 3, 5, 7" for input N = 10 represents the list of the prime numbers less than or equal to 10. Input: N = 5Output: 2, 3, 5 Explanation : The output "2, 3, 5" for i
15+ min read