Collatz Conjeture in Rust
As a test of Rust language I've done a simple Rust checker from 1 to 10.000.000 it takes 18 seconds in my MacBook.
Sample output (after 18 seconds):
10000000 numbers checked in 18 seconds (555555 numbers per second)
use std::process::exit;
use std::time::Instant;
fn main() {
let mut i=1;
let start = Instant::now();
while i<=10000000
{
let res=collatz(i);
if res
{
i=i+1;
}
else
{
println!("{} NO!!!", i);
exit(1);
}
}
let elapsed=start.elapsed().as_secs();
println!("{} numbers checked in {} seconds ({} numbers per second)", i-1, elapsed, (i-1)/elapsed);
}
fn collatz(i: u64) -> bool
{
let mut res;
res=false;
if i==1
{
res=true
}
else
{
if i%2==0
{// par
res=collatz(i/2);
}
else
{// impar
res=collatz((i*3)+1);
}
}
res
}
No hay comentarios:
Publicar un comentario