My prime Number generator

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: My prime Number generator

 
0
  #11
Jan 31st, 2007
I did improve upon it some more. I thought to myself: "what's the point in dividing by ALL the numbers between 0 and one lower than the number?", and then "why not just half way?" also, I got rid of the printing to screen and appended it to a list, so it seems very quick.

[php]from __future__ import division

def main():
num = input("How high to find prime numbers? ")
li = [2]
for i2 in range(3,(num+1),2):
prime = True
for i in range(2,(i2/2)):
if (i2/i) % 1 == 0:
prime = False
break
if prime == True:
li.append(i2)
print li

main()

while True:
go_again = raw_input("Again?(y/n) ")
if go_again.lower()[0] == "y":
main()
else:
break[/php]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: My prime Number generator

 
0
  #12
Jan 31st, 2007
With from __future__ import division I can see a problem in line for i in range(2,(i2/2)): you get a float from the division and range does not like floats. Change this line to for i in range(2,(i2//2)):

Nice thinking here, have to see what Bumsfeld's timing comes up with.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: My prime Number generator

 
0
  #13
Jan 31st, 2007
Thanks vegaseat

but what does that do?

[edit]just thought I would point something else out in my code, the first range is like that because what's the point in checking numbers we know to be divisible by 2? And since I did that I just threw 2 into the list already because it wouldn't find two like that.
Last edited by Matt Tacular; Jan 31st, 2007 at 3:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: My prime Number generator

 
0
  #14
Jan 31st, 2007
When you use 'from __future__ import division' then the '/' operator will do floating point division. Python actually has a '//' operator for integer division (floor or quotient) that was seldomly used, since '/' did the same thing. With new versions of Python the '//' will become more popular, since integer divisions are still needed.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: My prime Number generator

 
0
  #15
Jan 31st, 2007
Originally Posted by vegaseat View Post
With new versions of Python the '//' will become more popular, since integer divisions are still needed.
... and old code will break frequently, since division will no longer yield integers. :eek:

Jeff
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: My prime Number generator

 
0
  #16
Feb 1st, 2007
I sped it up again, "if I'm not even bothering to check even numbers, why divide by 2,4,6,etc.?"

replace:
  1. for i in range(2,(i2//2)):
With
  1. for i in range(3,(i2//2),2):
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: My prime Number generator

 
0
  #17
Feb 1st, 2007
Originally Posted by Matt Tacular View Post
I sped it up again, "if I'm not even bothering to check even numbers, why divide by 2,4,6,etc.?"

replace:
  1. for i in range(2,(i2//2)):
With
  1. for i in range(3,(i2//2),2):
Great thinking! I tested it out with 10,000 primes. That made it about 4 times faster than the previous code.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: My prime Number generator

 
0
  #18
Feb 2nd, 2007
Originally Posted by jrcagle View Post
... and old code will break frequently, since division will no longer yield integers. :eek:

Jeff
This is why it hasn't been implemented yet in a large number of other languages like C, C++ either.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: My prime Number generator

 
0
  #19
Feb 2nd, 2007
Another tweak.

With the part of the code that does the dividing: I thought that going half way would be the fastest because what's the point in going the whole way right? After all nothing is going to be divisible past the half way point, take for example 100, nothing past 50 will be divisible, it's just plain imposible. But I was wrong, not with that whole statement, but when I thought that would be the fastest.

Another thing seems to be true, if nothing past the third is divisible, than nothing at all will be divisible either, take 15 for example, 5 and 10 are both divisible, but 10 is divisible by 5 and 5 is at the one third mark. So if we only checked up to 1 third, than 5 will be checked and there would be no need to check up to 10.

At first I thought this would work with a quarter, but numbers like 15 and 9 wouldn't be found out to not be a prime. I'm not 100% sure this works, but it seems to, if anyone notices any flaws let me know, thanks.

Heres the code:
Replace:
  1. for i in range(3,(i2//2),2):
With
  1. for i in range(3,((i2//3)+1),2):
Last edited by Matt Tacular; Feb 2nd, 2007 at 7:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: My prime Number generator

 
0
  #20
Feb 2nd, 2007
Actually going only a quarter the way through the numbers to divide does work, the only number it won't eliminate is 9. just like how I just added 2 to the list because the loop won't determine it a prime, just add a line to remove 9.

Here is my updated prime number generater. Still no where nearly as fast as the sieve thingy, but way faster than my original thing.

  1. from __future__ import division
  2.  
  3. def main():
  4. num = input("How high to find prime numbers? ")
  5. li = [2]
  6. for i2 in range(3,(num+1),2):
  7. prime = True
  8. for i in range(3,((i2//4)+1),2):
  9. if (i2/i) % 1 == 0:
  10. prime = False
  11. break
  12. if prime == True:
  13. li.append(i2)
  14. li.remove(9)
  15. print li
  16.  
  17. main()
  18.  
  19. while True:
  20. go_again = raw_input("Again?(y/n) ")
  21. if go_again.lower()[0] == "y":
  22. main()
  23. else:
  24. break
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC