summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--prime/Cargo.toml6
-rw-r--r--prime/src/main.rs34
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