-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.rs
More file actions
46 lines (40 loc) · 888 Bytes
/
main.rs
File metadata and controls
46 lines (40 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn is_prime(n: u32) -> bool {
if n < 2 {
return false;
}
if n == 2 || n == 3 {
return true;
}
if n % 2 == 0 {
return false;
}
let mut i = 3;
while i * i <= n {
if n % i == 0 {
return false;
}
i += 2;
}
true
}
fn find_primes(start: u32, count: usize, buffer: &mut [u32]) -> usize {
let mut found = 0;
let mut candidate = if start % 2 == 0 { start + 1 } else { start };
while found < count && found < buffer.len() {
if is_prime(candidate) {
buffer[found] = candidate;
found += 1;
}
candidate += 2;
}
found
}
fn main() {
let mut primes = [0u32; 10];
let found = find_primes(10_000, 10, &mut primes);
let mut i: usize = 0;
while i < found {
assert!(is_prime(primes[i]));
i += 1;
}
}