modInverse & powMod

Salih Dincer salihdb at hotmail.com
Fri Jan 17 12:57:16 UTC 2025


On Saturday, 8 June 2024 at 12:28:43 UTC, Salih Dincer wrote:
> 
> Is PR required? Why not modInverse too!

The 
[std.bigint](https://github.com/dlang/phobos/blob/v2.109.1/std/bigint.d) module was prepared very harmoniously with its backend, but it seems to have been removed to the dusty pages of history. It isn't even magic touches made. The following is one of them:

```d
// ref. std.internal.math.biguintcore:
auto pow(T)(T e)
   => BigUint.pow(data, absUnsign(e)); /* or
   => data ^^ e; //*/
```

I just realized this. In fact, it has a lot of benefits. Because 
what you're trying to implement will work on all types. For 
example, let me try to illustrate with primeMersenne():

```d
auto primeMersenne(T)(T e)
{
   auto res = T(2);
   auto exp = cast(int)e;

   static if (__traits(isIntegral, T))
     import std.math : pow;

   if (auto result = res.pow(exp))
     return result - 1;
   assert(0, "Overflow number");
}
```

Thanks...

SDB at 79




More information about the Digitalmars-d-learn mailing list