Sunday, January 12, 2025
Google search engine
HomeData Modelling & AIProgram to find first N Fermat Numbers

Program to find first N Fermat Numbers

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




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


Java




// Java program to print fermat numbers
import java.util.*;
 
class GFG
{
 
  /* Iterative Function to calcate (x^y) in O(log y) */
  static long power(long x, long y)
  {
    long res = 1; // Initialize rest
 
    while (y > 0)
    {
 
      // If y is odd, mtiply x with the rest
      if ((y & 1) != 0)
        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
  static long Fermat(long i)
  {
    // 2 to the power i
    long power2_i = power(2, i);
 
    // 2 to the power 2^i
    long power2_2_i = power(2, power2_i);
 
    return power2_2_i + 1;
  }
 
  // Function to find first n Fermat numbers
  static void Fermat_Number(long n)
  {
 
    for (long i = 0; i < n; i++) {
 
      // Calcate 2^2^i
      System.out.print(Fermat(i));
 
      if(i!=n-1)
        System.out.print(", ");
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    long n = 7;
 
    // Function call
    Fermat_Number(n);
 
  }
}
 
// This code is contributed by phasing17


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


C#




// C# program to print fermat numbers
using System;
using System.Collections.Generic;
 
class GFG
{
 
  /* Iterative Function to calculate (x^y) in O(log y) */
  static ulong power(ulong x, ulong y)
  {
    ulong res = 1; // Initialize result
 
    while (y > 0)
    {
 
      // If y is odd, multiply x with the result
      if ((y & 1) != 0)
        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
  static ulong Fermat(ulong i)
  {
    // 2 to the power i
    ulong power2_i = power(2ul, i);
 
    // 2 to the power 2^i
    ulong power2_2_i = power(2ul, power2_i);
 
    return power2_2_i + 1;
  }
 
  // Function to find first n Fermat numbers
  static void Fermat_Number(ulong n)
  {
 
    for (ulong i = 0ul; i < n; i++) {
 
      // Calculate 2^2^i
      Console.Write(Fermat(i));
 
      if(i!=n-1)
        Console.Write(", ");
    }
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    ulong n = 7;
 
    // Function call
    Fermat_Number(n);
 
  }
}
 
// This code is contributed by phasing17


Javascript




<script>
 
// Javascript program to print fermat numbers
 
/* Iterative Function to calculate (x^y) in O(log y) */
function power(x, y)
{
    let 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
function Fermat(i)
{
    // 2 to the power i
    let power2_i = power(2, i);
 
    // 2 to the power 2^i
    let power2_2_i = power(2, power2_i);
 
    return power2_2_i + 1;
}
 
// Function to find first n Fermat numbers
function Fermat_Number(n)
{
     
    for (let i = 0; i < n; i++) {
         
        // Calculate 2^2^i
        document.write(Fermat(i));
         
        if(i!=n-1)
            document.write(", ");
    }
}
 
// Driver code
    let n = 7;
     
    // Function call
    Fermat_Number(n);
 
</script>


Output:  

3, 5, 17, 257, 65537, 4294967297, 18446744073709551617

Time Complexity: O(n * log n)

Auxiliary Space: O(1)

Reference:https://en.wikipedia.org/wiki/Fermat_number
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments