Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can WolframAlpha exponentiate numbers so quickly?

I was wondering how RSA algorithm deals with such big numbers and tried one example in WolframAlpha. How can they deal with such crazy numbers?

EDIT: Just to make it more bizarre, one more example

like image 459
good_evening Avatar asked Oct 14 '13 00:10

good_evening


People also ask

Is WolframAlpha accurate?

When you need an exact answer, Wolfram Alpha is efficient, accurate, and reliable –- as long as the answer can be found in one of its databases and the question is asked correctly.

What is Wolfram Alpha coded in?

As a result, the five million lines of Mathematica code that make up Wolfram|Alpha are equivalent to many tens of millions of lines of code in a lower-level language like C, Java, or Python. Mathematica is a very tall starting point from which to begin building Wolfram|Alpha (or anything else, for that matter).

Why is Wolfram Alpha called Wolfram Alpha?

It's Wolfram|Alpha, named after its creator, Stephen Wolfram, a 49-year-old former particle physics prodigy who became bewitched by the potential of computers.

How do you type exponents in Wolfram Alpha?

Exponential functions can be entered as ee x: Copy to clipboard.


1 Answers

There's a simple algorithm called exponentiation by squaring that can be used to compute ab mod c very efficiently. It's based on the observation that

a2k mod c = (ak)2 mod c

a2k + 1 mod c = a · (ak)2 mod c

Given this, you can compute ab mod c with this recursive approach:

function raiseModPower(a, b, c):
    if b == 0 return 1
    let d = raiseModPower(a, floor(b/2), c)
    if b mod 2 = 1:
        return d * d * a mod c
    else
        return d * d mod c

This does only O(log b) multiplications, each of which can't have any more digits in them than O(log c), so it's really fast. This is how RSA implementations raise things to powers as well. You can rewrite this to be iterative if you'd like, though I think the recursive presentation is really clean.

Once you have this algorithm, you can use standard techniques for multiplying arbitrary-precision numbers to do the computation. Since only O(log b) iterations of the multiplication are required (as opposed to, say, b iterations), it's crazy fast. You never actually end up computing ab and then modding it by c, which also keeps the number of digits low and makes it even faster.

Hope this helps!

like image 143
templatetypedef Avatar answered Nov 15 '22 21:11

templatetypedef