-
-
Save lovasoa/30904279d9e2fc7d1f3b to your computer and use it in GitHub Desktop.
501
This file contains hidden or 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
struct Primes { | |
primes : Vec<u64>, | |
} | |
impl Primes { | |
fn new() -> Primes { | |
Primes{primes: vec![2]} | |
} | |
fn test(&self, totest: u64) -> bool { | |
let mut n = totest; | |
for &i in &self.primes { | |
while n%i == 0 {n /= i;} | |
if n==1 {return false;} | |
} | |
return true; | |
} | |
} | |
impl Iterator for Primes { | |
type Item = u64; | |
fn next(&mut self) -> Option<u64> { | |
let mut n = *self.primes.last().unwrap() + 1; | |
while !self.test(n) { n += 1; } | |
self.primes.push(n); | |
return Some(n); | |
} | |
} | |
fn testnum (primes: &Vec<u64>, i: u64) -> u64 { | |
let mut k = i; | |
let mut count = 0; | |
for &prime in primes { | |
while k%prime == 0 { | |
k /= prime; | |
count += 1; | |
if count == 4 && k==1 {return 1} | |
else if count > 4 || k==1 {return 0} | |
} | |
} | |
if count == 4 && k==1 {1} else {0} | |
} | |
fn main() { | |
let m = 100u64; | |
let primes : Vec<u64> = Primes::new().take(m).collect(); | |
let mut s = 0; | |
for i in 1..m { | |
s += testnum(&primes, i); | |
} | |
println!("{}", s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment