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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR 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
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Print the nearest prime number formed by adding prime numbers to N
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Count all prime numbers in a given range whose sum of digits is also prime
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of prime numbers without odd prime digits
- Sum of Fibonacci Numbers
- GCD and Fibonacci Numbers
- Non Fibonacci Numbers
- Even Fibonacci Numbers Sum
- C Program for Fibonacci numbers
- Program for Fibonacci numbers in PL/SQL
- Alternate Fibonacci Numbers
- Sum of squares of Fibonacci numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Mithun Kumar