Circular primes: Difference between revisions

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

edits