Created
September 2, 2019 20:30
-
-
Save GoldsteinE/d460e45cdd2726099028f4febab1d802 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
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
typedef char bool; | |
static const bool false = 0; | |
static const bool true = 1; | |
bool primep(uint64_t n) { | |
if (n == 2) { | |
return true; | |
} else if (n % 2 == 0) { | |
return false; | |
} else if (n < 3) { | |
return false; | |
} else { | |
uint64_t p = 3; | |
while (p <= n / p) { | |
if (n % p == 0) { | |
return false; | |
} | |
p += 2; | |
} | |
return true; | |
} | |
} | |
int main(int argc, char** argv) { | |
(void)argc; | |
uint64_t n = strtoull(argv[1], NULL, 10); | |
if (primep(n)) { | |
puts("prime"); | |
} else { | |
puts("composite"); | |
} | |
} |
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::env; | |
fn primep(n: u64) -> bool { | |
if n == 2 { | |
true | |
} else if n % 2 == 0 { | |
false | |
} else if n < 3 { | |
false | |
} else { | |
let mut p = 3u64; | |
while p <= n / p { | |
if n % p == 0 { | |
return false; | |
} | |
p += 2; | |
} | |
true | |
} | |
} | |
fn main() { | |
let n: u64 = env::args() | |
.skip(1) | |
.next() | |
.expect("Must be at least 1 arg") | |
.parse() | |
.expect("Enter valid number"); | |
if primep(n) { | |
println!("prime") | |
} else { | |
println!("composite") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment