Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
71
Task/Prime-triangle/Rust/prime-triangle.rust
Normal file
71
Task/Prime-triangle/Rust/prime-triangle.rust
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
fn is_prime(n: u32) -> bool {
|
||||
assert!(n < 64);
|
||||
((1u64 << n) & 0x28208a20a08a28ac) != 0
|
||||
}
|
||||
|
||||
fn prime_triangle_row(a: &mut [u32]) -> bool {
|
||||
if a.len() == 2 {
|
||||
return is_prime(a[0] + a[1]);
|
||||
}
|
||||
for i in (1..a.len() - 1).step_by(2) {
|
||||
if is_prime(a[0] + a[i]) {
|
||||
a.swap(i, 1);
|
||||
if prime_triangle_row(&mut a[1..]) {
|
||||
return true;
|
||||
}
|
||||
a.swap(i, 1);
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn prime_triangle_count(a: &mut [u32]) -> u32 {
|
||||
let mut count = 0;
|
||||
if a.len() == 2 {
|
||||
if is_prime(a[0] + a[1]) {
|
||||
count += 1;
|
||||
}
|
||||
} else {
|
||||
for i in (1..a.len() - 1).step_by(2) {
|
||||
if is_prime(a[0] + a[i]) {
|
||||
a.swap(i, 1);
|
||||
count += prime_triangle_count(&mut a[1..]);
|
||||
a.swap(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
fn print(a: &[u32]) {
|
||||
if a.is_empty() {
|
||||
return;
|
||||
}
|
||||
print!("{:2}", a[0]);
|
||||
for x in &a[1..] {
|
||||
print!(" {:2}", x);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
use std::time::Instant;
|
||||
let start = Instant::now();
|
||||
for n in 2..21 {
|
||||
let mut a: Vec<u32> = (1..=n).collect();
|
||||
if prime_triangle_row(&mut a) {
|
||||
print(&a);
|
||||
}
|
||||
}
|
||||
println!();
|
||||
for n in 2..21 {
|
||||
let mut a: Vec<u32> = (1..=n).collect();
|
||||
if n > 2 {
|
||||
print!(" ");
|
||||
}
|
||||
print!("{}", prime_triangle_count(&mut a));
|
||||
}
|
||||
println!();
|
||||
let time = start.elapsed();
|
||||
println!("\nElapsed time: {} milliseconds", time.as_millis());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue