Substring primes: Difference between revisions

m
(add RPL)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by one other user not shown)
Line 1,016:
Found 9 numbers in which all substrings are primes
done...
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use primes::is_prime;
 
fn counted_prime_test() {
let mut number_of_prime_tests = 0;
let mut non_primes = vec![0; 0];
// start with 1 digit primes
let mut results: Vec<i32> = [2, 3, 5, 7].to_vec();
// check 2 digit candidates
for n in results.clone() {
for i in [3, 7].to_vec() {
if n != i {
let candidate = n * 10 + i;
if candidate < 100 {
number_of_prime_tests += 1;
if is_prime(candidate as u64) {
results.push(candidate);
} else {
non_primes.push(candidate);
}
}
}
}
}
// check 3 digit candidates
for n in results.clone() {
for i in [3, 7].to_vec() {
if 10 < n && n < 100 && n % 10 != i {
let candidate = n * 10 + i;
number_of_prime_tests += 1;
if is_prime(candidate as u64) {
results.push(candidate);
} else {
non_primes.push(candidate);
}
}
}
}
println!("Results: {results:?}.\nThe function isprime() was called {number_of_prime_tests} times.");
println!("Discarded nonprime candidates: {non_primes:?}");
println!("Because 237, 537, and 737 are excluded, we cannot generate any larger candidates from 373.");
}
 
fn main() {
counted_prime_test();
}
</syntaxhighlight>{{out}}
<pre>
Results: [2, 3, 5, 7, 23, 37, 53, 73, 373].
The function isprime() was called 10 times.
Discarded nonprime candidates: [27, 57, 237, 537, 737]
Because 237, 537, and 737 are already excluded, we cannot generate any larger candidates from 373.
</pre>
 
Line 1,133 ⟶ 1,187:
 
===Using a limit===
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
var primes = Int.primeSieve(499)
var sprimes = []
Line 1,160 ⟶ 1,214:
===Advanced===
This follows the logic in the OEIS A085823 comments.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var results = [2, 3, 5, 7] // number must begin with a prime digit
9,482

edits