Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.067 X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'subject:number': 0.09; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'subject:happy': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'url:wiki': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'subject:skip:i 10': 0.31; 'url:wikipedia': 0.31; 'computer.': 0.33; 'fri,': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'sequence': 0.36; 'url:org': 0.36; 'example,': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'most': 0.60; '500': 0.70; 'square': 0.74; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=kE1ye8Tl+YZqf4gnUSi6y+JX3SoD7TmuL0myXTwHgp8=; b=vmpr0nSUBuGzylaCfdn9BZ63cVa/mNCwLAMQ+uhc9gYvjTZL3c0AepR1+IpgcQjW3N 7HR89rEMfuvOrnl0VJnCZZb+B5xi6JZxt9DLlL7xXhoAy8iqvv/rkHKV8WXaNzt+xPRT I2zeZVifMbHw55AbJeRbjWYk5NfT65btwBa7mRyT+F2FejzSN6PkQj76Jm29D5nFNZVw xg+KjB7nWISjzzlAuCp70l5QEnVfGiGgFiGVjNRLtZ9/6hHExgZAPyPcyb3VU0bTIGii u3738sAw/EkpmQmq1f1DVPEYTAsaDjEm4Z1e4pnlGpiKJ3PesvowbuSiDXtsMFtZp7YQ lAEw== X-Received: by 10.107.17.29 with SMTP id z29mr12891069ioi.69.1430492652477; Fri, 01 May 2015 08:04:12 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <554338dd$0$13004$c3e8da3$5496439d@news.astraweb.com> References: <87oam5vc8k.fsf@Equus.decebal.nl> <554338dd$0$13004$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 1 May 2015 09:03:32 -0600 Subject: Re: Is my implementation of happy number OK To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430492660 news.xs4all.nl 2905 [2001:888:2000:d::a6]:59301 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89740 On Fri, May 1, 2015 at 2:27 AM, Steven D'Aprano 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.