Code Monkey home page Code Monkey logo

Comments (7)

gsingh93 avatar gsingh93 commented on May 28, 2024

#56 is similar to this.

from num.

vks avatar vks commented on May 28, 2024

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.

nejucomo avatar nejucomo commented on May 28, 2024

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.

vks avatar vks commented on May 28, 2024

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.

hauleth avatar hauleth commented on May 28, 2024

@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.

gsingh93 avatar gsingh93 commented on May 28, 2024

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.

cuviper avatar cuviper commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.