diff options
author | Friedrich Beckmann <friedrich.beckmann@gmx.de> | 2025-06-01 16:33:55 +0200 |
---|---|---|
committer | Friedrich Beckmann <friedrich.beckmann@gmx.de> | 2025-06-01 16:33:55 +0200 |
commit | 139f7655d28a9447a5d2e133e2979ec654134d7a (patch) | |
tree | 0a1e6c5e8891c2ae83c087cd86fc4f0b6bbd72c1 /prime | |
parent | 938be013365a9151557b9c4c133d61e697b3a5cc (diff) |
add prime on host project
Diffstat (limited to 'prime')
-rw-r--r-- | prime/Cargo.toml | 6 | ||||
-rw-r--r-- | prime/src/main.rs | 34 |
2 files changed, 40 insertions, 0 deletions
diff --git a/prime/Cargo.toml b/prime/Cargo.toml new file mode 100644 index 0000000..799d87b --- /dev/null +++ b/prime/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "prime" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/prime/src/main.rs b/prime/src/main.rs new file mode 100644 index 0000000..51b84bd --- /dev/null +++ b/prime/src/main.rs @@ -0,0 +1,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
\ No newline at end of file |