Last active
August 29, 2019 11:08
-
-
Save adityasuseno/1895ccb79a785d31bb307479fd08cddc to your computer and use it in GitHub Desktop.
Simple Program in Rust to Count Prime Numbers Linearly
This file contains 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::io; | |
fn is_prime(n: i32) -> bool | |
{ | |
for i in 2..n | |
{ | |
let p = n % i; | |
if p == 0 | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
fn main() | |
{ | |
loop | |
{ | |
println!("Find Prime Number From 1 to : "); | |
let mut r = String::new(); | |
io::stdin().read_line(&mut r) | |
.expect("Failed to read line"); | |
let r: i32 = match r.trim().parse() | |
{ | |
Ok(num) => num, | |
Err(_) => continue, | |
}; | |
let mut y: i32 = 0; | |
for x in 2..=r | |
{ | |
if is_prime(x) == true | |
{ | |
y = y + 1; | |
} | |
} | |
println!("There are {} Prime Numbers", y); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment