Python prime numbers

  • Thread starter Panagiotis Anastasiou
  • Start date
P

Panagiotis Anastasiou

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 first200 prime numbers. The output must be formatted with a title and the primenumbers 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
 
L

Larry Martell

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

Dave Angel

Panagiotis Anastasiou said:
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
 
D

Dave Angel

Panagiotis Anastasiou said:
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'.
 
W

Wiktor

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 :

Hi,
try out this code:

for i in range(200):
print '{0:>5}'.format(i),
if (i-4) % 5 == 0:
print

Or maybe, if it's still unclear, try execute these lines:

print 'Hello {0}'.format('world')
print '|{0:>30}|'.format('right')
print '|{0:<30}|'.format('left')
print '|{0:^30}|'.format('center')
print '|{0:>16}|'.format('right'),
print '|{0:<16}|'.format('left'),
print '|{0:^16}|'.format('center')

But still, it might be hard to implement this printing for..in loop while
you're verifying primes (in another loop), so maybe think about getting first
200 primes in while loop like you do (and only storing them in a list), and
then printing them out from this list in external for..in loop.



Now, to your primetest() function. It may be good for small primes, but try
to verify with it, if 832475734579 is a prime. :)
def primetest(potentialprime):
divisor = 2
while divisor <= potentialprime:

First of all, see that you rarely use this loop - you check this condition at
most two times. You end up for good in the second while loop.
if potentialprime == 2:
return True
elif potentialprime % divisor == 0:
return False
break

'break' after return is redundant - never executes
while potentialprime % divisor != 0:
if potentialprime - divisor > 1:
divisor += 1
else:
return True

So, this is your main loop. Very inefficient. Think about that:
a) do you really have to check divisors up to the potentialprime?
Maybe there is a point, where you may say, that you've checked all
possibilities? Remember that a * b = b * a
b) do you really have to check every divisor? I mean, increasing
it by 1 in every step?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top