[Tutor] in-built square root function

Norvell Spearman norvell@houseofspearman.org
Thu Feb 20 01:22:01 2003


On Thursday, 2003.02.20, 00:54:33 -0500, Timothy M. Brauch wrote:
> So, yes, it seems that in the first case, it works out (45**(89+1))%3, as I
> expected.  The second case works out as ((45**89)+1)%3, as powers take
> precedence over addition.  But, because you are working modulo,
> (45**89+1)%3, meaning ((45**89)+1)%3 is actually the same thing as
> (45**89)%3 + 1, which works as...
> 
> >>> pow(45,89,3)+1
> 1
> >>>

So you're saying that (x + 1)%y is the same as (x%y) + 1? or just for
the one instance with the example values I gave?  Because in the
interpreter I get

>>> pow(45, 89, 3) + 1
1
>>> (45**89)%3 + 1
1L
>>> ((45**89) + 1)%3
1L

but I also get

>>> (3%2) + 1
2
>>> (3 + 1)%2
0

which is one example showing that (x%y) + 1 is not necessarily equal to
(x + 1)%y.

That's why I was curious about buit-in pow's z argument:  Yes, it may be
more efficient for long modulo division but only for certain dividends
(namely, those which can be expressed as an integer base to an integer
power, per the Python Library Reference).  If I'm being dense and
missing something really obvious then I sincerely apologize.

-- 
Norvell Spearman