forked from Twiggecode/Integer-Sequences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fermat_numbers.cpp
60 lines (47 loc) · 1.1 KB
/
fermat_numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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;
}