prime
This commit is contained in:
parent
bb9a14c6e8
commit
eb5eb41e4a
2 changed files with 73 additions and 0 deletions
8
9-prime_numbers/Cargo.toml
Normal file
8
9-prime_numbers/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "prime_numbers"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
65
9-prime_numbers/src/main.rs
Normal file
65
9-prime_numbers/src/main.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
use std::io;
|
||||
fn get_input()->u32
|
||||
{
|
||||
loop
|
||||
{
|
||||
let mut input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("Failed to read line");
|
||||
match input.trim().parse()
|
||||
{
|
||||
Ok(num) => return num,
|
||||
Err(_) =>
|
||||
{
|
||||
println!("Expected Valid Number");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
fn prime(limit:u32)->i8
|
||||
{
|
||||
if limit < 2
|
||||
{
|
||||
println!("None");
|
||||
return -1;
|
||||
}
|
||||
println!("\t2");
|
||||
if limit == 2
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
let mut b;
|
||||
|
||||
for i in (3..=limit).step_by(2)
|
||||
{
|
||||
b = true;
|
||||
if i%2 != 0
|
||||
{
|
||||
for j in (3..=(i as f64).sqrt()as u32).step_by(2)
|
||||
{
|
||||
if i%j == 0
|
||||
{
|
||||
//println!("{} is even", i);
|
||||
b = false;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if b == true
|
||||
{
|
||||
println!("\t{}", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
fn main()
|
||||
{
|
||||
println!("Hello, world!");
|
||||
|
||||
println!("Limit for Prime = ↓");
|
||||
prime(get_input());
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue