-
Notifications
You must be signed in to change notification settings - Fork 0
/
goldbachs_other_conjecture.rs
32 lines (28 loc) · 1.07 KB
/
goldbachs_other_conjecture.rs
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
use crate::utils;
pub fn solve() -> i64 {
const LIMIT: usize = 10000;
// This indicates which numbers can be constructed in the described manner.
// Since only odd numbers are of interest, half of the space used is
// wasted. Doing extra processing to prevent wastage increases the running
// time by more than 100 µs.
let mut goldbach_constructible = vec![false; LIMIT];
let sieve = utils::SieveOfAtkin::new(LIMIT);
for prime in sieve.iter() {
// Squares are quadrilateral numbers. Generating them like this avoids
// having to do multiplication.
for square in utils::Polygonal::new(4) {
let num = (prime + 2 * square) as usize;
if num < goldbach_constructible.len() {
goldbach_constructible[num] = true;
} else {
break;
}
}
}
let result = (3..goldbach_constructible.len())
.step_by(2)
.find(|&idx| !goldbach_constructible[idx] && !sieve.is_prime(idx))
.unwrap();
assert_eq!(result, 5777);
result as i64
}