Ian Kelly wrote: > On Fri, May 1, 2015 at 2:27 AM, Steven D'Aprano > <steve+comp.lang.pyt...@pearwood.info> wrote: >> Rather than 10**7, how about trying (10**500 + 2). Is it happy? >> >> Using the Python code from Wikipedia: >> https://en.wikipedia.org/wiki/Happy_number >> >> SQUARE = dict([(c, int(c)**2) for c in "0123456789"]) >> def is_happy(n): >> while (n > 1) and (n != 4): >> n = sum(SQUARE[d] for d in str(n)) >> return n == 1 >> >> >> I can calculate whether n=10**500 + 2 is happy in less than a millisecond >> on my computer. > > Not really the most exciting example, since the following number in > the sequence would be 5. But a random sequence of 500 non-zero digits > doesn't seem to take substantially longer.
I may be missing something, but isn't the '+ 2' in '10**500 + 2' just a distraction? 10**500 would bring across nicely what the approach Steven took from Wikipeda can do in one iteration where Jon's cannot even find enough memory. Also, with 500 digits and 0 <= d <= 9 sum(d*d for d in digits) <= 81 * 500 should hold, i. e. on the second iteration the maximum number of digits is already down to five in the worst case. By repeating this simple reasoning you can tell that all interesting examples must be below 1000 as num_digits(3*81) == 3. A computer that cannot calculate a lookup table with all 3-digit cases should be almost as hard to find as the one needed by Jon ;) -- https://mail.python.org/mailman/listinfo/python-list