Lucas-Lehmer test: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Added an embedded version.) |
|||
Line 3,687: | Line 3,687: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
===Wren-CLI (BigInt)=== |
|||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
{{libheader|wren-math}} |
{{libheader|wren-math}} |
||
This follows the lines of my Kotlin entry but uses a table to quicken up the checking of the larger numbers. Despite this, it still takes just over 3 minutes to reach M4423. Surprisingly, using ''modPow'' rather than the simple ''%'' operator is even slower. |
This follows the lines of my Kotlin entry but uses a table to quicken up the checking of the larger numbers. Despite this, it still takes just over 3 minutes to reach M4423. Surprisingly, using ''modPow'' rather than the simple ''%'' operator is even slower. |
||
<lang ecmascript> |
<lang ecmascript>import "/big" for BigInt |
||
import "/math" for Int |
import "/math" for Int |
||
import "io" for Stdout |
import "io" for Stdout |
||
Line 3,733: | Line 3,734: | ||
Took 181.271083 seconds. |
Took 181.271083 seconds. |
||
</pre> |
|||
===Embedded (GMP)=== |
|||
{{libheader|Wren-gmp}} |
|||
Same approach as the CLI version but now uses GMP. Far quicker, of course, as we can now check up to M110503 in less time than before. |
|||
<lang ecmascript>import "./gmp" for Mpz |
|||
import "./math" for Int |
|||
var start = System.clock |
|||
var max = 28 |
|||
var count = 0 |
|||
var table = [521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503] |
|||
var p = 3 // first odd prime |
|||
var ix = 0 |
|||
var one = Mpz.one |
|||
var two = Mpz.two |
|||
var m = Mpz.new() |
|||
var s = Mpz.new() |
|||
while (true) { |
|||
m.uiPow(2, p).sub(one) |
|||
s.setUi(4) |
|||
for (i in 1..p-2) s.square.sub(two).rem(m) |
|||
if (s.isZero) { |
|||
count = count + 1 |
|||
System.write("M%(p) ") |
|||
if (count == max) { |
|||
System.print() |
|||
break |
|||
} |
|||
} |
|||
// obtain next odd prime or look up in table after 127 |
|||
if (p < 127) { |
|||
while (true) { |
|||
p = p + 2 |
|||
if (Int.isPrime(p)) break |
|||
} |
|||
} else { |
|||
p = table[ix] |
|||
ix = ix + 1 |
|||
} |
|||
} |
|||
System.print("\nTook %(System.clock - start) seconds.")</lang> |
|||
{{out}} |
|||
<pre> |
|||
M3 M5 M7 M13 M17 M19 M31 M61 M89 M107 M127 M521 M607 M1279 M2203 M2281 M3217 M4253 M4423 M9689 M9941 M11213 M19937 M21701 M23209 M44497 M86243 M110503 |
|||
Took 127.317323 seconds. |
|||
</pre> |
</pre> |
||