From 139f7655d28a9447a5d2e133e2979ec654134d7a Mon Sep 17 00:00:00 2001 From: Friedrich Beckmann Date: Sun, 1 Jun 2025 16:33:55 +0200 Subject: add prime on host project --- prime/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 prime/src/main.rs (limited to 'prime/src/main.rs') 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 -- cgit v1.2.3