Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'value,': 0.04; '"if': 0.09; 'bits': 0.09; 'latter': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"""return': 0.16; 'boolean': 0.16; 'btw': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hint': 0.16; 'zero.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'things.': 0.19; '(the': 0.22; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'tells': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'nearly': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "we'd": 0.29; 'am,': 0.29; "doesn't": 0.30; 'especially': 0.30; 'message- id:@mail.gmail.com': 0.30; 'work.': 0.31; 'getting': 0.31; 'away.': 0.31; 'factor': 0.31; 'sep': 0.31; "they'll": 0.31; 'yourself.': 0.31; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'found.': 0.36; 'returning': 0.36; 'useful': 0.36; 'rather': 0.38; 'that,': 0.38; 'aside': 0.39; 'how': 0.40; 'tell': 0.60; 'break': 0.61; 'times': 0.62; 'information': 0.63; 'telling': 0.64; 'more': 0.64; 'teach': 0.65; 'effectively': 0.66; 'truth': 0.81; 'cost,': 0.84; 'to:none': 0.92; 'factors': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=8kxqN8AbITVooyL9fz32O8V9aUgmmqu+3DEArQgh8vs=; b=sgqXVc4s9qtM4L38GogN3DCymfl1P+WPZx3uqdZkm3LT97+uZ/RYa8n6fzFmtPdwMt tdX4vneKIl8nfx2cZyxZVvdFvpUY+rp7PCFVPVDDrieDeMQdATAqZsmen1Fs2eG64x/W jl4IPaB0BvO8fgsiitZEPX3AnPeB5jWB9mrftWkLuAyH3+xMfh82oJrD90VwCXpRePWA QOjgS/NTjysptIgLFs3qi8liZcQQhjZI+Jy/OdSXOFLj3cy7d+29diHV7PRIcf4BIg3m XqC8oICbbOk6dK6F5svNH8rEBT/muT5cGwMa8YaqWXr21TbVsNxFodDlZwAGfKHf7bga 62Sg== MIME-Version: 1.0 X-Received: by 10.50.176.169 with SMTP id cj9mr8954434igc.14.1409960688291; Fri, 05 Sep 2014 16:44:48 -0700 (PDT) In-Reply-To: References: <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> Date: Sat, 6 Sep 2014 09:44:48 +1000 Subject: Re: My backwards logic From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409960691 news.xs4all.nl 2922 [2001:888:2000:d::a6]:58192 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77626 On Sat, Sep 6, 2014 at 3:44 AM, Seymore4Head wrote: > BTW since I am getting no grade, I much prefer the answer than a hint. > The best hint IMO is to tell me how you would do it. But for your own learning, it's still better for you to do things yourself. Giving you the answer doesn't teach you nearly as effectively as giving you a hint and having you work out the answer yourself. But telling you how we'd do it can be useful too, especially when we get down to the detaily bits where there are lots of ways to do things. For instance, there's been a suggestion made a few times to break the primality test out into a function... but compare these two: def isprime(n): """Return True iff n is prime""" def find_factor(n): """Return a factor of n, or None if n is prime""" Aside from inverting the truth value, these are both tests of primality - you can write "if isprime(n):" or "if find_factor(n):" and they'll both work. (The latter is actually "iscomposite".) But the second function is giving you a bit more information - it tells you what factor it found. As a general rule, try to avoid throwing information away. To prove that n is composite, your function found a factor. Returning that, rather than a boolean value, might make your function more useful; and it's zero cost, because you know that factors of a number will never be zero. ChrisA