public
Created

  • Download Gist
isprime.rs
Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// Rough port of https://gist.github.com/1412787
 
use std;
use euler;
import std::io::println;
 
fn result(arg: str, b: bool) -> str {
 
let msg = "";
 
if b {
msg = #fmt("%s is prime", arg);
}
else {
msg = #fmt("%s is not prime", arg);
}
 
msg
}
 
fn main(args: [str]) {
 
// TODO: Find out why
// in the mean time, do this icky dance to check for args
let x = -1;
 
for arg in args {
x += 1;
}
 
// why not? if str::is_empty(args[1]) {
if x == 0 {
for i in [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11197 ] {
println(
result( int::str(i), euler::is_prime(i) )
);
}
}
else {
 
let num = int::from_str(args[1]);
 
println(
result( args[1], euler::is_prime(num) )
);
}
}
 
 
// compile and run:
// rustc isprime.rs -L . && ./isprime && ./isprime 9450031 && ./isprime 104743
// 2 is prime
// 3 is prime
// 5 is prime
// 7 is prime
// 11197 is prime
// 9450031 is not prime

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.