Fermat numbers: Difference between revisions

Content added Content deleted
m (Minor performance improvement)
m (Performance improvement)
Line 123: Line 123:
{{trans|Java}}
{{trans|Java}}
{{libheader|Boost}}
{{libheader|Boost}}
Built and tested on macOS 10.15.2, CPU: 3.2 GHz Intel Core i5.
Built and tested on macOS 10.15, CPU: 3.2 GHz Intel Core i5.
Execution time is about 16 minutes.
Execution time is about 12 minutes.
<lang cpp>#include <iostream>
<lang cpp>#include <iostream>
#include <vector>
#include <vector>
Line 140: Line 140:
}
}


inline integer g(const integer& x, const integer& n) {
inline void g(integer& x, const integer& n) {
return (x * x + 1) % n;
x *= x;
x += 1;
x %= n;
}
}


Line 148: Line 150:
int count = 0;
int count = 0;
for (;;) {
for (;;) {
x = g(x, n);
g(x, n);
y = g(g(y, n), n);
g(y, n);
g(y, n);
d = abs(x - y);
d = abs(x - y);
z = (z * d) % n;
z = (z * d) % n;