Substring primes: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Added 'Advanced' version.) |
(→Advanced: No need to check 2 digit numbers separately.) |
||
Line 449: | Line 449: | ||
<lang ecmascript>import "/math" for Int |
<lang ecmascript>import "/math" for Int |
||
var |
var results = [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 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 discarded = [] |
||
var tests = 4 // i.e. to obtain |
var tests = 4 // i.e. to obtain initial results in the first place |
||
// check 2 digit numbers |
// check 2 digit numbers or greater |
||
// note that 'results' is a moving feast. If the loop eventually terminates that's all there are. |
|||
for ( |
for (r in results) { |
||
for (od in odigits) { |
for (od in odigits) { |
||
// |
// the 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) { |
if ((r % 10) != od) { |
||
var n = |
var n = r * 10 + od |
||
if (Int.isPrime(n)) results.add(n) else discarded.add(n) |
if (Int.isPrime(n)) results.add(n) else discarded.add(n) |
||
tests = tests + 1 |
tests = tests + 1 |