Circular primes: Difference between revisions
Content deleted Content added
→{{header|Wren}}: Using a more efficient method to calculate repunit strings, marginally quicker overall. |
|||
Line 2,652: | Line 2,652: | ||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-str}} |
|||
===Wren-cli=== |
===Wren-cli=== |
||
Second part is very slow - over 37 minutes to find all four. |
Second part is very slow - over 37 minutes to find all four. |
||
<lang ecmascript>import "/math" for Int |
<lang ecmascript>import "/math" for Int |
||
import "/big" for BigInt |
import "/big" for BigInt |
||
import "/str" for Str |
|||
var circs = [] |
var circs = [] |
||
Line 2,677: | Line 2,679: | ||
return true |
return true |
||
} |
} |
||
var repunit = Fn.new { |n| BigInt.new("1" |
var repunit = Fn.new { |n| BigInt.new(Str.repeat("1", n)) } |
||
System.print("The first 19 circular primes are:") |
System.print("The first 19 circular primes are:") |
||
Line 2,707: | Line 2,709: | ||
} |
} |
||
System.print(circs) |
System.print(circs) |
||
System.print("\nThe next 4 circular primes, in repunit format, are:") |
System.print("\nThe next 4 circular primes, in repunit format, are:") |
||
count = 0 |
count = 0 |
||
Line 2,732: | Line 2,734: | ||
===Embedded=== |
===Embedded=== |
||
{{libheader|Wren-gmp}} |
{{libheader|Wren-gmp}} |
||
A massive speed-up, of course, when GMP is plugged in for the 'probably prime' calculations. |
A massive speed-up, of course, when GMP is plugged in for the 'probably prime' calculations. 11 minutes 19 seconds including the stretch goal. |
||
<lang ecmascript>/* circular_primes_embedded.wren */ |
<lang ecmascript>/* circular_primes_embedded.wren */ |
||
import "./gmp" for Mpz |
import "./gmp" for Mpz |
||
import "./math" for Int |
import "./math" for Int |
||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
import "./str" for Str |
|||
var circs = [] |
var circs = [] |
||
var isCircular = Fn.new { |n| |
var isCircular = Fn.new { |n| |
||
var nn = n |
var nn = n |
||
Line 2,759: | Line 2,762: | ||
return true |
return true |
||
} |
} |
||
System.print("The first 19 circular primes are:") |
System.print("The first 19 circular primes are:") |
||
var digits = [1, 3, 7, 9] |
var digits = [1, 3, 7, 9] |
||
Line 2,787: | Line 2,790: | ||
} |
} |
||
System.print(circs) |
System.print(circs) |
||
System.print("\nThe next 4 circular primes, in repunit format, are:") |
System.print("\nThe next 4 circular primes, in repunit format, are:") |
||
count = 0 |
count = 0 |
||
Line 2,794: | Line 2,797: | ||
var repunit = Mpz.new() |
var repunit = Mpz.new() |
||
for (p in primes[3..-1]) { |
for (p in primes[3..-1]) { |
||
repunit.setStr("1" |
repunit.setStr(Str.repeat("1", p), 10) |
||
if (repunit.probPrime(10) > 0) { |
if (repunit.probPrime(10) > 0) { |
||
rus.add("R(%(p))") |
rus.add("R(%(p))") |
||
Line 2,804: | Line 2,807: | ||
System.print("\nThe following repunits are probably circular primes:") |
System.print("\nThe following repunits are probably circular primes:") |
||
for (i in [5003, 9887, 15073, 25031, 35317, 49081]) { |
for (i in [5003, 9887, 15073, 25031, 35317, 49081]) { |
||
repunit.setStr("1" |
repunit.setStr(Str.repeat("1", i), 10) |
||
Fmt.print("R($-5d) : $s", i, repunit.probPrime(15) > 0) |
Fmt.print("R($-5d) : $s", i, repunit.probPrime(15) > 0) |
||
}</lang> |
}</lang> |