Comments (7)
#56 is similar to this.
from num.
Why not just use num::pow
?
For modular exponentiation, here is a working implementation:
/// Calculate base^exp (mod modulus).
fn powm(base: &BigInt, exp: &BigInt, modulus: &BigInt) -> BigInt {
let zero = Integer::zero();
let one: BigInt = Integer::one();
let two = &one + &one;
let mut exp = exp.clone();
let mut result = one.clone();
let mut base = base % modulus;
if exp < zero {
exp = -exp;
base = inverse(base, modulus).unwrap();
}
while exp > zero {
if &exp % &two == one {
result = (result * &base) % modulus;
}
exp = exp >> 1;
base = (&base * &base) % modulus;
}
result
}
For negative powers more machinery is needed (namely the extended Euclidean algorithm for inverting the base). See here.
from num.
Why not just use num::pow?
My application needs negative and/or non-integer powers (but not modular arithmetic). If I use this powm
implementation, but simply remove the modulus
parameter and drop all … % modulus
, will the result work correctly?
from num.
It sounds like you want big floats? That is not possible with num. You might want to have a look at rust-gmp.
from num.
@nejucomo I think that big fixed and floating point arithmetic is no goal for num
crate. However it can happen that there will be additional library "blessed" by rust-num organization.
from num.
Why not just use num::pow
I think there probably was a good reason back in Jan. 2015, but I don't remember it now.
For modular exponentiation, here is a working implementation:
Can you make a PR with that?
from num.
There is BigUint::modpow
now. If you need more, please open an issue on the new repo:
https://github.com/rust-num/num-bigint/issues
from num.
Related Issues (20)
- Add a function that actually does `abs_sub` for both Signed and Unsign numbers.
- Traits for converting from/into bytes HOT 1
- Implement rand traits for Ratio ? HOT 2
- Conversion between openssl::bn::BigNum and num::BigInt HOT 4
- Question: Is this crate official? HOT 3
- Support a more generic binomial coefficients
- Trait size of number in bits
- Human readable (concise) output HOT 1
- Signed::is_positive and Signed::is_negative implementation/documentation is inconsistent
- Extended GCD on BigUint doesn't work HOT 2
- Yank pre-Rust 1.0.0 versions HOT 1
- Bump num-family to v1.0? HOT 6
- conversion from BigUint to Complex and vice versa HOT 1
- `rustc-serialize` is deprecated, uses deprecated Rust features HOT 2
- Invalid Complex formatting for floats (f32 and f64). HOT 3
- New API additions in num-complex 0.4.2 not configurable through num version HOT 2
- NUM
- Curiousness at 1200! (factorial)... ? HOT 2
- Feature: more comparison operations with BigInts HOT 1
- Some overflow panics found by fuzzing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from num.