| Crates.io | rprime |
| lib.rs | rprime |
| version | 1.0.0 |
| created_at | 2022-03-01 17:15:48.605987+00 |
| updated_at | 2022-03-01 17:25:38.575999+00 |
| description | Provides useful functions for prime numbers. |
| homepage | https://github.com/iamtheblackunicorn/rprime |
| repository | https://github.com/iamtheblackunicorn/rprime |
| max_upload_size | |
| id | 541664 |
| size | 8,212 |
Fun with prime numbers in Rust.
Since I've been digging into prime numbers a lot lately, I was wondering how to write a tool that lets you check whether a number is a prime number. rprime is that tool.
You will need the following tools installed and available:
To compile rprime, follow these steps:
$ git clone https://github.com/iamtheblackunicorn/rprime.git
$ cd rprime
$ cargo build --release
Move the executable on the path rprime/target/release/rprime to the directory where you keep your binary executables. If you are on Linux or Mac OSX, you might have to change permissions like this: chmod a+x rprime. If you have Rust's package manager installed, running cargo install rprime from a terminal window should also install RPrime.
From an algorithmic point of view, RPrime is very simple. It first finds all factors of a given number and dumps these factors into a list. Finally, it checks whether this list's only factors are 1 and the number itself. Depending on whether this is the case a boolean to that effect is returned.
Using rprime is quite simple:
i is short for is_prime. Returns true in this case.$ rprime i 23
true
false in this case.$ rprime i 28
false
n is short for next.$ rprime n 24
29
To use RPrime from your Rust code, add this line to your project's Cargo.toml:
[dependencies]
rprime = "*"
Finally, use RPrime's functions like this:
use rprime::rprime::*;
is_prime: Returns true or false after checking whether a number of type i128 is a prime number.next_prime: Returns the next biggest prime as a number of type i128 after a number of type i128.number_factors: Returns a vector of numbers of type i128 of all factors of a number of type i128.