Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@madhusudan12
Created June 25, 2019 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhusudan12/a18cc5c9052fbc3885e2827b9d9005e1 to your computer and use it in GitHub Desktop.
Save madhusudan12/a18cc5c9052fbc3885e2827b9d9005e1 to your computer and use it in GitHub Desktop.
finds the list of prime numbers from 2..n, given n.
use std::io;
fn find_prime(n:i32) -> Vec<i32>{
let mut prime_list: Vec<i32> = Vec::new();
let mut flag:bool;
for i in 2..n{
flag = false;
for j in 2..(i / 2 + 1){
if i%j == 0 {
flag = true;
break;
}
}
if flag == false {
prime_list.push(i);
}
}
return prime_list;
}
fn main(){
let mut buff = String::new();
io::stdin().read_line(&mut buff).expect("Read line error");
let n:i32 = buff.trim().parse::<i32>().unwrap();
let result = find_prime(n);
for val in &result{
print!("{} ", val);
}
println!();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment