30 lines
947 B
Text
30 lines
947 B
Text
\\ Define a function to calculate Fermat numbers
|
|
fermat(n) = 2^(2^n) + 1;
|
|
|
|
|
|
\\ Define a function to factor Fermat numbers up to a given maximum
|
|
\\ and optionally just print them without factoring
|
|
factorfermats(mymax, nofactor=0) =
|
|
{
|
|
local(fm, factors, n);
|
|
for (n = 0, mymax,
|
|
fm = fermat(n);
|
|
if (nofactor,
|
|
print("Fermat number F(", n, ") is ", fm, ".");
|
|
next;
|
|
);
|
|
factors = factorint(fm);
|
|
if (matsize(factors)[1] == 1 && factors[1,2] == 1, \\ Check if it has only one factor with exponent 1 (i.e., prime)
|
|
print("Fermat number F(", n, "), ", fm, ", is prime.");
|
|
,
|
|
print("Fermat number F(", n, "), ", fm, ", factors to ", (factors), ".");
|
|
);
|
|
);
|
|
}
|
|
|
|
{
|
|
\\ Example usage
|
|
factorfermats(9, 1); \\ Print Fermat numbers without factoring
|
|
print(""); \\ Just to add a visual separator in the output
|
|
factorfermats(10); \\ Factor Fermat numbers
|
|
}
|