Created
May 31, 2014 15:52
-
-
Save misterhat/5b89a529ea688c75f69f 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
use std::num; | |
fn is_prime(number: int) -> bool { | |
for i in range(2, ((number as f64).sqrt() as int) + 1) { | |
if number % i == 0 { | |
return false | |
} | |
} | |
true | |
} | |
fn is_circular_prime(number: int) -> bool { | |
if !is_prime(number) { | |
return false | |
} | |
let mut places = 1; | |
while (number / num::pow(10, places)) > 0 { | |
places += 1; | |
} | |
let mut check = number; | |
for i in range(0, places) { | |
if i > 0 && !is_prime(check) { | |
return false | |
} | |
check = (check / 10) + ((check % 10) * num::pow(10, places - 1)); | |
} | |
true | |
} | |
fn main() { | |
let mut count = 0; | |
for i in range(2, 1000000) { | |
if is_circular_prime(i) { | |
count += 1; | |
} | |
} | |
println!("{}", count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment