Related Articles

Related Articles

Program to find first N Fermat Numbers
  • Difficulty Level : Easy
  • Last Updated : 21 Aug, 2019

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, 257

Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right



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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :