Fermat numbers: Difference between revisions

m (→‎factoring by trial division: changed a comment.)
(36 intermediate revisions by 19 users not shown)
Line 1:
{{task|Prime Numbers}}
 
In mathematics, a Fermat number, ''named after Pierre de Fermat who first studied them,'' is a positive integer of the form <big>'''F<sub>n</sub> = 2<sup>''2''<sup>''n''</sup></sup> + 1'''</big> where '''n''' is a non-negative integer.
Line 25:
<br>
 
=={{header|ArturoALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT which has programmer specifiable precision.
{{libheader|ALGOL 68-primes}}
The source of primes.incl.a68 is on another page on Rosetta Code, see the above link.
<syntaxhighlight lang="algol68">
BEGIN # find and factorise some Fermat numbers: F(n) = 2^(2^n) + 1 #
 
PR read "primes.incl.a68" PR # include prime utilities #
<lang arturo>nPowers: #(1 2 4 8 16 32 64 128 256 512)
PR precision 256 PR # set the precision of LONG LONG INT #
fermatSet: map 0..9 -> 2^nPowers.[&]+1
 
PROC gcd = ( LONG LONG INT x, y )LONG LONG INT: # iterative gcd #
loop 0..9 {
print "F(" + & + ") = " + fermatSet.[&]BEGIN
LONG LONG INT a := x, b := y;
}
WHILE b /= 0 DO
LONG LONG INT next a = b;
b := a MOD b;
a := next a
OD;
ABS a
END # gcd # ;
 
# returns a prime factor (if possible) of n, if n is prime, n is returned #
print ""
PROC pollard rho = ( LONG LONG INT n )LONG LONG INT:
IF is probably prime( n )
THEN n
ELIF LONG LONG INT x := 2, y := 2, d := 1;
PROC g = ( LONG LONG INT x )LONG LONG INT: ( ( x * x ) + 1 ) MOD n;
WHILE d = 1 DO
x := g( x );
y := g( g( y ) );
d := gcd( ABS( x - y ), n )
OD;
d = n
THEN print( ( "pollard rho found non probably prime n for: ", n, newline ) );
n
ELIF LONG LONG INT other d = n OVER d;
d > other d
THEN other d
ELSE d
FI # pollard rho # ;
 
# returns the lowest prime factor of n, or n if n is prime #
loop 0..9 {
PROC prime factor = ( LONG LONG INT n )LONG LONG INT:
print "prime factors of F(" + & + ") = " + [primeFactors fermatSet.[&]]
IF LONG LONG INT d := pollard rho( n );
}</lang>
d = n
THEN d
ELSE # check for a lower factor #
LONG LONG INT other d := n OVER d;
LONG LONG INT d1 := pollard rho( other d );
WHILE d1 < d DO
d := d1;
other d := other d OVER d;
d1 := pollard rho( other d )
OD;
IF d1 < d THEN d1 ELSE d FI
FI # prime factor # ;
 
# task #
INT p2 := 1;
FOR i FROM 0 TO 9 DO
LONG LONG INT fn = 1 + ( LONG LONG 2 )^p2;
print( ( "F(", whole( i, 0 ), "): ", whole( fn, 0 ) ) );
IF i < 7 THEN
print( ( ", " ) );
LONG LONG INT pf = prime factor( fn );
IF pf = fn THEN
print( ( "prime" ) )
ELSE
print( ( whole( pf, 0 ), " x ", whole( fn OVER pf, 0 ) ) )
FI
FI;
print( ( newline ) );
p2 *:= 2
OD
 
END
</syntaxhighlight>
{{out}}
<pre>
F(0): 3, prime
F(1): 5, prime
F(2): 17, prime
F(3): 257, prime
F(4): 65537, prime
F(5): 4294967297, 641 x 6700417
F(6): 18446744073709551617, 274177 x 67280421310721
F(7): 340282366920938463463374607431768211457
F(8): 115792089237316195423570985008687907853269984665640564039457584007913129639937
F(9): 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
</pre>
 
=={{header|Arturo}}==
<pre>F(0) = 3
<syntaxhighlight lang="rebol">nPowers: [1 2 4 8 16 32 64 128 256 512]
F(1) = 5
fermatSet: map 0..9 'x -> 1 + 2 ^ nPowers\[x]
F(2) = 17
F(3) = 257
loop 0..9 'i ->
F(4) = 65537
print ["F(" i ") =" fermatSet\[i]]
F(5) = 4294967297
 
F(6) = 18446744073709551617
print ""
F(7) = 340282366920938463463374607431768211457
F(8) = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F(9) = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
 
loop 0..9 'i ->
prime factors of F(0) = #(3)
prime print ["Prime factors of F(1" i ") =" #(5)factors.prime fermatSet\[i]]</syntaxhighlight>
prime factors of F(2) = #(17)
prime factors of F(3) = #(257)
prime factors of F(4) = #(65537)
prime factors of F(5) = #(641 6700417)
prime factors of F(6) = #(274177 67280421310721)
prime factors of F(7) = #(59649589127497217 5704689200685129054721)
prime factors of F(8) = #(1238926361552897 93461639715357977769163558199606896584051237541638188580280321)
prime factors of F(9) = #(2424833)</pre>
 
=={{header|C}}==
Compile with : <pre>gcc -o fermat fermat.c -lgmp</pre>
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
Line 106 ⟶ 171:
 
return 0;
}</langsyntaxhighlight>
<pre>
F(0) = 3 -> PRIME
Line 125 ⟶ 190:
Built and tested on macOS 10.15, CPU: 3.2 GHz Intel Core i5.
Execution time is about 12 minutes.
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <boost/integer/common_factor.hpp>
Line 207 ⟶ 272:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 235 ⟶ 300:
</pre>
 
 
=={{header|Crystal|}}==
 
=={{header|Common Lisp}}==
{{trans|Lisp}}
This uses the 'factor' function defined in the page Prime Decomposition
http://rosettacode.org/wiki/Prime_decomposition#Common_Lisp
<syntaxhighlight lang="lisp">
(defun fermat-number (n)
"Return the n-th Fermat number"
(1+ (expt 2 (expt 2 n))) )
 
 
(defun factor (n &optional (acc '()))
"Return the list of factors of n"
(when (> n 1) (loop with max-d = (isqrt n)
for d = 2 then (if (evenp d) (1+ d) (+ d 2)) do
(cond ((> d max-d) (return (cons (list n 1) acc)))
((zerop (rem n d))
(return (factor (truncate n d) (if (eq d (caar acc))
(cons
(list (caar acc) (1+ (cadar acc)))
(cdr acc))
(cons (list d 1) acc)))))))))
</syntaxhighlight>
 
{{out}}
<pre>
(dotimes (i 8) (format t "~d: ~d = ~d~%" i (fermat-number i) (factors (fermat-number i))))
0: 3 = (3)
1: 5 = (5)
2: 17 = (17)
3: 257 = (257)
4: 65537 = (65537)
5: 4294967297 = (641 6700417)
6: 18446744073709551617 = (274177 (67280421310721))
7: 340282366920938463463374607431768211457 = ((340282366920938463463374607431768211457))
8: 115792089237316195423570985008687907853269984665640564039457584007913129639937 = ((115792089237316195423570985008687907853269984665640564039457584007913129639937))
9: 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097 = ((13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097))
10: 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137217 = ((179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137217))
11: 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230657 = (319489
(101152171346465785365739905563790778275446424351747584524444802254614885454171789617787158279386499891040749324458425859713854183244152133860909616251101863039512713637405344446131784663602352840930541077733717180487566766438342966931062084574042206368862921265008513729385286790910065162204496552694867070609361616173540692858549041708993328392702647150062512506151403207406283761595736673720405375033810606080158013948717662760215065784116654734290374983906448207065425365852408720566771005345821995341556493590254118091846659097349200248570452085641250044738949182704974520704370036542579394575574913724915713))
12: 1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190337 = (114689
(9106268965752186405773463110818163752233991481723476361152625650968740750826648212547208641935996986118024454955917854702609434541985662158212523327009262247869952450049350838706079834460006786304075107567909269645531121898331250125751682239313156601738683820643686003638396435055834553570682260579462973839574318172464558815116581626749391315641251152532705571615644886981829338611134458123396450764186936496833100701185274214915961723337127995182593580031119299575446791424418154036863609858251201843852076223383379133238000289598800458955855329052103961332983048473420515918928565951506637819342706575976725030506905683310915700945442329953941604008255667676914945655757474715779252371155778495946746587469464160684843488975918662295274965457887082037460184558511575570318625886351712499453155527762335682281851520733417380809781252979478377941937578568481859702438295520231435016188495646093490407803983345420364088331996467459309353537828143080691834120737157445502646809195267166779721413577366833939771467773331873590129210913628329073978766992198221682739812652450408607796042492802295258713711959073218748776359806123717024800451461326745599716651128725627280643537507664130920416107218492950792456907321580171946770433))</pre>
 
 
 
=={{header|Crystal}}==
{{trans|Ruby}}
This uses the `factor` function from the `coreutils` library
Line 241 ⟶ 352:
https://www.gnu.org/software/coreutils/
https://en.wikipedia.org/wiki/GNU_Core_Utilities
<langsyntaxhighlight lang="ruby">require "big"
 
def factors(n)
Line 254 ⟶ 365:
puts
puts "Factors for each Fermat Number F0 .. F8."
(0..8).each { |n| puts "F#{n} = #{factors fermat(n)}" }</langsyntaxhighlight>
 
System: Lenovo V570 (2011), I5-2410M, 2.9 GHz, Crystal 0.34
Line 286 ⟶ 397:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel lists lists.lazy math math.functions
math.primes.factors sequences ;
 
Line 300 ⟶ 411:
"Factors of F%c: %[%d, %]%s\n" printf 1 +
] each drop
] 2bi</langsyntaxhighlight>
{{out}}
<pre>
Line 336 ⟶ 447:
 
The timings are for my Intel Core i7-8565U laptop using Go 1.14.1 on Ubuntu 18.04.
<langsyntaxhighlight lang="go">package main
 
import (
Line 696 ⟶ 807:
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 728 ⟶ 839:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes (primeFactors)
import Data.Bool (bool)
 
fermat :: Integer -> Integer
fermat = succ . (2 ^) . (2 ^)
 
fermats :: [Integer]
fermats = fermat <$> [0 ..]
 
--------------------------- TEST ---------------------------
---------------------------TEST----------------------------
main :: IO ()
main =
Line 748 ⟶ 857:
"Factors of first 7:"
show
showFactors
((bool "(prime)" . show) <*> ((1 <) . length))
primeFactors
(take 7 fermats)
]
 
-------------------------- DISPLAY --------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
Line 760 ⟶ 869:
where
rjust n c = drop . length <*> (replicate n c ++)
w = maximum (length . xShow <$> xs)</lang>
 
showFactors :: [Integer] -> String
showFactors x
| 1 < length x = show x
| otherwise = "(prime)"</syntaxhighlight>
{{Out}}
<pre>First 10 Fermats:
Line 817 ⟶ 931:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.ArrayList;
Line 947 ⟶ 1,061:
 
}
</syntaxhighlight>
</lang>
Output includes composite numbers attempted to factor with Pollard rho.
{{out}}
Line 990 ⟶ 1,104:
F[12] = 114689 * 26017793 * 63766529 * (C1213)
</pre>
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
 
'''Preliminaries'''
<syntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
def gcd(a; b):
# subfunction expects [a,b] as input
# i.e. a ~ .[0] and b ~ .[1]
def rgcd: if .[1] == 0 then .[0]
else [.[1], .[0] % .[1]] | rgcd
end;
[a,b] | rgcd;
 
# This is fast because the state of `until` is just a number
def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
elif ($n % 23 == 0) then $n == 23
elif ($n % 29 == 0) then $n == 29
elif ($n % 31 == 0) then $n == 31
elif ($n % 37 == 0) then $n == 37
elif ($n % 41 == 0) then $n == 41
else 43
| until( (. * .) > $n or ($n % . == 0); . + 2)
| . * . > $n
end;</syntaxhighlight>
'''Fermat Numbers'''
<syntaxhighlight lang="jq">def fermat:
. as $n
| (2 | power( 2 | power($n))) + 1;
 
# https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm
def pollardRho($x):
. as $n
| def g: (.*. + 1) % $n ;
{x:$x, y:$x, d:1}
| until(.d != 1;
.x |= g
| .y |= (g|g)
| .d = gcd((.x - .y)|length; $n) )
| if .d == $n then 0
else .d
end ;
 
def rhoPrimeFactors:
. as $n
| pollardRho(2)
| if . == 0
then [$n, 1]
else [., ($n / .)]
end ;
"The first 10 Fermat numbers are:",
[ range(0;10) | fermat ] as $fns
| (range(0;10) | "F\(.) is \($fns[.])"),
("\nFactors of the first 7 Fermat numbers:",
range(0;7) as $i
| $fns[$i]
| rhoPrimeFactors as $factors
| if $factors[1] == 1
then "F\($i) : rho-prime", " ... => \(if is_prime then "prime" else "not" end)"
else "F\($i) => \($factors)"
end )</syntaxhighlight>
{{out}}
<pre>
The first 10 Fermat numbers are:
F0 is 3
F1 is 5
F2 is 17
F3 is 257
F4 is 65537
F5 is 4294967297
F6 is 18446744073709551617
F7 is 340282366920938463463374607431768211457
F8 is 115792089237316195423570985008687907853269984665640564039457584007913129639937
F9 is 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
 
Factors of the first 7 Fermat numbers:
F0 : rho-prime
... => prime
F1 : rho-prime
... => prime
F2 : rho-prime
... => prime
F3 : rho-prime
... => prime
F4 : rho-prime
... => prime
F5 => [641,6700417]
F6 => [274177,67280421310721]
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
fermat(n) = BigInt(2)^(BigInt(2)^n) + 1
Line 1,012 ⟶ 1,230:
factorfermats(9, true)
factorfermats(10)
</langsyntaxhighlight>{{out}}
<pre>
Fermat number F(0) is 3.
Line 1,036 ⟶ 1,254:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
import kotlin.math.pow
 
Line 1,157 ⟶ 1,375:
private fun pollardRhoG(x: BigInteger, n: BigInteger): BigInteger {
return (x * x + BigInteger.ONE).mod(n)
}</langsyntaxhighlight>
 
=={{header|langur}}==
{{trans|Python}}
<syntaxhighlight lang="langur">val .fermat = fn(.i) { 2 ^ 2 ^ .i + 1 }
{{works with|langur|0.10}}
<lang langur>val .fermat = f 2 ^ 2 ^ .n + 1
 
val .factors = ffn(var .x) {
for[.f=[]] .i, .s = 2, truncatetrunc .x ^/ 2; .i < .s; .i += 1 {
if .x div .i {
.f ~= [.i]
.x \= .i
.s = truncatetrunc .x ^/ 2
}
} ~ [.x]
Line 1,190 ⟶ 1,407:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
I just ran an initial test. Maybe I'll take the time to calculate more factors later.
<pre>first 10 Fermat numbers
F₀ = 3
Line 1,214 ⟶ 1,430:
F₅ factors: [641, 6700417]
F₆ factors: [274177, 67280421310721]
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Fermat]
Fermat[n_] := 2^(2^n) + 1
Fermat /@ Range[0, 9]
Scan[FactorInteger /* Print, %]</syntaxhighlight>
{{out}}
<pre>{3,5,17,257,65537,4294967297,18446744073709551617,340282366920938463463374607431768211457,115792089237316195423570985008687907853269984665640564039457584007913129639937,13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097}
{{3,1}}
{{5,1}}
{{17,1}}
{{257,1}}
{{65537,1}}
{{641,1},{6700417,1}}
{{274177,1},{67280421310721,1}}
{{59649589127497217,1},{5704689200685129054721,1}}
{{1238926361552897,1},{93461639715357977769163558199606896584051237541638188580280321,1}}
$Aborted</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
{{libheader|bignum}}
<syntaxhighlight lang="nim">import math
import bignum
import strformat
import strutils
import tables
import times
 
const Composite = {9: "5529", 10: "6078", 11: "1037", 12: "5488", 13: "2884"}.toTable
 
const Subscripts = ["₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉"]
 
let One = newInt(1)
 
#---------------------------------------------------------------------------------------------------
 
func fermat(n: int): Int {.inline.} = 2^(culong(2^n)) + 1
 
#---------------------------------------------------------------------------------------------------
 
template isProbablyPrime(n: Int): bool = n.probablyPrime(25) != 0
 
#---------------------------------------------------------------------------------------------------
 
func pollardRhoG(x, n: Int): Int {.inline.} = (x * x + 1) mod n
 
#---------------------------------------------------------------------------------------------------
 
proc pollardRhoFast(n: Int): Int =
 
let start = getTime()
var
x = newInt(2)
y = newInt(2)
count = 0
z = One
 
while true:
x = pollardRhoG(x, n)
y = pollardRhoG(pollardRhoG(y, n), n)
result = abs(x - y)
z = z * result mod n
inc count
if count == 100:
result = gcd(z, n)
if result != One: break
z = One
count = 0
 
let duration = (getTime() - start).inMilliseconds
echo fmt" Pollard rho try factor {n} elapsed time = {duration} ms (factor = {result})."
if result == n:
result = newInt(0)
 
#---------------------------------------------------------------------------------------------------
 
proc factors(fermatIndex: int; n: Int): seq[Int] =
 
var n = n
var factor: Int
while true:
 
if n.isProbablyPrime():
result.add(n)
break
 
if fermatIndex in Composite:
let stop = Composite[fermatIndex]
let s = $n
if s.startsWith(stop):
result.add(newInt(-s.len))
break
 
factor = pollardRhoFast(n)
if factor.isZero():
result.add(n)
break
result.add(factor)
n = n div factor
 
#---------------------------------------------------------------------------------------------------
 
func `$`(factors: seq[Int]): string =
 
if factors.len == 1:
result = fmt"{factors[0]} (PRIME)"
 
else:
result = $factors[0]
let start = result.high
for factor in factors[1..^1]:
result.addSep(" * ", start)
result.add(if factor < 0: fmt"(C{-factor})" else: $factor)
 
#---------------------------------------------------------------------------------------------------
 
func subscript(n: Natural): string =
var n = n
while true:
result.insert(Subscripts[n mod 10], 0)
n = n div 10
if n == 0: break
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
echo "First 10 Fermat numbers:"
for i in 0..9:
echo fmt"F{subscript(i)} = {fermat(i)}"
 
echo ""
echo "First 12 Fermat numbers factored:"
for i in 0..12:
echo fmt"F{subscript(i)} = {factors(i, fermat(i))}"</syntaxhighlight>
 
{{out}}
<pre>First 10 Fermat numbers:
F₀ = 3
F₁ = 5
F₂ = 17
F₃ = 257
F₄ = 65537
F₅ = 4294967297
F₆ = 18446744073709551617
F₇ = 340282366920938463463374607431768211457
F₈ = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F₉ = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
 
First 12 Fermat numbers factored:
F₀ = 3 (PRIME)
F₁ = 5 (PRIME)
F₂ = 17 (PRIME)
F₃ = 257 (PRIME)
F₄ = 65537 (PRIME)
Pollard rho try factor 4294967297 elapsed time = 0 ms (factor = 641).
F₅ = 641 * 6700417
Pollard rho try factor 18446744073709551617 elapsed time = 1 ms (factor = 274177).
F₆ = 274177 * 67280421310721
Pollard rho try factor 340282366920938463463374607431768211457 elapsed time = 516705 ms (factor = 59649589127497217).
F₇ = 59649589127497217 * 5704689200685129054721
Pollard rho try factor 115792089237316195423570985008687907853269984665640564039457584007913129639937 elapsed time = 20765 ms (factor = 1238926361552897).
F₈ = 1238926361552897 * 93461639715357977769163558199606896584051237541638188580280321
Pollard rho try factor 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097 elapsed time = 3 ms (factor = 2424833).
F₉ = 2424833 * (C148)
Pollard rho try factor 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137217 elapsed time = 46 ms (factor = 45592577).
Pollard rho try factor 3942951359960012586542991835686376608231592127249807732373409846031135195659174148737161255930050543559319182152642816343958573976075461198274610155058226350701077796608546283231637018483208223116080561800334422176622099740983337736621316898600121619871377542107047343253864459964167331555646795960321 elapsed time = 595 ms (factor = 6487031809).
F₁₀ = 45592577 * 6487031809 * (C291)
Pollard rho try factor 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230657 elapsed time = 1 ms (factor = 974849).
Pollard rho try factor 33150781373639412155846573868024639672856106606987835072026893834352453701925006737655987144186344206834820532125383540932102878651453631377873037143648178457002958783669056532601662155256508553423204658756451069116132055982639112479817996775373591674794399801442382402697828988429044712163168243619196804348072710121945390948428910309765481110260333687910970886853046635254307274981520537180895290310783635953818082306553996934497908037349010876970379631341148456045116407475229712217130141926525362871253437794629422541384355185626695660779797862427347553871011957167960991543632376506466281643163047416635393 elapsed time = 11 ms (factor = 319489).
F₁₁ = 974849 * 319489 * (C606)
Pollard rho try factor 1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190337 elapsed time = 11 ms (factor = 114689).
Pollard rho try factor 9106268965752186405773463110818163752233991481723476361152625650968740750826648212547208641935996986118024454955917854702609434541985662158212523327009262247869952450049350838706079834460006786304075107567909269645531121898331250125751682239313156601738683820643686003638396435055834553570682260579462973839574318172464558815116581626749391315641251152532705571615644886981829338611134458123396450764186936496833100701185274214915961723337127995182593580031119299575446791424418154036863609858251201843852076223383379133238000289598800458955855329052103961332983048473420515918928565951506637819342706575976725030506905683310915700945442329953941604008255667676914945655757474715779252371155778495946746587469464160684843488975918662295274965457887082037460184558511575570318625886351712499453155527762335682281851520733417380809781252979478377941937578568481859702438295520231435016188495646093490407803983345420364088331996467459309353537828143080691834120737157445502646809195267166779721413577366833939771467773331873590129210913628329073978766992198221682739812652450408607796042492802295258713711959073218748776359806123717024800451461326745599716651128725627280643537507664130920416107218492950792456907321580171946770433 elapsed time = 18 ms (factor = 26017793).
Pollard rho try factor 350001591824186871106763863899530746217943677302816436473017740242946077356624684213115564488348300185108877411543625345263121839042445381828217455916005721464151569276047005167043946981206545317048033534893189043572263100806868980998952610596646556521480658280419327021257968923645235918768446677220584153297488306270426504473941414890547838382804073832577020334339845312084285496895699728389585187497914849919557000902623608963141559444997044721763816786117073787751589515083702681348245404913906488680729999788351730419178916889637812821243344085799435845038164784900107242721493170135785069061880328434342030106354742821995535937481028077744396075726164309052460585559946407282864168038994640934638329525854255227752926198464207255983432778402344965903148839661825873175277621985527846249416909718758069997783882773355041329208102046913755441975327368023946523920699020098723785533557579080342841062805878477869513695185309048285123705067072486920463781103076554014502567884803571416673251784936825115787932810954867447447568320403976197134736485611912650805539603318790667901618038578533362100071745480995207732506742832634459994375828162163700807237997808869771569154136465922798310222055287047244647419069003284481 elapsed time = 114 ms (factor = 63766529).
F₁₂ = 114689 * 26017793 * 63766529 * (C1213)</pre>
 
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
\\ 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
}
</syntaxhighlight>
{{out}}
<pre>
Fermat number F(0) is 3.
Fermat number F(1) is 5.
Fermat number F(2) is 17.
Fermat number F(3) is 257.
Fermat number F(4) is 65537.
Fermat number F(5) is 4294967297.
Fermat number F(6) is 18446744073709551617.
Fermat number F(7) is 340282366920938463463374607431768211457.
Fermat number F(8) is 115792089237316195423570985008687907853269984665640564039457584007913129639937.
Fermat number F(9) is 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097.
 
Fermat number F(0), 3, is prime.
Fermat number F(1), 5, is prime.
Fermat number F(2), 17, is prime.
Fermat number F(3), 257, is prime.
Fermat number F(4), 65537, is prime.
Fermat number F(5), 4294967297, factors to [641, 1; 6700417, 1].
Fermat number F(6), 18446744073709551617, factors to [274177, 1; 67280421310721, 1].
Fermat number F(7), 340282366920938463463374607431768211457, factors to [59649589127497217, 1; 5704689200685129054721, 1].
Fermat number F(8), 115792089237316195423570985008687907853269984665640564039457584007913129639937, factors to [1238926361552897, 1; 93461639715357977769163558199606896584051237541638188580280321, 1].
 
</pre>
 
Line 1,219 ⟶ 1,669:
{{libheader|ntheory}}
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,235 ⟶ 1,685:
for my $f (map { [factor($_)] } @Fermats[0..8]) {
printf "Factors of F%s: %s\n", $sub++, @$f == 1 ? 'prime' : join ' ', @$f
}</langsyntaxhighlight>
{{out}}
<pre>First 10 Fermat numbers:
Line 1,262 ⟶ 1,712:
=={{header|Phix}}==
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- demo\rosetta\Fermat.exw
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
include mpfr.e
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Fermat.exw</span>
 
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
procedure fermat(mpz res, integer n)
integer pn = power(2,n)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">fermat</span><span style="color: #0000FF;">(</span><span style="color: #004080;">mpz</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
mpz_ui_pow_ui(res,2,pn)
<span style="color: #004080;">integer</span> <span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
mpz_add_si(res,res,1)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #7060A8;">mpz_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
mpz fn = mpz_init()
for i=0 to 29 do -- (see note)
<span style="color: #004080;">mpz</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
fermat(fn,i)
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">18</span><span style="color: #0000FF;">:</span><span style="color: #000000;">29</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (see note)</span>
if i<=20 then
<span style="color: #000000;">print_lim</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">16</span><span style="color: #0000FF;">:</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
printf(1,"F%d = %s\n",{i,shorten(mpz_get_str(fn))})
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
else -- (since printing it takes too long...)
<span style="color: #000000;">fermat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"F%d has %,d digits\n",{i,mpz_sizeinbase(fn,10)})
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">print_lim</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"F%d = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">))})</span>
end for
<span style="color: #008080;">else</span> <span style="color: #000080;font-style:italic;">-- (since printing it takes too long...)</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"F%d has %,d digits\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_sizeinbase</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span>
printf(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
randstate state = gmp_randinit_mt()
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=0 to 13 do
atom t = time()
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
fermat(fn,i)
<span style="color: #008080;">constant</span> <span style="color: #000000;">flimit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">11</span><span style="color: #0000FF;">:</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)</span>
sequence f = mpz_prime_factors(fn, 200000)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">flimit</span> <span style="color: #008080;">do</span>
t = time()-t
<span style="color: #004080;">atom</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
string fs = "",
<span style="color: #000000;">fermat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
ts = elapsed(t)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">200000</span><span style="color: #0000FF;">)</span>
if length(f[$])=1 then -- (as per docs)
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t</span>
mpz_set_str(fn,f[$][1])
<span style="color: #004080;">string</span> <span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">,</span>
if not mpz_probable_prime_p(fn, state) then
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
if length(f)=1 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[$])=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (as per docs)</span>
fs = " (not prime)"
<span style="color: #7060A8;">mpz_set_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[$][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
else
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
fs = " (last factor is not prime)"
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" (not prime)"</span>
end if
<span style="color: #008080;">else</span>
f[$][1] = shorten(f[$][1])
<span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" (last factor is not prime)"</span>
elsif length(f)=1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
and mpz_probable_prime_p(fn, state) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
fs = " (prime)"
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">f</span><span style="color: #0000FF;">[$][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[$][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
fs = mpz_factorstring(f)&fs
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span>
printf(1,"Factors of F%d: %s [%s]\n",{i,fs,ts})
<span style="color: #008080;">and</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end for</lang>
<span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" (prime)"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_factorstring</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">fs</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Factors of F%d: %s [%s]\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ts</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
Note that mpz_prime_factors(), a phix-specific extension to gmp, is designed to find small factors quickly and
Line 1,363 ⟶ 1,819:
Factors of F12: 114689*910626896...<1,228 digits>...946770433 (last factor is not prime) [0.6s]
Factors of F13: 109074813...<2,467 digits>...715792897 (not prime) [1.3s]
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(seed (in "/dev/urandom" (rd 8)))
(de **Mod (X Y N)
(let M 1
(loop
(when (bit? 1 Y)
(setq M (% (* M X) N)) )
(T (=0 (setq Y (>> 1 Y)))
M )
(setq X (% (* X X) N)) ) ) )
(de isprime (N)
(cache '(NIL) N
(if (== N 2)
T
(and
(> N 1)
(bit? 1 N)
(let (Q (dec N) N1 (dec N) K 0 X)
(until (bit? 1 Q)
(setq
Q (>> 1 Q)
K (inc K) ) )
(catch 'composite
(do 16
(loop
(setq X
(**Mod
(rand 2 (min (dec N) 1000000000000))
Q
N ) )
(T (or (=1 X) (= X N1)))
(T
(do K
(setq X (**Mod X 2 N))
(when (=1 X) (throw 'composite))
(T (= X N1) T) ) )
(throw 'composite) ) )
(throw 'composite T) ) ) ) ) ) )
(de gcd (A B)
(until (=0 B)
(let M (% A B)
(setq A B B M) ) )
(abs A) )
(de g (A)
(% (+ (% (* A A) N) C) N) )
(de pollard-brent (N)
(let
(A (dec N)
Y (rand 1 (min A 1000000000000000000))
C (rand 1 (min A 1000000000000000000))
M (rand 1 (min A 1000000000000000000))
G 1
R 1
Q 1 )
(ifn (bit? 1 N)
2
(loop
(NIL (=1 G))
(setq X Y)
(do R
(setq Y (g Y)) )
(zero K)
(loop
(NIL (and (> R K) (=1 G)))
(setq YS Y)
(do (min M (- R K))
(setq
Y (g Y)
Q (% (* Q (abs (- X Y))) N) ) )
(setq
G (gcd Q N)
K (+ K M) )
)
(setq R (* R 2)) )
(when (== G N)
(loop
(NIL (> G 1))
(setq
YS (g YS)
G (gcd (abs (- X YS)) N) ) ) )
(if (== G N)
NIL
G ) ) ) )
(de factors (N)
(sort
(make
(loop
(setq N (/ N (link (pollard-brent N))))
(T (isprime N)) )
(link N) ) ) )
(de fermat (N)
(inc (** 2 (** 2 N))) )
(for (N 0 (>= 8 N) (inc N))
(println N ': (fermat N)) )
(prinl)
(for (N 0 (>= 8 N) (inc N))
(let N (fermat N)
(println
N
':
(if (isprime N) 'PRIME (factors N)) ) ) )</syntaxhighlight>
{{out}}
<pre>
$ pil VU.l
0 : 3
1 : 5
2 : 17
3 : 257
4 : 65537
5 : 4294967297
6 : 18446744073709551617
7 : 340282366920938463463374607431768211457
8 : 115792089237316195423570985008687907853269984665640564039457584007913129639937
 
3 : PRIME
5 : PRIME
17 : PRIME
257 : PRIME
65537 : PRIME
4294967297 : (641 6700417)
18446744073709551617 : (274177 67280421310721)
340282366920938463463374607431768211457 : (59649589127497217 5704689200685129054721)
115792089237316195423570985008687907853269984665640564039457584007913129639937 : (1238926361552897 93461639715357977769163558199606896584051237541638188580280321)
</pre>
 
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">def factors(x):
factors = []
i = 2
Line 1,392 ⟶ 1,973:
print("F{} -> IS PRIME".format(chr(i + 0x2080)))
else:
print("F{} -> FACTORS: {}".format(chr(i + 0x2080), fac))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,422 ⟶ 2,003:
===Functional===
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Fermat numbers'''
 
from itertools import count, islice
Line 1,558 ⟶ 2,139:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First ten Fermat numbers:
Line 1,583 ⟶ 2,164:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.07.1}}
I gave up on factoring F₉ after about 20 minutes.
 
I gave up on factoring F₉ after about 20 minutes.
<lang perl6>use ntheory:from<Perl5> <factor>;
{{libheader|ntheory}}
<syntaxhighlight lang="raku" line>use ntheory:from<Perl5> <factor>;
 
my @Fermats = (^Inf).map: 2 ** 2 ** * + 1;
Line 1,598 ⟶ 2,179:
for @Fermats[^9].map( {"$_".&factor} ) -> $f {
printf "Factors of F%s: %s %s\n", $sub++, $f.join(' '), $f.elems == 1 ?? '- prime' !! ''
}</langsyntaxhighlight>
{{out}}
<pre>First 10 Fermat numbers:
Line 1,626 ⟶ 2,207:
=={{header|REXX}}==
=== factoring by trial division ===
<langsyntaxhighlight lang="rexx">/*REXX program to find and display Fermat numbers, and show factors of Fermat numbers.*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 9 /*Not specified? Then use the default.*/
Line 1,658 ⟶ 2,239:
return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
build: do while z//j==0; z= z % j; ?= ? j; end; return strip(?)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,696 ⟶ 2,277:
 
=== factoring via Pollard's rho algorithm ===
<langsyntaxhighlight lang="rexx">/*REXX program to find and display Fermat numbers, and show factors of Fermat numbers.*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 9 /*Not specified? Then use the default.*/
numeric digits 200 /*ensure enough decimal digits, for n=9*/
 
do j=0 to n; f= 2** (2**j) + 1 /*calculate a series of Fermat numbers.*/
say right('F'j, length(n) + 1)': ' f /*display a particular " " */
end /*j*/
say
do k=5 to n; f= 2** (2**k) + 1; say /*calculate a series of Fermat numbers.*/
say center(' F'k": " f' ', 79, "═") /*display a particular " " */
a= rho(f) /*factor a Fermat number, given time. */
b= f % a
if a==b then say f ' is prime.'
else say 'factors: ' commas(a) " " commas(b)
end /*k*/
exit 0 /*stick a fork in it, we're all done. */
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do ?=length(_)-3 to 1 by -3; _=insert(',', _, ?); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
rho: procedure; parse arg n; y= 2; d= 1 /*initialize X, Y, and D variables.*/
do x=2 until d==n /*try rho method with X=2 for 1st time.*/
do while d==1
x= (x*x + 1) // n
v= (y*y + 1) // n
Line 1,730 ⟶ 2,311:
d= xy /*assign variable D with a new XY */
end /*while*/
end /*x*/
if d==n then iterate /*Current X failure; bump X; try again.*/
return d /*found a factor of N. Return it.*/</syntaxhighlight>
end /*x*/</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,757 ⟶ 2,337:
</pre>
 
=={{header|Ruby|Ring}}==
<syntaxhighlight lang="ring">
decimals(0)
load "stdlib.ring"
 
see "working..." + nl
see "The first 10 Fermat numbers are:" + nl
 
num = 0
limit = 9
 
for n = 0 to limit
fermat = pow(2,pow(2,n)) + 1
mod = fermat%2
if n > 5
ferm = string(fermat)
tmp = number(right(ferm,1))+1
fermat = left(ferm,len(ferm)-1) + string(tmp)
ok
see "F(" + n + ") = " + fermat + nl
next
 
see "done..." + nl
</syntaxhighlight>
Output:
<pre>
working...
The first 10 Fermat numbers are:
F(0) = 3
F(1) = 5
F(2) = 17
F(3) = 257
F(4) = 65537
F(5) = 4294967297
F(6) = 18446744073709551617
F(7) = 340282366920938463463374607431768211457
F(8) = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F(9) = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
</pre>
 
=={{header|Ruby}}==
This uses the `factor` function from the `coreutils` library
that comes standard with most GNU/Linux, BSD, and Unix systems.
https://www.gnu.org/software/coreutils/
https://en.wikipedia.org/wiki/GNU_Core_Utilities
<langsyntaxhighlight lang="ruby">def factors(n)
factors = `factor #{n}`.split(' ')[1..-1].map(&:to_i)
factors.group_by { _1 }.map { |prime, exp| [prime, exp.size] } # Ruby 2.7 or later
Line 1,774 ⟶ 2,394:
puts
puts "Factors for each Fermat Number F0 .. F8."
(0..8).each { |n| puts "F#{n} = #{factors fermat(n)}" }</langsyntaxhighlight>
 
System: Lenovo V570 (2011), I5-2410M, 2.9 GHz, Ruby 2.7.1
Line 1,805 ⟶ 2,425:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust"> struct DivisorGen {
curr: u64,
last: u64,
Line 1,861 ⟶ 2,481:
println!("{} is{}prime", f, not_or_not);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,878 ⟶ 2,498:
===Alternative using rug crate===
Based on the C++ code, which was based on the Java solution.
<langsyntaxhighlight lang="rust">// [dependencies]
// rug = "1.9"
 
Line 1,955 ⟶ 2,575:
println!();
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,982 ⟶ 2,602:
F(8): 1238926361552897, 93461639715357977769163558199606896584051237541638188580280321
</pre>
 
=={{header|Scala}}==
{{trans|Kotlin}}
<syntaxhighlight lang="scala">import scala.collection.mutable
import scala.collection.mutable.ListBuffer
 
object FermatNumbers {
def main(args: Array[String]): Unit = {
println("First 10 Fermat numbers:")
for (i <- 0 to 9) {
println(f"F[$i] = ${fermat(i)}")
}
println()
println("First 12 Fermat numbers factored:")
for (i <- 0 to 12) {
println(f"F[$i] = ${getString(getFactors(i, fermat(i)))}")
}
}
 
private val TWO = BigInt(2)
 
def fermat(n: Int): BigInt = {
TWO.pow(math.pow(2.0, n).intValue()) + 1
}
 
def getString(factors: List[BigInt]): String = {
if (factors.size == 1) {
return s"${factors.head} (PRIME)"
}
 
factors.map(a => a.toString)
.map(a => if (a.startsWith("-")) "(C" + a.replace("-", "") + ")" else a)
.reduce((a, b) => a + " * " + b)
}
 
val COMPOSITE: mutable.Map[Int, String] = scala.collection.mutable.Map(
9 -> "5529",
10 -> "6078",
11 -> "1037",
12 -> "5488",
13 -> "2884"
)
 
def getFactors(fermatIndex: Int, n: BigInt): List[BigInt] = {
var n2 = n
var factors = new ListBuffer[BigInt]
var loop = true
while (loop) {
if (n2.isProbablePrime(100)) {
factors += n2
loop = false
} else {
if (COMPOSITE.contains(fermatIndex)) {
val stop = COMPOSITE(fermatIndex)
if (n2.toString.startsWith(stop)) {
factors += -n2.toString().length
loop = false
}
}
if (loop) {
val factor = pollardRhoFast(n2)
if (factor == 0) {
factors += n2
loop = false
} else {
factors += factor
n2 = n2 / factor
}
}
}
}
 
factors.toList
}
 
def pollardRhoFast(n: BigInt): BigInt = {
var x = BigInt(2)
var y = BigInt(2)
var z = BigInt(1)
var d = BigInt(1)
var count = 0
 
var loop = true
while (loop) {
x = pollardRhoG(x, n)
y = pollardRhoG(pollardRhoG(y, n), n)
d = (x - y).abs
z = (z * d) % n
count += 1
if (count == 100) {
d = z.gcd(n)
if (d != 1) {
loop = false
} else {
z = BigInt(1)
count = 0
}
}
}
 
println(s" Pollard rho try factor $n")
if (d == n) {
return 0
}
d
}
 
def pollardRhoG(x: BigInt, n: BigInt): BigInt = ((x * x) + 1) % n
}</syntaxhighlight>
{{out}}
<pre>First 10 Fermat numbers:
F[0] = 3
F[1] = 5
F[2] = 17
F[3] = 257
F[4] = 65537
F[5] = 4294967297
F[6] = 18446744073709551617
F[7] = 340282366920938463463374607431768211457
F[8] = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F[9] = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
 
First 12 Fermat numbers factored:
F[0] = 3 (PRIME)
F[1] = 5 (PRIME)
F[2] = 17 (PRIME)
F[3] = 257 (PRIME)
F[4] = 65537 (PRIME)
Pollard rho try factor 4294967297
F[5] = 641 * 6700417
Pollard rho try factor 18446744073709551617
F[6] = 274177 * 67280421310721
Pollard rho try factor 340282366920938463463374607431768211457
F[7] = 59649589127497217 * 5704689200685129054721
Pollard rho try factor 115792089237316195423570985008687907853269984665640564039457584007913129639937
F[8] = 1238926361552897 * 93461639715357977769163558199606896584051237541638188580280321
Pollard rho try factor 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
F[9] = 2424833 * (C148)
Pollard rho try factor 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137217
Pollard rho try factor 3942951359960012586542991835686376608231592127249807732373409846031135195659174148737161255930050543559319182152642816343958573976075461198274610155058226350701077796608546283231637018483208223116080561800334422176622099740983337736621316898600121619871377542107047343253864459964167331555646795960321
F[10] = 45592577 * 6487031809 * (C291)
Pollard rho try factor 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230657
Pollard rho try factor 33150781373639412155846573868024639672856106606987835072026893834352453701925006737655987144186344206834820532125383540932102878651453631377873037143648178457002958783669056532601662155256508553423204658756451069116132055982639112479817996775373591674794399801442382402697828988429044712163168243619196804348072710121945390948428910309765481110260333687910970886853046635254307274981520537180895290310783635953818082306553996934497908037349010876970379631341148456045116407475229712217130141926525362871253437794629422541384355185626695660779797862427347553871011957167960991543632376506466281643163047416635393
F[11] = 974849 * 319489 * (C606)
Pollard rho try factor 1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190337
Pollard rho try factor 9106268965752186405773463110818163752233991481723476361152625650968740750826648212547208641935996986118024454955917854702609434541985662158212523327009262247869952450049350838706079834460006786304075107567909269645531121898331250125751682239313156601738683820643686003638396435055834553570682260579462973839574318172464558815116581626749391315641251152532705571615644886981829338611134458123396450764186936496833100701185274214915961723337127995182593580031119299575446791424418154036863609858251201843852076223383379133238000289598800458955855329052103961332983048473420515918928565951506637819342706575976725030506905683310915700945442329953941604008255667676914945655757474715779252371155778495946746587469464160684843488975918662295274965457887082037460184558511575570318625886351712499453155527762335682281851520733417380809781252979478377941937578568481859702438295520231435016188495646093490407803983345420364088331996467459309353537828143080691834120737157445502646809195267166779721413577366833939771467773331873590129210913628329073978766992198221682739812652450408607796042492802295258713711959073218748776359806123717024800451461326745599716651128725627280643537507664130920416107218492950792456907321580171946770433
Pollard rho try factor 350001591824186871106763863899530746217943677302816436473017740242946077356624684213115564488348300185108877411543625345263121839042445381828217455916005721464151569276047005167043946981206545317048033534893189043572263100806868980998952610596646556521480658280419327021257968923645235918768446677220584153297488306270426504473941414890547838382804073832577020334339845312084285496895699728389585187497914849919557000902623608963141559444997044721763816786117073787751589515083702681348245404913906488680729999788351730419178916889637812821243344085799435845038164784900107242721493170135785069061880328434342030106354742821995535937481028077744396075726164309052460585559946407282864168038994640934638329525854255227752926198464207255983432778402344965903148839661825873175277621985527846249416909718758069997783882773355041329208102046913755441975327368023946523920699020098723785533557579080342841062805878477869513695185309048285123705067072486920463781103076554014502567884803571416673251784936825115787932810954867447447568320403976197134736485611912650805539603318790667901618038578533362100071745480995207732506742832634459994375828162163700807237997808869771569154136465922798310222055287047244647419069003284481
F[12] = 114689 * 26017793 * 63766529 * (C1213)</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func fermat_number(n) {
2**(2**n) + 1
}
Line 2,002 ⟶ 2,770:
say ("F_#{n} = ", join(' * ', f.shift,
f.map { <C P>[.is_prime] + .len }...))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,030 ⟶ 2,798:
F_12 = 114689 * C1228
F_13 = 2710954639361 * C2454
</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">namespace import ::tcl::mathop::*
package require math::numtheory 1.1.1; # Buggy before tcllib-1.20
 
proc fermat n {
+ [** 2 [** 2 $n]] 1
}
 
 
for {set i 0} {$i < 10} {incr i} {
puts "F$i = [fermat $i]"
}
 
for {set i 1} {1} {incr i} {
puts -nonewline "F$i... "
flush stdout
set F [fermat $i]
set factors [math::numtheory::primeFactors $F]
if {[llength $factors] == 1} {
puts "is prime"
} else {
puts "factors: $factors"
}
}</syntaxhighlight>
 
{{out}}
<pre>
$ tclsh fermat.tcl
F0 = 3
F1 = 5
F2 = 17
F3 = 257
F4 = 65537
F5 = 4294967297
F6 = 18446744073709551617
F7 = 340282366920938463463374607431768211457
F8 = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F9 = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
F1... is prime
F2... is prime
F3... is prime
F4... is prime
F5... factors: 641 6700417
F6...
</pre>
 
=={{header|Wren}}==
{{libheader|===Wren-math}}cli===
{{libheader|Wren-big}}
In the absence of 'big integer' support, limited to computing and factorizing the first six Fermat numbers.
The first seven Fermat numbers are factorized in about 0.75 seconds but as it would take 'hours' to get any further I've stopped there.
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang="wren">import "./big" for BigInt
 
var fermat = Fn.new { |n| 2BigInt.two.pow(2.pow(n)) + 1 }
 
var fns = List.filled(10, null)
for (i in 0..5) {
System.print("The first 10 Fermat numbers are:")
var n = fermat.call(i)
for (i in 0..9) {
var pf = Int.primeFactors(n)
fns[i] = fermat.call(i)
var kind = (pf.count == 1) ? "prime" : "composite"
System.print("F%(String.fromCodePoint(0x2080+i)): %(n) -> factors = %(pf) -> %(kindfns[i])")
}
}</lang>
 
System.print("\nFactors of the first 7 Fermat numbers:")
for (i in 0..6) {
System.write("F%(String.fromCodePoint(0x2080+i)) = ")
var factors = BigInt.primeFactors(fns[i])
System.write("%(factors)")
if (factors.count == 1) {
System.print(" (prime)")
} else if (!factors[1].isProbablePrime(5)) {
System.print(" (second factor is composite)")
} else {
System.print()
}
}</syntaxhighlight>
 
{{out}}
<pre>
The first 10 Fermat numbers are:
F₀: 3 -> factors = [3] -> prime
F₀ = 3
F₁: 5 -> factors = [5] -> prime
F₁ = 5
F₂: 17 -> factors = [17] -> prime
F₂ = 17
F₃: 257 -> factors = [257] -> prime
F₃ = 257
F₄: 65537 -> factors = [65537] -> prime
F₄ = 65537
F₅: 4294967297 -> factors = [641, 6700417] -> composite
F₅ = 4294967297
F₆ = 18446744073709551617
F₇ = 340282366920938463463374607431768211457
F₈ = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F₉ = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
 
Factors of the first 7 Fermat numbers:
F₀ = [3] (prime)
F₁ = [5] (prime)
F₂ = [17] (prime)
F₃ = [257] (prime)
F₄ = [65537] (prime)
F₅ = [641, 6700417]
F₆ = [274177, 67280421310721]
</pre>
 
===Embedded===
{{libheader|Wren-gmp}}
{{libheader|Wren-ecm}}
This uses Lenstra's elliptical curve method (ECM) to find F₇ and Pollard's Rho to find the rest, including the first factor of F₉.
 
The overall time for this particular run was about 86 seconds with F₇ being found in only a couple of them. However, being non-deterministic, ECM is quite erratic time-wise and could take up to 3 minutes longer.
<syntaxhighlight lang="wren">/* fermat_numbers_2.wren */
 
import "./gmp" for Mpz
import "./ecm" for Ecm
import "random" for Random
var fermat = Fn.new { |n| Mpz.two.pow(2.pow(n)) + 1 }
var fns = List.filled(10, null)
System.print("The first 10 Fermat numbers are:")
for (i in 0..9) {
fns[i] = fermat.call(i)
System.print("F%(String.fromCodePoint(0x2080+i)) = %(fns[i])")
}
 
System.print("\nFactors of the first 8 Fermat numbers:")
for (i in 0..8) {
System.write("F%(String.fromCodePoint(0x2080+i)) = ")
var factors = (i != 7) ? Mpz.primeFactors(fns[i]) : Ecm.primeFactors(fns[i])
System.write("%(factors)")
if (factors.count == 1) {
System.print(" (prime)")
} else if (!factors[1].probPrime(15)) {
System.print(" (second factor is composite)")
} else {
System.print()
}
}
 
System.print("\nThe first factor of F₉ is %(Mpz.pollardRho(fns[9])).")</syntaxhighlight>
 
{{out}}
<pre>
The first 10 Fermat numbers are:
F₀ = 3
F₁ = 5
F₂ = 17
F₃ = 257
F₄ = 65537
F₅ = 4294967297
F₆ = 18446744073709551617
F₇ = 340282366920938463463374607431768211457
F₈ = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F₉ = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
 
Factors of the first 8 Fermat numbers:
F₀ = [3] (prime)
F₁ = [5] (prime)
F₂ = [17] (prime)
F₃ = [257] (prime)
F₄ = [65537] (prime)
F₅ = [641, 6700417]
F₆ = [274177, 67280421310721]
F₇ = [59649589127497217, 5704689200685129054721]
F₈ = [1238926361552897, 93461639715357977769163558199606896584051237541638188580280321]
 
The first factor of F₉ is 2424833.
</pre>
 
Line 2,059 ⟶ 2,967:
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
for big ints and primes
<langsyntaxhighlight lang="zkl">fermatsW:=[0..].tweak(fcn(n){ BI(2).pow(BI(2).pow(n)) + 1 });
println("First 10 Fermat numbers:");
foreach n in (10){ println("F",n,": ",fermatsW.next()) }</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 2,076 ⟶ 2,984:
F9: 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097
</pre>
<langsyntaxhighlight lang="zkl">fcn primeFactorsBI(n){ // Return a list of the prime factors of n
acc:=fcn(n,k,acc,maxD){ // k is primes
if(n==1 or k>maxD) acc.close();
Line 2,088 ⟶ 2,996:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fermatsW:=[0..].tweak(fcn(n){ BI(2).pow(BI(2).pow(n)) + 1 });
println("Factors of first few Fermat numbers:");
foreach n in (7){
println("Factors of F",n,": ",factorsBI(fermatsW.next()).concat(" "));
}</langsyntaxhighlight>
{{out}}
<pre>
885

edits