My prime Number generator

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

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
  #31
Jun 1st, 2007
You need to test your code. This one does not work and contains nonprimes in the list!
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
  #32
Jun 1st, 2007
The second one is broken. int(sqrt(len(myarray))) is not guaranteed to be the same as int(sqrt(i)), which is the last number that needs to be tested. Consider that for i = 49,

myarray == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
len(myarray) == 15
and int(sqrt(len(myarray))) == 3

Clearly, 3 is an inadequate bound, as 7 needs to be tested!!

Also, making slices as you go probably negates any speed-ups from using filter().

Jeff
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 8
Reputation: arsham is an unknown quantity at this point 
Solved Threads: 2
arsham arsham is offline Offline
Newbie Poster

Re: My prime Number generator

 
0
  #33
Jun 2nd, 2007
Thanks all for replies
I tested this one , no non-primes there , and x+1 is for the 49 problem :

  1. myarray = [2,3]
  2. from math import sqrt
  3. def prime(x):
  4. for i in xrange(3, x+1,2):
  5. mylist = map(lambda x: i % x,myarray[0:int(sqrt(len(myarray)))])
  6. if 0 not in mylist:
  7. myarray.append(i)
Again thanks for replies
Last edited by arsham; Jun 2nd, 2007 at 3:43 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
  #34
Jun 3rd, 2007
Not quite there yet ...
  1. myarray = [2, 3]
  2. from math import sqrt
  3. def prime(x):
  4. for i in xrange(3, x+1,2):
  5. mylist = map(lambda x: i % x,myarray[0:int(sqrt(len(myarray)))])
  6. if 0 not in mylist:
  7. myarray.append(i)
  8.  
  9. prime(50)
  10. print myarray
  11. """
  12. result -->
  13. [2, 3, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49]
  14. should be -->
  15. [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
  16. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 8
Reputation: arsham is an unknown quantity at this point 
Solved Threads: 2
arsham arsham is offline Offline
Newbie Poster

Re: My prime Number generator

 
0
  #35
Jun 3rd, 2007
Thanks
You are right
Must be :
  1. if 0 not in map(lambda x: i % x, myarray[0:int(sqrt(len(myarray)))+1]):
  2. myarray.append(i)

a sqrt(x)+1 must be in test

Regards
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
  #36
Jun 5th, 2007
I'm confused about the math behind

myarray[0:int(sqrt(len(myarray)))+1]

It seems to me that this could not work in general, since the largest number to be tested has to be sqrt(n), and sqrt(len(primes(n)) < sqrt(n), since len(primes(n)) < n.

But I may be missing something subtle.

Jeff
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: ffao is an unknown quantity at this point 
Solved Threads: 4
ffao ffao is offline Offline
Newbie Poster

Re: My prime Number generator

 
0
  #37
Jun 6th, 2007
Originally Posted by jrcagle View Post
since the largest number to be tested has to be sqrt(n), and sqrt(len(primes(n)) < sqrt(n), since len(primes(n)) < n.
Note that it's not the sqrt(len(primes(n)) natural number, it's the sqrt(len(primes(n)) prime.

I'm curious as to how this works exactly, perhaps it's because the primes array grows faster than sqrt(n), I don't know.

EDIT: A basic debugging showed me some numbers were being tested by too many factors, which seems to corroborate with my theory (such as 99 being divided by 13).

As your intent seemed to be creating a golf, try to beat mine:

  1. p = lambda x: [i for i in range(2,x+1) if all(map(lambda x: i % x,range(2,i**0.5+1)))]

EDIT2: Removing extraneous spaces:
[code=python]
p = lambda x:[i for i in range(2,x+1) if all(map(lambda x:i%x,range(2,i**0.5+1)))]

Filter is actually one char bigger, because you need a lambda...

  1. p = lambda x:filter(lambda i:all(map(lambda x:i%x,range(2,i**0.5+1))),range(2,x+1))
Last edited by ffao; Jun 6th, 2007 at 9:14 pm. Reason: updating my golfs
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: ffao is an unknown quantity at this point 
Solved Threads: 4
ffao ffao is offline Offline
Newbie Poster

Re: My prime Number generator

 
0
  #38
Jun 6th, 2007
When I realized the filter was actually bigger, I transformed the map-loop into a comprehension as well:

  1. p=lambda x:[i for i in range(2,x+1) if all([i%x for x in range(2,i**0.5+1)])]

Can anybody do better?

EDIT: 73 chars now:

  1. p=lambda x:[i for i in range(2,x+1)if sum(i%d<1 for d in range(2,i+1))<2]
Last edited by ffao; Jun 6th, 2007 at 9:28 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
  #39
Jun 15th, 2007
Nice work ffao! Maybe you can post that in "Python Bitch", that would be a tough one to bitch about. I like the second code, since it also works with Python24.
From ffao ...
  1. p=lambda x:[i for i in range(2,x+1)if sum(i%d<1 for d in range(2,i+1))<2]
  2.  
  3. print p(100)
  4.  
  5. """
  6. result -->
  7. [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
  8. """
May 'the Google' be with you!
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