Fermat numbers are non-negative odd numbers which is valid for all values of k>=0. Only the first seven terms of the sequence are known till date. First, five terms of the series are prime but rest of them are not. The kth term of Fermat number is represented as
The sequence:
3, 5, 17, 257, 65537, 4294967297, 18446744073709551617
For a given N, the task is to find the first N Fermat numbers.
Examples:
Input: N = 4
Output: 3, 5, 17, 257Input: N = 7
Output : 3, 5, 17, 257, 65537, 4294967297, 18446744073709551617
Approach :
Using the above-mentioned formula we will find the Nth term of the series.
Below is the implementation of the above approach :
C++
// CPP program to print fermat numbers #include <bits/stdc++.h> #include <boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; #define llu int128_t using namespace std; /* Iterative Function to calculate (x^y) in O(log y) */llu power(llu x, llu y) { llu res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with the result if (y & 1) res = res * x; // n must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } // Function to find nth fermat number llu Fermat(llu i) { // 2 to the power i llu power2_i = power(2, i); // 2 to the power 2^i llu power2_2_i = power(2, power2_i); return power2_2_i + 1; } // Function to find first n Fermat numbers void Fermat_Number(llu n) { for (llu i = 0; i < n; i++) { // Calculate 2^2^i cout << Fermat(i); if(i!=n-1) cout << ", "; } } // Driver code int main() { llu n = 7; // Function call Fermat_Number(n); return 0; } |
Python3
# Python3 program to print fermat numbers # Iterative Function to calculate (x^y) in O(log y) def power(x, y): res = 1 # Initialize result while (y > 0): # If y is odd, # multiply x with the result if (y & 1): res = res * x # n must be even now y = y >> 1 # y = y/2 x = x * x # Change x to x^2 return res # Function to find nth fermat number def Fermat(i): # 2 to the power i power2_i = power(2, i) # 2 to the power 2^i power2_2_i = power(2, power2_i) return power2_2_i + 1 # Function to find first n Fermat numbers def Fermat_Number(n): for i in range(n): # Calculate 2^2^i print(Fermat(i), end = "") if(i != n - 1): print(end = ", ") # Driver code n = 7 # Function call Fermat_Number(n) # This code is contributed by Mohit Kumar |
output:
3, 5, 17, 257, 65537, 4294967297, 18446744073709551617
Reference:https://en.wikipedia.org/wiki/Fermat_number
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Fermat's Factorization method for large numbers
- Program to find LCM of 2 numbers without using GCD
- Program to find GCD or HCF of two numbers
- Program to find LCM of two numbers
- Program to find sum of first n natural numbers
- Program to find first N Iccanobif Numbers
- Program to find the LCM of two prime numbers
- Program to find sum of prime numbers between 1 to n
- Program to find LCM of two Fibonnaci Numbers
- C/C++ Program to find Prime Numbers between given range
- Program to find GCD of floating point numbers
- Program to find if two numbers and their AM and HM are present in an array using STL
- Program to find the common ratio of three numbers
- Program to find the percentage of difference between two numbers
- Program to find HCF (Highest Common Factor) of 2 Numbers
- Program to find the maximum difference between the index of any two different numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure
- Program to find count of numbers having odd number of divisors in given range
- Fermat's Last Theorem
- Fermat's little theorem
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

