User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 426,193 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,800 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 7900 | Replies: 38
Reply
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: My prime Number generator

  #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  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,468
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: My prime Number generator

  #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  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: My prime Number generator

  #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 2:50 pm.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,468
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: My prime Number generator

  #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  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: My prime Number generator

  #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  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: My prime Number generator

  #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:
for i in range(2,(i2//2)):
With
for i in range(3,(i2//2),2):
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,468
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: My prime Number generator

  #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:
for i in range(2,(i2//2)):
With
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  
Join Date: Aug 2005
Posts: 1,133
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 66
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: My prime Number generator

  #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  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: My prime Number generator

  #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:
for i in range(3,(i2//2),2):
With
for i in range(3,((i2//3)+1),2):
Last edited by Matt Tacular : Feb 2nd, 2007 at 6:46 pm.
Reply With Quote  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: My prime Number generator

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

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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC