Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65240

Re:Python prime numbers

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'output': 0.05; 'subject:Python': 0.06; 'assignment': 0.07; 'column': 0.07; 'string': 0.09; 'function,': 0.09; 'newline': 0.09; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'suppress': 0.09; 'trailing': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'carefully.': 0.16; 'columns': 0.16; 'comma': 0.16; 'compute': 0.16; 'count.': 0.16; 'formatted': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'redundant': 0.16; 'true:': 0.16; 'pointed': 0.19; 'properly': 0.19; 'programming': 0.22; 'print': 0.22; 'header:X-Complaints-To:1': 0.27; 'bigger': 0.30; "i'm": 0.30; 'code': 0.31; 'aligned': 0.31; 'implied': 0.31; 'subject:numbers': 0.31; 'checking': 0.33; 'maybe': 0.34; "i'd": 0.34; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'complete.': 0.36; 'false': 0.36; 'wrong': 0.37; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'break': 0.61; 'new': 0.61; 'numbers': 0.61; 'first': 0.61; 'back': 0.62; 'prime': 0.74; 'subject::': 0.85
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re:Python prime numbers
Date Sat, 1 Feb 2014 17:07:56 -0500 (EST)
Organization news.gmane.org
References <158634a8-3c20-4e04-bc2a-a562c7270a3b@googlegroups.com>
X-Gmane-NNTP-Posting-Host dpc6744192077.direcpc.com
X-Newsreader PiaoHong Usenet NewsReaders 1.36
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6291.1391292302.18130.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391292302 news.xs4all.nl 2841 [2001:888:2000:d::a6]:58439
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65240

Show key headers only | View raw


 Panagiotis Anastasiou <panast24@gmail.com> Wrote in message:
> Hi i'm new in programming and in python and i have an assignment that i cant complete. I have to Write a Python program to compute and print the first 200 prime numbers. The output must be formatted with a title and the prime numbers must be printed in 5 properly aligned columns . I have used this code so far :
> 
> numprimes = raw_input('Prime Numbers  ')
> count = 0
> potentialprime = 2
> 
> def primetest(potentialprime):
>     divisor = 2
>     while divisor <= potentialprime:
>         if potentialprime == 2:
>             return True
>         elif potentialprime % divisor == 0:
>             return False
>             break
>         while potentialprime % divisor != 0:
>             if potentialprime - divisor > 1:
>                 divisor += 1
>             else:
>                 return True
> 

There are several things wrong with this function,  and it's
 redundant enough that maybe none of them matter.  I'd test it
 carefully. 

> while count < int(numprimes):
>     if primetest(potentialprime) == True:
>         print potentialprime
>         count += 1
>         potentialprime += 1
>     else:
>         potentialprime += 1
> 
> but i get the result in a single column . How can i get it in 5 rows? Can someone help please
> 

As has been pointed out,  you can use a trailing comma to suppress
 the implied newline for each print. 
Then you can add it back in every five items by checking count.

But you have a bigger problem,  lining up the columns.  Try using
 the string modulus operator, or 'format'.

-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python prime numbers Panagiotis Anastasiou <panast24@gmail.com> - 2014-02-01 07:33 -0800
  Re: Python prime numbers Larry Martell <larry.martell@gmail.com> - 2014-02-01 10:50 -0500
  Re:Python prime numbers Dave Angel <davea@davea.name> - 2014-02-01 16:59 -0500
  Re:Python prime numbers Dave Angel <davea@davea.name> - 2014-02-01 17:07 -0500
  Re: Python prime numbers Wiktor <look@signature.invalid> - 2014-02-02 00:34 +0100

csiph-web