init
This commit is contained in:
commit
4d3d09ba97
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
45
.vscode/launch.json
vendored
Normal file
45
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'isprime'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--bin=isprime",
|
||||
"--package=isprime"
|
||||
],
|
||||
"filter": {
|
||||
"name": "isprime",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'isprime'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--bin=isprime",
|
||||
"--package=isprime"
|
||||
],
|
||||
"filter": {
|
||||
"name": "isprime",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
46
Cargo.lock
generated
Normal file
46
Cargo.lock
generated
Normal file
@ -0,0 +1,46 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "isprime"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "isprime"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
num-bigint = "0.4.3"
|
45
src/main.rs
Normal file
45
src/main.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use std::io::{self, Write};
|
||||
use num_bigint::BigUint;
|
||||
|
||||
fn log_2(x: BigUint) -> u64 {
|
||||
x.bits() - 1
|
||||
}
|
||||
|
||||
fn is_prime(number: BigUint) -> bool {
|
||||
let a = log_2(number.clone()+1u8);
|
||||
if BigUint::from(2u8).pow(a as u32)-BigUint::from(1u8) != number {
|
||||
eprintln!("only supports numbers of type (2^x)-1");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let mut last = BigUint::from(4u8)%number.clone();
|
||||
let two = BigUint::from(2u8);
|
||||
for _i in 2..a {
|
||||
last = (last.clone()*last-two.clone())%number.clone();
|
||||
}
|
||||
if last == BigUint::from(0u8) {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buffer = String::new();
|
||||
let stdin = io::stdin();
|
||||
print!("please enter the number to check: ");
|
||||
io::stdout().flush().expect("could not flush");
|
||||
stdin.read_line(&mut buffer).expect("could not read from stdin");
|
||||
buffer = buffer.trim().to_owned();
|
||||
|
||||
let number = BigUint::parse_bytes(&buffer.as_bytes(), 10).expect("expected number");
|
||||
|
||||
// number = 2^x - 1
|
||||
// x = log2(number + 1)
|
||||
|
||||
if is_prime(number.clone()) {
|
||||
println!("{number} is a prime");
|
||||
} else {
|
||||
println!("{number} is not a prime");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user