## Function Build – My version of PowerMod collapses at 10 ^ 308

I tried to write a function that duplicated `PowerMod[a, b, n]`count `a ^ b` mod `n`, I am currently testing `3 ^ x mod 353` and different `x`, I've found that my results match those of the built-in feature until I strike `x = 10 ^ (308)`, At this point, I get a recursion error. Is there a reason that Mathematica's built-in functions still work on these values?

``````pmod[a_, b_, mod_] : =
module[{l, z, binarylist = IntegerDigits[b, 2]val = 1}
l = length[binarylist];
clear[z];
z = a;
z[j_] : = z[j] = Mod[z[j - 1]^ 2, mod];
z[l];
Do[
If[binarylist[[Binarylist[[binärliste[[binarylist[[j]]== 1,
val * = z[l - j + 1]; val = mod[val,
mod]],
{j, 1, l}];
val]
``````

I use `l - j + 1` because I want when `j = 1`if binary list[[[[[j]]= 1; val * = z[j]when `j = 2`; want `val * = z[j - 1]`, …, `j = 1, val * = z`, This is a result of Mathematica, lists start at 1 and not at 0.

``````pmod[3, 10^305, 353]
``````

`140`

``````PowerMod[3, 10^305, 353]
``````

`140`

``````pmod[3, 10^308, 353]
``````

\$ RecursionLimit :: reclim2: Recursion depth of 1024 exceeded during the evaluation of mod[z\$50178[4-1]^ 2.353],

`185`

``````PowerMod[3, 10^308, 353]
``````

`58`

### To edit

I thought that might be because 10 ^ 308 exceeds 2 ^ 1024, but my calculations show that this happens at 10 ^ 309. If I use 2 ^ 1023 (I do not understand why I would do it, but I may think that too much), that explains the mistake.

Posted on Categories Articles

## Built-in symbols – PowerMod is not understood

By definition PowerMod[a, 1/r, m] finds a modular rth root of a mod m.
Here are some examples to illustrate my problem in 11.1.1 / Win 10.

``````PowerMod[2488, 1/3, 10^4]
8992
8992 ^ 3
727057727488
Mod[PowerMod[PowerMod[PowerMod[PowerMod[2488, 1/3, 10^4]^ 3, 10 ^ 4]== 2488
Not correct
``````

It may be relevant that the value returned above (8992) is not included in the corresponding PowerModList values:

``````PowerModList[2488, 1/3, 10^4]
{242, 2742, 5242, 7742}
Mod[PowerModList[PowerModList[PowerModList[PowerModList[2488, 1/3, 10^4]^ 3, 10 ^ 4]{2488, 2488, 2488, 2488}
``````

Likewise (if not identical) for

``````PowerMod[91175, 1/3, 10^5]
84375
84375 ^ 3
600677490234375
Mod[PowerMod[PowerMod[PowerMod[PowerMod[91175, 1/3, 10^5]^ 3, 10 ^ 5]== 91175
Not correct
``````

And in contrast to the first example

``````PowerModList[91175, 1/3, 10^5]
{}
``````

In both cases PowerModList seems to be correct (PowerModList)[91175, 1/3, 10^5] should not give anything back and does not).

Very obliged to look at this.

Posted on Categories Articles

## Error – PowerMod code is not running

```````` powermod[b_Integer /; PositiveQ[b]e_Integer /; PositiveQ[e],
m_Integer /; PositiveQ[m]]: = Module[{n, bl, el},
n = 1;
bl = b;
el = e;
While[el != 0, If[Mod[el, 2] == 1, n = mod[n*bl, m];
];
el = floor[el/2];
bl = Mod[bl*bl, m];
];
n]``
``````

When I run examples like Powermodel[7,41,34] The output is the same as the input, so I would like to know where the error lies.

I also follow this pseudo-code:

Power Model[(b,e,m)]

$$n ← 1$$

while $$e! = 0$$ do

If $$e$$ $$mod$$ $$2$$ $$=$$ $$1$$ then

$$n ← (n * b)$$ $$mod$$ $$m$$

when

$$e ← floor[e/2]$$

$$b$$$$(b * b)$$ $$mod$$ $$m$$

during the end

return $$n$$