[Python-Dev] SIGCHECK() in longobject.c

Tim Peters tim.peters at gmail.com
Mon Oct 19 22:58:47 CEST 2009

[Mark Dickinson]
> By the way, here's an example of an *almost* real-life use of million digit
> calculations.
> For an elementary number theory course that I taught a while ago, there
> was an associated (optional) computer lab, where the students used
> Python to investigate various ideas, conjectures, examples, etc.  One
> of the less open-ended questions might have been[1] something like:
> "On August 23rd, 2008 a computer at UCLA found the first example
> of a prime with more than 10 million digits: p = 2**43112609-1.  Find
> (1) the exact number of digits in p, when written out in decimal
> (2) the last 100 digits of p
> (3) the first 100 digits of p."
> It's wonderfully easy to get answers to these questions with Python:
> But if you (not unreasonably) try to compute str(p),
> you'll find it's impossibly slow, and it's very handy
> that it's possible to interrupt that calculation, attempt
> to understand *why* it's slow, and then try different
> methods.

Don't want to hijack this thread, but this is the kind of use case
justifying keeping the 3-argument pow in the decimal module.  People
"playing" with number theory questions can learn a bag of tricks to
worm around that non-decimal arithmetic can make it inconvenient &
slow to get answers about decimal digits, but most people -- and
especially beginners -- find life easier when doing calculations in
decimal to begin with.  Then they can use obvious methods, and not get
sidetracked by wondering why they take forever to finish.

Although, to be fair, I started

>>> decimal.getcontext().prec = 20000000
>>> p10 = pow(decimal.Decimal(2), 43112609)

before I started typing this, and am still waiting for it to finish ;-)


>>> decimal.getcontext().prec = 200
>>> pow(decimal.Decimal(2), 43112609)   # get the first 100 digits (& then some)


>>> pow(decimal.Decimal(2), 43112609, 10**100) - 1  # get the last 100 digits

both appear to work instantaneously.

More information about the Python-Dev mailing list