Skip to content

Commit

Permalink
migrate back to num_bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
ognevny committed Oct 18, 2023
1 parent 819578a commit e81a5a0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 121 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ jobs:
update: true
msystem: ${{ matrix.sys }}
location: 'D:\M'
pacboy: >-
rust:p
autotools:p
pacboy: rust:p
- name: hack rustup
run: |
rustup toolchain link msys2 D:\M\msys64\${{ matrix.prefix }}
Expand Down
119 changes: 32 additions & 87 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ognlib"
version = "0.4.10"
version = "0.4.11"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Some code that is written to practice Rust"
Expand All @@ -11,17 +11,9 @@ rust-version = "1.65.0"

[dependencies]
rayon = "1.7"
num-bigint = "0.4"

[dependencies.rand]
version = "0.8"
default-features = false
features = ["std", "std_rng"]

[dependencies.rug]
version = "1.22"
default-features = false
features = ["integer"]

[dependencies.gmp-mpfr-sys]
version = "~1.6"
default-features = false
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@
# ognlib

ognlib is created to practice Rust (for me), so you won't find any new code, that can be used for your projects. It contains code for operations under number digits, some power algorithms, radix numbers and some algorithms.
## How to build on Windows?
first: this crate DOES NOT compiles for `*-msvc` targets, so use cargo from MSYS2.
#### Installing MSYS2
to cleanly install MSYS2 follow these steps:
- install it from https://msys2.org/ and follow all steps provided
- install all needed dependencies with
```console
$ pacman -S mingw-w64-ucrt-x86_64-rust mingw-w64-ucrt-x86_64-autotools
```
then you can use this crate for your projects.
IMPORTANT: compile-time may take up to 10 minutes (or even more!), so just be patient.
## Contributing
I'm open for your feedback! If you want to improve my code and explain everything that is new for me, feel free to open PR in GitHub repo.
## Why was it created?
Expand Down
21 changes: 21 additions & 0 deletions src/algorithm/extra.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! A list of algorithms that is not categorized currently.
use num_bigint::BigInt;

/// A binary search algorithm (sorted array is requiered).
/// # Examples
///
Expand All @@ -25,3 +27,22 @@ pub fn bin_search<T: Ord>(arr: &[T], targ: T) -> Option<usize> {
}
None
}

/// Factorial of number
/// # Examples
///
/// ```
/// use ognlib::algorithm::extra::factorial;
/// use num_bigint::BigInt;
///
/// let (n1, n2) = (factorial(3), factorial(5));
/// assert_eq!(n1, BigInt::from(6));
/// assert_eq!(n2, BigInt::from(120))
/// ```
pub fn factorial(n: isize) -> BigInt {
match n {
0 | 1 => BigInt::from(1),
_ => BigInt::from(n) * factorial(n - 1),
}
}
11 changes: 3 additions & 8 deletions src/algorithm/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,13 @@ pub fn sqrtest(n: isize) -> Result<PrimeStatus, PrimeStatusError> {
/// ```
pub fn wilson_th(n: isize) -> Result<PrimeStatus, PrimeStatusError> {
use rug::Integer;
use super::extra::factorial;
use num_bigint::BigInt;

if n < 2 {
return Err(PrimeStatusError);
}
fn factorial(n: isize) -> Integer {
match n {
0 | 1 => Integer::from(1),
_ => Integer::from(n) * factorial(n - 1),
}
}
if (factorial(n - 1) % Integer::from(n)) - Integer::from(n) == -1 {
if (factorial(n - 1) % BigInt::from(n)) - BigInt::from(n) == BigInt::from(-1) {
Ok(PrimeStatus::Prime)
} else {
Ok(PrimeStatus::Composite)
Expand Down
4 changes: 2 additions & 2 deletions src/num/digit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ macro_rules! impl_digit {
fn has_digit(mut self, k: u8) -> bool {
while self.as_bool() {
if self % 10 == k as $type {
if self % 10 == k.try_into().unwrap() {
return true;
}
self /= 10;
Expand All @@ -116,4 +116,4 @@ macro_rules! impl_digit {
}
}

impl_digit!(i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);
impl_digit!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);

0 comments on commit e81a5a0

Please sign in to comment.