0

I'm writing a program that is spending major amounts of time of CPU doing power operations. I need to get the modulo of $i^j$ by k. Is there any simplification of $i^j$%k that doesn't involve an exponentiation?

Note: I remove the specification about needing to heck divisibility. I actually need to get the modulo so that I can do more things with it.

eftshift0
  • 147
  • What is '^' operator? – user58697 Jul 16 '18 at 21:26
  • That's a power operation. – eftshift0 Jul 16 '18 at 21:26
  • which language are you using? – gd1035 Jul 16 '18 at 21:27
  • I'm working on python. – eftshift0 Jul 16 '18 at 21:31
  • The divisors of $i^j$ are $1, i, i^2, i^3, i^4,... i^j$. – hamam_Abdallah Jul 16 '18 at 21:33
  • Sure... and k might just equal one of those divisors and then i^j%k would be zero... but how can I "foretell" the result of $i^j mod k $ (which might be between 0 and k - 1) in advance without having to resolve $i^j$ in the first place? – eftshift0 Jul 16 '18 at 21:40
  • @Salahamam_Fatima I modified the request because I don't need to check for divisibility. What I really need is the modulo so that I can do more operations over it. – eftshift0 Jul 16 '18 at 21:45
  • @Salahamam_Fatima : That's only true if $i$ is prime. For example, if $i=10$, then $i^2=100$ has more divisors than just $1,10,100$. You're forgetting about what you can get by repeating various factors of $i$ a different number of times. So you also have $2,4,5,25,50$. – MPW Jul 16 '18 at 21:48
  • 2
    Perhaps the built in Python function pow could be of use, it takes a third argument which is used to compute modulo: https://docs.python.org/3/library/functions.html#pow – gd1035 Jul 16 '18 at 21:49
  • Perhaps you could use the fact that $$(a \cdot b)\mod n = ((a\mod n) \cdot (b \mod n) \mod n)$$ – gd1035 Jul 16 '18 at 21:54
  • Ok! Let me see if that makes a difference in terms of perfoemance. Thank you very much! However, I'll leave the question open so that anybody can provide a "generic" response to the question. – eftshift0 Jul 16 '18 at 21:54
  • Possible duplicate: https://math.stackexchange.com/questions/2204627/repeated-squaring-techniques . Check wikipedia: https://en.wikipedia.org/wiki/Exponentiation_by_squaring – Ethan Bolker Jul 16 '18 at 22:17

1 Answers1

2

I will put my two comments in to an answer.

One option is to use the built-in Python function "pow()" and the documentation is found here: https://docs.python.org/3/library/functions.html#pow

Another option is to use the fact that: $(a \cdot b) \mod n = ((a\mod n)\cdot(b\mod n)\mod n)$

So for your case you would have something like: $i^j \mod k = (i \mod k)^j \mod k$

gd1035
  • 598
  • Thanks! But I'd like to get rid of the exponentiation if possible. – eftshift0 Jul 16 '18 at 22:05
  • I just tried using pow with the third parameter and I do see an improvement on performance. Some 2-4 percent. But as I grow the numbers that I'm using for all 3 elements, then the improvement dissipates. – eftshift0 Jul 16 '18 at 22:27