mardi 4 août 2015

Comparing large powers in Ruby using Mod 10

I am trying to write a function that compares whether one number x1 to the power of another number x2 is greater than another number y1 to another number y2 power.

For example, suppose we have a pair of numbers for x.

x = [2,10]

and another pair y

y = [3,9]

In this case y1 ** y2 is greater than x1 ** x2.

However, sometimes there are really large, scary numbers. Like so

x = [935243113, 987702643]
y = [50894069, 704259526]

To calculate this, I tried using the mod 10 algorithm

(x[0] ** x[1]) % 10 > (y[0] ** y[1]) % 10 

However, apparently this algorithm doesn't work. Is there a more efficient algorithm for this.

Aucun commentaire:

Enregistrer un commentaire