Jump to content

Twin primes: Difference between revisions

Rust program allows limits to be specified on command line
(→‎{{header|Phix}}: Added both parameter to reflect recent task specification changes.)
(Rust program allows limits to be specified on command line)
Line 603:
 
=={{header|Rust}}==
Limits can be specified on the command line, otherwise the twin prime counts for powers
of ten from 1 to 10 are shown.
<lang rust>// [dependencies]
// primal = "0.3"
// num-format = "0.4"
 
use num_format::{Locale, ToFormattedString};
fn main() {
 
use num_format::{Locale, ToFormattedString};
fn twin_prime_count_for_powers_of_ten(max_power: u32) {
let mut count = 0;
let mut previous = 0;
Line 622 ⟶ 625:
limit *= 10;
power += 1;
if power > 10max_power {
break;
}
Line 630 ⟶ 633:
}
previous = prime;
}
}
 
fn twin_prime_count(limit: usize) {
let mut count = 0;
let mut previous = 0;
for prime in primal::Primes::all().take_while(|x| *x < limit) {
if previous > 0 && prime == previous + 2 {
count += 1;
}
previous = prime;
}
println!(
"Number of twin prime pairs less than {} is {}",
limit.to_formatted_string(&Locale::en),
count.to_formatted_string(&Locale::en)
);
}
 
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
for i in 1..args.len() {
if let Ok(limit) = args[i].parse::<usize>() {
twin_prime_count(limit);
} else {
eprintln!("Cannot parse limit from string {}", args[i]);
}
}
} else {
twin_prime_count_for_powers_of_ten(10);
}
}</lang>
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.