Python prime numbers

Larry Martell larry.martell at gmail.com
Sat Feb 1 10:50:53 EST 2014


On Saturday, February 1, 2014, Panagiotis Anastasiou <panast24 at gmail.com>
wrote:

> 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
>
> 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
>


If you put a comma at the end of the print statement it will suppress the
newline.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140201/8fb10447/attachment.html>


More information about the Python-list mailing list