# Parallel PowerMod

Is there anyway to parallelize the PowerMod function?

Here is my Left-To-Right modular exponentation:

AfshinPowerMod[a_, b_, m_] := (Output = 1;
Do[If[n == 1, Output = Mod[Output*Output*a, m],
Output = PowerMod[Output, 2, m]], {n, IntegerDigits[b, 2]}];
Return[Output])


It computes roughly in the same time as original PowerMod for 10K digits numbers.

Update

(Since for prime testing, 2 is the lowest base, I've used it for faster computation)

Timing Result (Mathematica 8):

In[1]:= Total[DigitCount[2^100000 + 1]]

Out[1]= 30103

In[2]:= AbsoluteTiming[AfshinPowerMod[2, 2^100000 + 1, 2^100000 + 1]]

Out[2]= {91.839778,}

In[3]:= AbsoluteTiming[PowerMod[2, 2^100000 + 1, 2^100000 + 1]]

Out[3]= {92.9851312,}


As per J.M. request for RandomPrime (Based on the generated random prime timing differs, but yet it is roughly as fast as PowerMod)

In[41]:= prime = RandomPrime[10^2000]

In[43]:= AbsoluteTiming[PowerMod[2, prime, prime]]

Out[43]= {0.1406312, 2}

In[42]:= AbsoluteTiming[AfshinPowerMod[2, prime, prime]]

Out[42]= {0.1562399, 2}

-
Why would you need to do this? Why is the built-in version unsuitable? –  Ｊ. Ｍ. Nov 22 '12 at 16:38
Time. It take hours to compute PowerMod for 100K numbers while my cores are idle. I know I can distribute multiple numbers to multiple cores but I want to split PowerMod for a single number to decrease computing time. –  Mohsen Afshin Nov 22 '12 at 16:42
If it's a matter of speed when you have 100K numbers, why do you need to split each number into cores? Wouldn't be equally efficient to just distribute the 100K numbers among the kernels? –  Rojo Nov 22 '12 at 18:58
Just use ParallelMap or ParallelTable to run PowerMod on your 100k numbers over all your cores. –  s0rce Nov 22 '12 at 19:10
I cannot reproduce @Mohsen's statement that AfshinPowerMod "computes roughly in the same time as original PowerMod for 10K digits numbers." My timings have the built-in PowerMod orders of magnitude faster for input integers $a$ and $b$ tested with up to 25 thousand digits... –  KennyColnago Nov 23 '12 at 0:23
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.28626611828804016, "perplexity": 9138.68878949773}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-11/segments/1424936460576.24/warc/CC-MAIN-20150226074100-00254-ip-10-28-5-156.ec2.internal.warc.gz"}