Created
June 1, 2021 18:41
-
-
Save dilakv/d074f474b7280bac0a8673ee4be5d457 to your computer and use it in GitHub Desktop.
Generate N primes numbers with Rust
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::time::{SystemTime, UNIX_EPOCH}; | |
fn is_prime(num: u128) -> bool | |
{ | |
let mut response: bool = true; | |
for i in (2..(num-1) as usize).step_by(1){ | |
if (num % i as u128) == 0 { | |
response = false; | |
} | |
}; | |
return response; | |
} | |
fn main() { | |
let time_start = (SystemTime::now() | |
.duration_since(UNIX_EPOCH) | |
.expect("Time went backwards")).as_millis(); | |
const SIZEARRAY: usize = 3000; | |
let mut array: [u128; SIZEARRAY] = [0; SIZEARRAY]; | |
let num_inicial = 3; | |
let num_final : u128 = 1e10 as u128; | |
let mut inc: u128= 0; | |
println!("-------------------------------------------"); | |
println!("Se solicitaron {} números primos, desde el {} hasta el {}", SIZEARRAY, num_inicial, num_final); | |
for i in num_inicial..num_final{ | |
if is_prime(i) && inc < SIZEARRAY as u128 { | |
array[inc as usize] = i; | |
inc+=1; | |
continue; | |
} | |
if inc > (SIZEARRAY - 1) as u128 | |
{ | |
break; | |
} | |
} | |
for i in array.iter(){ | |
print!(" - {}", i); | |
} | |
println!(" - "); | |
let time_end = (SystemTime::now() | |
.duration_since(UNIX_EPOCH) | |
.expect("Time went backwards")).as_millis(); | |
println!("-------------------------------------------"); | |
println!("Duracion en milisegundos -> {:?}", time_end-time_start); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment