mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
Merge pull request #1 from Weltenbummler397/exercism-sync/ebfc06743a5beee6
[Sync Iteration] rust/nth-prime/1
This commit is contained in:
commit
dd9840e502
9
solutions/rust/nth-prime/1/Cargo.toml
Normal file
9
solutions/rust/nth-prime/1/Cargo.toml
Normal 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]
|
||||
27
solutions/rust/nth-prime/1/src/lib.rs
Normal file
27
solutions/rust/nth-prime/1/src/lib.rs
Normal 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]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user