pset 1 soln
A few modifications of Wengsheng Wang's code def primes(n): if n==2: return [2] elif n<2: return [] s=range(3,n+1,2) mroot = n ** 0.5 half=(n+1)/2-1 i=0 m=3 while m <= mroot: if s[i]: j=(m*m-3)/2 s[j]=0 while j<half: s[j]=0 j+=m i=i+1 m=2*i+3 return [2]+[x for x in s if x] def nprimes(p): n=0 while len(primes(n))<p: n=n+1 if str(p)[1]=='1': print "The ",p,"st prime number is: ",n elif str(p)[1]=='2': print "The ",p,"nd prime number is: ",n elif str(p)[1]=='3': print "The ",p,"rd prime number is: ",n else: print "The ",p,"th prime number is: ",n
please use a code pasting site: http://dpaste.com http://pastebin.com http://codepad.org
Join our real-time social learning platform and learn together with your friends!