Substring primes: Difference between revisions

→‎{{header|Wren}}: Added 'Advanced' version.
(Against my better judgement, but I've had enough of this nonsense, and since some people are too weak-minded and lily-livered to do it themselves....)
(→‎{{header|Wren}}: Added 'Advanced' version.)
Line 408:
=={{header|Wren}}==
{{libheader|Wren-math}}
 
===Using a limit===
<lang ecmascript>import "/math" for Int
 
Line 441 ⟶ 443:
Found 9 primes < 500 where all substrings are also primes, namely:
[2, 3, 5, 7, 23, 37, 53, 73, 373]
</pre>
 
===Advanced===
This follows the logic in the OEIS A085823 comments.
<lang ecmascript>import "/math" for Int
 
var sdigits = [2, 3, 5, 7] // 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 sdigits in the first place
 
// check 2 digit numbers
for (sd in sdigits) {
for (od in odigits) {
// sd 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 = 10 * r + od
if (Int.isPrime(n)) results.add(n) else discarded.add(n)
tests = tests + 1
}
}
}
 
System.print("There are %(results.count) primes where all substrings are also primes, namely:")
System.print(results)
System.print("\nThe following numbers were also tested for primality but found to be composite:")
System.print(discarded)
System.print("\nTotal number of primality tests = %(tests)")</lang>
 
{{out}}
<pre>
There are 9 primes where all substrings are also primes, namely:
[2, 3, 5, 7, 23, 37, 53, 73, 373]
 
The following numbers were also tested for primality but found to be composite:
[27, 57, 237, 537, 737, 3737]
 
Total number of primality tests = 15
</pre>
9,482

edits