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++
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
#define llu int128_t
using namespace std;
llu power(llu x, llu y)
{
llu res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
llu Fermat(llu i)
{
llu power2_i = power(2, i);
llu power2_2_i = power(2, power2_i);
return power2_2_i + 1;
}
void Fermat_Number(llu n)
{
for (llu i = 0; i < n; i++) {
cout << Fermat(i);
if (i!=n-1)
cout << ", " ;
}
}
int main()
{
llu n = 7;
Fermat_Number(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long power( long x, long y)
{
long res = 1 ;
while (y > 0 )
{
if ((y & 1 ) != 0 )
res = res * x;
y = y >> 1 ;
x = x * x;
}
return res;
}
static long Fermat( long i)
{
long power2_i = power( 2 , i);
long power2_2_i = power( 2 , power2_i);
return power2_2_i + 1 ;
}
static void Fermat_Number( long n)
{
for ( long i = 0 ; i < n; i++) {
System.out.print(Fermat(i));
if (i!=n- 1 )
System.out.print( ", " );
}
}
public static void main(String[] args)
{
long n = 7 ;
Fermat_Number(n);
}
}
|
Python3
def power(x, y):
res = 1
while (y > 0 ):
if (y & 1 ):
res = res * x
y = y >> 1
x = x * x
return res
def Fermat(i):
power2_i = power( 2 , i)
power2_2_i = power( 2 , power2_i)
return power2_2_i + 1
def Fermat_Number(n):
for i in range (n):
print (Fermat(i), end = "")
if (i ! = n - 1 ):
print (end = ", " )
n = 7
Fermat_Number(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static ulong power( ulong x, ulong y)
{
ulong res = 1;
while (y > 0)
{
if ((y & 1) != 0)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
static ulong Fermat( ulong i)
{
ulong power2_i = power(2ul, i);
ulong power2_2_i = power(2ul, power2_i);
return power2_2_i + 1;
}
static void Fermat_Number( ulong n)
{
for ( ulong i = 0ul; i < n; i++) {
Console.Write(Fermat(i));
if (i!=n-1)
Console.Write( ", " );
}
}
public static void Main( string [] args)
{
ulong n = 7;
Fermat_Number(n);
}
}
|
Javascript
<script>
function power(x, y)
{
let res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
function Fermat(i)
{
let power2_i = power(2, i);
let power2_2_i = power(2, power2_i);
return power2_2_i + 1;
}
function Fermat_Number(n)
{
for (let i = 0; i < n; i++) {
document.write(Fermat(i));
if (i!=n-1)
document.write( ", " );
}
}
let n = 7;
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
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...