Last active
May 2, 2017 13:31
-
-
Save ranjanprj/cf4ee0a91052dc415cf1a9a0ce0bb982 to your computer and use it in GitHub Desktop.
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
/* | |
The prime factors of 13195 are 5, 7, 13 and 29. | |
What is the largest prime factor of the number 600851475143 ? | |
Answer: | |
*/ | |
fn is_prime(num : u64 ) -> bool{ | |
if num == 1 { | |
return true; | |
} | |
if num < 1 { | |
return false; | |
} | |
for i in 2..num{ | |
if num % i == 0 && i != num { | |
return false; | |
} | |
} | |
return true; | |
} | |
fn get_largest_prime_factor(num : u64) -> u64 { | |
println!("Entering prime "); | |
let mut largest_pf :u64 = 2; | |
let mut next_pf : u64 = 2; | |
loop{ | |
if next_pf < num && num % next_pf == 0 && is_prime(next_pf){ | |
largest_pf = next_pf; | |
}else if next_pf >= num { | |
break; | |
} | |
next_pf += 1; | |
} | |
largest_pf | |
} | |
fn main(){ | |
// let mut p = 1; | |
// loop { | |
// p += 1; | |
// if is_prime(p) { | |
// println!("Prime number {}",p); | |
// } | |
// if p > 21{ | |
// break; | |
// } | |
// } | |
// println!("{}", p); | |
println!("Calling prime "); | |
// Still not resolved for such large integer | |
let largest_pf = get_largest_prime_factor(600851475143 ); | |
println!("{}", largest_pf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment