Given a number, find the numbers (smaller than or equal to n) which are both Fibonacci and prime.
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.
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
2 3 5 13
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print the nearest prime number formed by adding prime numbers to N
- Non Fibonacci Numbers
- Even Fibonacci Numbers Sum
- GCD and Fibonacci Numbers
- Sum of Fibonacci Numbers
- Find the sum of first N odd Fibonacci numbers
- Program for Fibonacci numbers
- The Magic of Fibonacci Numbers
- Program for Fibonacci numbers in PL/SQL
- Sum of squares of Fibonacci numbers
- Alternate Fibonacci Numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Find the GCD of N Fibonacci Numbers with given Indices
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.