Substring primes: Difference between revisions

→‎Advanced: No need to check 2 digit numbers separately.
(→‎{{header|Wren}}: Added 'Advanced' version.)
(→‎Advanced: No need to check 2 digit numbers separately.)
Line 449:
<lang ecmascript>import "/math" for Int
 
var sdigitsresults = [2, 3, 5, 7] // number must begin with a prime digit
var odigits = [3, 7] // other digits must be 3 or 7 as there would be a composite substring otherwise
var results = sdigits.toList
var discarded = []
var tests = 4 // i.e. to obtain sdigitsinitial results in the first place
 
// check 2 digit numbers or greater
// note that 'results' is a moving feast. If the loop eventually terminates that's all there are.
for (sdr in sdigitsresults) {
for (od in odigits) {
// sdthe last digit of r and od must be different otherwise number would be divisible by 11
if (sd != od) {
var n = sd * 10 + od
if (Int.isPrime(n)) results.add(n) else discarded.add(n)
tests = tests + 1
}
}
}
 
// check 3 digit numbers or greater
for (r in results.where {|r| r > 7 }) {
for (od in odigits) {
// can exclude twin digits as would be divisible by 11
if ((r % 10) != od) {
var n = 10r * r10 + od
if (Int.isPrime(n)) results.add(n) else discarded.add(n)
tests = tests + 1
9,482

edits