[Python-Dev] SIGCHECK() in longobject.c
Mark Dickinson
dickinsm at gmail.com
Mon Oct 19 13:27:04 CEST 2009
On Sun, Oct 18, 2009 at 9:01 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Can we remove this check, or are there people doing million-digits calculations
> they want to interrupt using Control-C ?
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:
>>> import math
>>> e = 43112609
>>> p = 2**e - 1
>>> ndigits = int(math.ceil(math.log10(p)))
>>> last_100_digits = '{:0100d}'.format(p % 10**100)
>>> first_100_digits = str(p // 10**(ndigits-100))
Getting the first 100 digits takes a good few seconds; the
other operations are faster.
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.
(And yes, there are better ways to get both the first
and last hundred digits.)
Mark
[1] Might have been, but wasn't. Hence the *almost*. If I'd been
teaching that course this semester I'd almost certainly have included
something like this.
More information about the Python-Dev
mailing list