Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65220 > unrolled thread
| Started by | Panagiotis Anastasiou <panast24@gmail.com> |
|---|---|
| First post | 2014-02-01 07:33 -0800 |
| Last post | 2014-02-02 00:34 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Panagiotis Anastasiou <panast24@gmail.com> |
|---|---|
| Date | 2014-02-01 07:33 -0800 |
| Subject | Python prime numbers |
| Message-ID | <158634a8-3c20-4e04-bc2a-a562c7270a3b@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Larry Martell <larry.martell@gmail.com> |
|---|---|
| Date | 2014-02-01 10:50 -0500 |
| Message-ID | <mailman.6283.1391269855.18130.python-list@python.org> |
| In reply to | #65220 |
[Multipart message — attachments visible in raw view] — view raw
On Saturday, February 1, 2014, Panagiotis Anastasiou <panast24@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.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-02-01 16:59 -0500 |
| Message-ID | <mailman.6290.1391291803.18130.python-list@python.org> |
| In reply to | #65220 |
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
>
> 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
>
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-02-01 17:07 -0500 |
| Message-ID | <mailman.6291.1391292302.18130.python-list@python.org> |
| In reply to | #65220 |
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
[toc] | [prev] | [next] | [standalone]
| From | Wiktor <look@signature.invalid> |
|---|---|
| Date | 2014-02-02 00:34 +0100 |
| Message-ID | <10n152z4ab5ee$.uxddm41hh11e$.dlg@40tude.net> |
| In reply to | #65220 |
On Sat, 1 Feb 2014 07:33:47 -0800 (PST), Panagiotis Anastasiou 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 :
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?
--
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web