Merge pull request #1 from Weltenbummler397/exercism-sync/ebfc06743a5beee6

[Sync Iteration] rust/nth-prime/1
This commit is contained in:
Weltenbummler397 2026-05-04 09:52:58 +02:00 committed by GitHub
commit dd9840e502
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,9 @@
[package]
name = "nth_prime"
version = "0.1.0"
edition = "2024"
# Not all libraries from crates.io are available in Exercism's test runner.
# The full list of available libraries is here:
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
[dependencies]

View File

@ -0,0 +1,27 @@
pub fn nth(n: u32) -> u32 {
let mut primes= Vec::new();
let mut canditate = 2;
while primes.len() <= n as usize {
let mut is_prime = true;
let sqrt_canditate = (canditate as f64).sqrt() as u32;
for &prime in primes.iter() {
if prime > sqrt_canditate {
break;
}
if canditate % prime == 0 {
is_prime = false;
break;
}
}
if is_prime {
primes.push(canditate);
}
if canditate == 2 {
canditate = 3;
} else {
canditate += 2;
}
}
primes[n as usize]
}