structPrimesIterator{found_primes:Vec<u64>,next_to_check:u64,}implPrimesIterator{fnnew()->Self{PrimesIterator{found_primes:Vec::new(),next_to_check:2,}}}implIteratorforPrimesIterator{typeItem=u64;fnnext(&mutself)->Option<u64>{fornuminself.next_to_check..{ifself.found_primes.iter().all(|prime|num%prime!=0){self.found_primes.push(num);self.next_to_check=num+1;returnSome(num);}}unreachable!()}}fngreatest_prime_divisor(mutnumber:u64)->u64{forprimeinPrimesIterator::new(){whilenumber%prime==0{number/=prime;}ifnumber<=1{returnprime;}}unreachable!()}fnmain(){letnumber=std::env::args().nth(1).expect("Number must be first argument");letnumber:u64=number.parse().unwrap();println!("The greatest prime divisor of {} is {}",number,greatest_prime_divisor(number));}
Result:
The greatest prime divisor of 600851475143 is 6857
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust:
Result: