| | |
My prime Number generator
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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]
[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]
With
Nice thinking here, have to see what Bumsfeld's timing comes up 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!
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.
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.
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!
I sped it up again, "if I'm not even bothering to check even numbers, why divide by 2,4,6,etc.?"
replace: With
replace:
Python Syntax (Toggle Plain Text)
for i in range(2,(i2//2)):
Python Syntax (Toggle Plain Text)
for i in range(3,(i2//2),2):
•
•
•
•
I sped it up again, "if I'm not even bothering to check even numbers, why divide by 2,4,6,etc.?"
replace:WithPython Syntax (Toggle Plain Text)
for i in range(2,(i2//2)):Python Syntax (Toggle Plain Text)
for i in range(3,(i2//2),2):
May 'the Google' be with you!
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: With
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:
Python Syntax (Toggle Plain Text)
for i in range(3,(i2//2),2):
Python Syntax (Toggle Plain Text)
for i in range(3,((i2//3)+1),2):
Last edited by Matt Tacular; Feb 2nd, 2007 at 7:46 pm.
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.
Here is my updated prime number generater. Still no where nearly as fast as the sieve thingy, but way faster than my original thing.
Python Syntax (Toggle Plain Text)
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(3,((i2//4)+1),2): if (i2/i) % 1 == 0: prime = False break if prime == True: li.append(i2) li.remove(9) print li main() while True: go_again = raw_input("Again?(y/n) ") if go_again.lower()[0] == "y": main() else: break
![]() |
Similar Threads
Other Threads in the Python Forum
- Previous Thread: Where's Nemo?
- Next Thread: Reversing a sentence
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code copy customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyglet pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






