Created
December 27, 2020 12:13
-
-
Save transhapHigsn/032f34dd9deef7f7407a0f661b148672 to your computer and use it in GitHub Desktop.
Rust Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn check_for_prime(div: u32, primes: &mut Vec<u32>) -> bool { | |
for i in primes { | |
if *i > div/2 { | |
return true; | |
} | |
if div % *i == 0 { | |
return false; | |
} | |
} | |
return true; | |
} | |
pub fn nth(n: u32) -> u32 { | |
let mut div = 2; | |
let mut primes: Vec<u32> = Vec::new(); | |
let mut counter = 0; | |
loop { | |
match check_for_prime(div, &mut primes) { | |
true => { | |
primes.push(div); | |
counter += 1; | |
if counter == n + 1 { | |
return div; | |
} | |
}, | |
false => (), | |
}; | |
if div % 2 == 0 { | |
div += 1; | |
} else { | |
div += 2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment