simplification of (i^j)%k

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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.







share|cite|improve this question





















  • What is '^' operator?
    – user58697
    Jul 16 at 21:26










  • That's a power operation.
    – eftshift0
    Jul 16 at 21:26










  • which language are you using?
    – gd1035
    Jul 16 at 21:27











  • I'm working on python.
    – eftshift0
    Jul 16 at 21:31






  • 2




    Perhaps the built in Python function pow could be of use, it takes a third argument which is used to compute modulo: docs.python.org/3/library/functions.html#pow
    – gd1035
    Jul 16 at 21:49














up vote
0
down vote

favorite












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.







share|cite|improve this question





















  • What is '^' operator?
    – user58697
    Jul 16 at 21:26










  • That's a power operation.
    – eftshift0
    Jul 16 at 21:26










  • which language are you using?
    – gd1035
    Jul 16 at 21:27











  • I'm working on python.
    – eftshift0
    Jul 16 at 21:31






  • 2




    Perhaps the built in Python function pow could be of use, it takes a third argument which is used to compute modulo: docs.python.org/3/library/functions.html#pow
    – gd1035
    Jul 16 at 21:49












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.







share|cite|improve this question













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.









share|cite|improve this question












share|cite|improve this question




share|cite|improve this question








edited Jul 16 at 22:12
























asked Jul 16 at 21:24









eftshift0

1215




1215











  • What is '^' operator?
    – user58697
    Jul 16 at 21:26










  • That's a power operation.
    – eftshift0
    Jul 16 at 21:26










  • which language are you using?
    – gd1035
    Jul 16 at 21:27











  • I'm working on python.
    – eftshift0
    Jul 16 at 21:31






  • 2




    Perhaps the built in Python function pow could be of use, it takes a third argument which is used to compute modulo: docs.python.org/3/library/functions.html#pow
    – gd1035
    Jul 16 at 21:49
















  • What is '^' operator?
    – user58697
    Jul 16 at 21:26










  • That's a power operation.
    – eftshift0
    Jul 16 at 21:26










  • which language are you using?
    – gd1035
    Jul 16 at 21:27











  • I'm working on python.
    – eftshift0
    Jul 16 at 21:31






  • 2




    Perhaps the built in Python function pow could be of use, it takes a third argument which is used to compute modulo: docs.python.org/3/library/functions.html#pow
    – gd1035
    Jul 16 at 21:49















What is '^' operator?
– user58697
Jul 16 at 21:26




What is '^' operator?
– user58697
Jul 16 at 21:26












That's a power operation.
– eftshift0
Jul 16 at 21:26




That's a power operation.
– eftshift0
Jul 16 at 21:26












which language are you using?
– gd1035
Jul 16 at 21:27





which language are you using?
– gd1035
Jul 16 at 21:27













I'm working on python.
– eftshift0
Jul 16 at 21:31




I'm working on python.
– eftshift0
Jul 16 at 21:31




2




2




Perhaps the built in Python function pow could be of use, it takes a third argument which is used to compute modulo: docs.python.org/3/library/functions.html#pow
– gd1035
Jul 16 at 21:49




Perhaps the built in Python function pow could be of use, it takes a third argument which is used to compute modulo: docs.python.org/3/library/functions.html#pow
– gd1035
Jul 16 at 21:49










1 Answer
1






active

oldest

votes

















up vote
2
down vote













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 = ((amod n)cdot(bmod n)mod n)$



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






share|cite|improve this answer





















  • Thanks! But I'd like to get rid of the exponentiation if possible.
    – eftshift0
    Jul 16 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 at 22:27










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "69"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2853871%2fsimplification-of-ijk%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













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 = ((amod n)cdot(bmod n)mod n)$



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






share|cite|improve this answer





















  • Thanks! But I'd like to get rid of the exponentiation if possible.
    – eftshift0
    Jul 16 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 at 22:27














up vote
2
down vote













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 = ((amod n)cdot(bmod n)mod n)$



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






share|cite|improve this answer





















  • Thanks! But I'd like to get rid of the exponentiation if possible.
    – eftshift0
    Jul 16 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 at 22:27












up vote
2
down vote










up vote
2
down vote









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 = ((amod n)cdot(bmod n)mod n)$



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






share|cite|improve this answer













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 = ((amod n)cdot(bmod n)mod n)$



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







share|cite|improve this answer













share|cite|improve this answer



share|cite|improve this answer











answered Jul 16 at 22:01









gd1035

29319




29319











  • Thanks! But I'd like to get rid of the exponentiation if possible.
    – eftshift0
    Jul 16 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 at 22:27
















  • Thanks! But I'd like to get rid of the exponentiation if possible.
    – eftshift0
    Jul 16 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 at 22:27















Thanks! But I'd like to get rid of the exponentiation if possible.
– eftshift0
Jul 16 at 22:05




Thanks! But I'd like to get rid of the exponentiation if possible.
– eftshift0
Jul 16 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 at 22:27




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 at 22:27












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2853871%2fsimplification-of-ijk%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What is the equation of a 3D cone with generalised tilt?

Color the edges and diagonals of a regular polygon

Relationship between determinant of matrix and determinant of adjoint?