summaryrefslogtreecommitdiff
path: root/prime/src/main.rs
blob: 51b84bd553ce17b90e3d34df74635d83bfff3678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//! # Host Prime Seach
//!
//! Compute the next smaller prime
//!

use std::time::Instant;

fn is_prime (a : u128) -> bool {
    if a % 2 == 0 {return false};
    for i in (3..a.isqrt()).step_by(2) {
        if a % i == 0 {
            return false;
        }
    }
    return true;
}

fn find_next_smaller_prime(mut x : u128) -> u128 {
    if x <= 1 {return x};
    if x % 2 == 0 {x=x-1};
    while !is_prime(x) {x = x - 2};
    return x;
}

fn main() {
    let n = 1<<56;
    println!("What is the next prime number smaller than {}?", n);
    let now = Instant::now();
    let p = find_next_smaller_prime(n);
    let duration = now.elapsed();
    println!("Found: {}. This took {:?}.", p, duration);
}

// End of file