[Sync Iteration] rust/nth-prime/1

This commit is contained in:
exercism-solutions-syncer[bot] 2025-12-02 16:38:11 +00:00 committed by GitHub
parent f80fbca12e
commit 90436f1baf
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]
}