-
-
Save rtaycher/f15b21ec662208b07d7e to your computer and use it in GitHub Desktop.
euler problem doesn't compile
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
struct Primes{ | |
current: u64, | |
prev: Vec<u64> | |
} | |
impl Primes{ | |
fn new() -> Primes { | |
Primes { current: 1, prev: Vec::new()} | |
} | |
} | |
impl Iterator for Primes { | |
type Item = u64; | |
fn next(&mut self) -> Option<u64> { | |
let mut c=self.current; | |
while true { | |
c = c+1; | |
if(self.prev.into_iter().any(|x| c % x == 0)) | |
{ | |
break; | |
} | |
} | |
self.current = c; | |
self.prev.push(self.current); | |
Some(self.current) | |
} | |
} | |
fn largest_prime_factor(n: u64) -> u64 | |
{ | |
let mut n2 = n; | |
let mut lp = 1; | |
//let primes : Iterator<u64> = Primes::new(); | |
let primes = Primes::new(); | |
for p in Primes::new() { | |
if(n2 % p == 0) | |
{ | |
lp = p; | |
n2 = n2 /p; | |
} | |
if(n2 / 2 > p) | |
{ | |
return lp; | |
} | |
} | |
} | |
fn main() { | |
let n = 600851475143; | |
let v = largest_prime_factor(n); | |
println!("Largest prime factor of {:?}:{:?}", n, v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment