You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.5 KiB
71 lines
1.5 KiB
use std::collections::HashSet; |
|
use std::env; |
|
|
|
fn er() -> Vec<u64> { |
|
let mut nums = Vec::new(); |
|
let size = 4000000; |
|
for n in 0..size+1 { nums.push(true); } |
|
let mut p = 1; |
|
loop { |
|
let mut found = false; |
|
for i in p+1..size { |
|
if nums[i] { |
|
found = true; |
|
p = i; |
|
break; |
|
} |
|
} |
|
if !found { break; } |
|
for i in (2 * p .. size).step_by(p) { |
|
nums[i] = false; |
|
} |
|
} |
|
let mut res = Vec::new(); |
|
for i in 2..size { |
|
if nums[i] { res.push(i as u64); } |
|
} |
|
return res; |
|
} |
|
|
|
fn span(l: usize, ps: &Vec<u64>, hs: &HashSet<u64>) -> u64 { |
|
let mut sum = 0u64; |
|
let mut max = 0u64; |
|
|
|
for i in 0..ps.len() { |
|
sum += ps.get(i).unwrap(); |
|
if i >= l { |
|
sum -= ps.get(i - l).unwrap(); |
|
} |
|
if sum > 1000000 { |
|
continue; |
|
} |
|
if hs.contains(&sum) && i >= l { |
|
if sum > max { |
|
max = sum; |
|
} |
|
} |
|
} |
|
return max; |
|
} |
|
|
|
fn main() { |
|
//env::set_var("RUST_BACKTRACE", "full"); |
|
let ps = er(); |
|
let mut hs = HashSet::new(); |
|
for p in ps.iter() { |
|
hs.insert(*p); |
|
} |
|
let mut max = 0; |
|
//let mut l_max = 0; |
|
for s in 1..ps.len() - 1 { |
|
let m = span(s, &ps, &hs); |
|
if m > 0 { |
|
//l_max = s; |
|
max = m; |
|
println!("{} {}", s, m); |
|
} |
|
} |
|
|
|
println!("{}", max); |
|
|
|
}
|
|
|