## Fast power algorithm realization

I need to calculate a quadratic residue. Prime p is 3 mod 4 and is a very big number, about `1e38`

(constexpr). I found only the formula of Lagrange `x = +- a^((p + 1)/4) mod p`

.

I need to calculate powers of big numbers (`a ^ 1e38`

). I was trying to use `boost::multiprecision::cpp_int`

, but seems it has no sense. May be somebody knows a good realization, for such calculations or alternative algorithm.

Your example asked for modular arithmetic modulo a prime. As stated in https://en.wikipedia.org/wiki/Fermat%27s_little_theorem, where p is a prime a^p mod p = a, and if a is not a multiple of p then a^(p-1) = 1 mod p. So if a != 0, then a^x = a^(x mod p-1). If you want something modulo a number not prime, then factor it and use the https://en.wikipedia.org/wiki/Chinese_remainder_theorem.

If you want arithmetic of the reals, use a^x = exp(x * ln(a)) but for large x you will probably need to keep it as a logarithm to avoid overflow.

**Fast Power Algorithm - C++ and Python Implementation,** Implementation � Applications Fast application of a set of geometric operations to a set of points � Number of paths Algorithm. Raising a to the power of n is expressed naively as multiplication by a done n−1 times: an=a⋅a⋅…⋅a. However� Fast power algorithm realization. Ask Question Asked 4 years, 6 months ago. Active 4 months ago. Viewed 813 times 1. I need to calculate a quadratic residue. Prime p

Usually, testing if something is a quadratic residue is done with a Euclidean algorithm-style calculation by invoking via quadratic reciprocity to flip the Jacobi symbols (or Kronecker symbols) whenever the top is smaller than the bottom.

Also, you should pretty much never compute an exponent *then* reduce modulo `p`

: your exponentiation algorithm (e.g. square-and-multiply) should reduce modulo `p`

after every step or so of its calculation. Key phrase: "modular exponentiation".

**Exponentiation by squaring,** Below is the implementation based on above property. C++; C; Java; Python3; C#; PHP. C++. A new fast matrix multiplication algorithm is proposed, which, as compared to the Winograd algorithm, has a lower multiplicative complexity equal to Wm ≈ 0.437n3 multiplication operations.

i think it may help https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/

/* Iterative Function to calculate (x^y) in O(log y) */ int power(int x, unsigned int y) { int res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = res*x; // y must be even now y = y>>1; // y = y/2 x = x*x; // Change x to x^2 } return res; }

**Binary Exponentiation,** Fast addition-chain exponentiation in C++ using templates. Once realizing this, many people will write small utility functions for efficiently the simple recursive algorithm uses more multiplications than the optimal one. The algorithm continuously monitors the speed and the shape of the impedance trajectory and will immediately see the start of a power swing cycle or an out of step condition. This detection also enables the algorithm to detect fast swing cycles with swing frequencies up to 7 Hz.

**Modular Exponentiation (Power in Modular Arithmetic ,** Therefore, power is generally evaluated under modulo of a large number. Below is the fundamental modular property that is used for efficiently computing power under modular arithmetic. (ab) mod p = ( (a mod p) (b mod p) ) mod p For example a = 50, b = 100, p = 13 50 mod 13 = 11 100 mod 13 = 9 (50 * 100) mod 13 = ( (50 mod 13) * (100 mod 13

**Fast computation of integer powers,** algorithm may be considered as the best power allocation algorithm since the performance of the fast algorithm is almost as good as the fullsearch (optimal) algorithm and the mean processing time is considerably less than the fullsearch algorithm. The fast algorithm achieves about 97:6% agreement with the op-timal throughput on average.

A description of the fast powering algorithm, used to evaluate very high powers of very large numbers, taken mod N. For more math, subscribe to my channel: h

##### Comments

- Are you sure you want to raise to
`1e38`

power, and not multiply by`1e38`

? For`x == 2`

, the resulting number would take`1e38`

bits to represent, which is billions of yottabytes, much more than all available storage combined. - I need to calculate powers about 1e38. If it is impossible with cpp_int representation, I need other. Standard calculator in Ubuntu Studio can calculate for example 5×10¹²⁸ mod 251 and give the answer 20 without delay I can feel
- 5 times
`10^128`

is not at all the same thing as 5 raised to`10^128`

power. Which one do you want? - I need to calculate a quadratic residue using formula of Lagrange
`a ^ (n + 1)/4`

- As @IgorTandetnik said, the int representation is insufficient because the whole universe is by far too small to represent all digits of such powers. You are probably thinking of a modular representation, for which ordinary arithmetic can be enough.
- For
`a ^ (n + 1)/4`

if I use prime 251`a^x = a^(x mod p-1)`

gives the same a ^ 63.... Not better. I can't to find other algorithm for quadratic residue. prime is 3 mod 4