Created
June 25, 2019 15:02
-
-
Save madhusudan12/a18cc5c9052fbc3885e2827b9d9005e1 to your computer and use it in GitHub Desktop.
finds the list of prime numbers from 2..n, given n.
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 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