Bug 6244 – Add powmod / modpow function to std.math

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-07-03T15:10:08Z
Last change time
2017-12-18T22:55:06Z
Assigned to
alex.jercaianu
Creator
[email protected]

Comments

Comment #0 by josvanuden — 2011-07-03T15:10:08Z
I think a powmod function should be part of std.math. It's important for many types of algorithms for avoiding overflow and it's not something that the average person can write off the top of their head. Currently I'm using a powmod code that I found on the internet. I'm not even sure what the licensing terms of it are.
Comment #1 by josvanuden — 2015-09-28T19:29:07Z
ulong powmod(ulong b, ulong e, ulong m) { ulong r = 1; for (; e > 0; e >>= 1) { if (e & 1) { r = (r * b) % m; } b = (b * b) % m; } return r; }
Comment #2 by github-bugzilla — 2017-10-16T17:46:17Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/7781aaa7b1b50858c459372b291fd80092f5ef18 Fix Issue 6244 - Add powmod / modpow function to std.math addmod for powmod powm refactor supports unsigned added more tests specify imported symbols spaces around casts review comments https://github.com/dlang/phobos/commit/24cc67c1576c8da291d5812bc31279d8e871bdf2 Merge pull request #5761 from jercaianu/powmod Fix Issue 6244 - Add powmod / modpow function to std.math merged-on-behalf-of: Andrei Alexandrescu <[email protected]>
Comment #3 by github-bugzilla — 2017-12-18T22:55:06Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/7781aaa7b1b50858c459372b291fd80092f5ef18 Fix Issue 6244 - Add powmod / modpow function to std.math https://github.com/dlang/phobos/commit/24cc67c1576c8da291d5812bc31279d8e871bdf2 Merge pull request #5761 from jercaianu/powmod