Substring primes: Difference between revisions
Content added Content deleted
(add RPL) |
m (rust example) |
||
Line 1,016: | Line 1,016: | ||
Found 9 numbers in which all substrings are primes |
Found 9 numbers in which all substrings are primes |
||
done... |
done... |
||
</pre> |
|||
=={{header|Rust}}== |
|||
<stntaxhighlight 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> |
</pre> |
||