Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4400060
commit afdf886
Showing
1 changed file
with
27 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,38 @@ | ||
import math | ||
from math_func import * | ||
|
||
class Prime: | ||
|
||
|
||
def __init__(self, **kargs): | ||
self.primes = [] | ||
try: | ||
self.n_max = kargs['max'] | ||
self.numlist = range(2, self.n_max) | ||
self._createPrime() | ||
except: | ||
print "Warning: No max provided" | ||
print "test Yutong" | ||
def __init__(self, **args): | ||
self.value = 0 | ||
self.currentvalue = 3 | ||
self.primelist = [2, 3] | ||
try: | ||
self.nth = kargs['nth'] | ||
self._getNth() | ||
self.operation = args.keys()[0] | ||
except: | ||
print "Warning: No nth provided." | ||
print "test sheep" | ||
return | ||
print "Warning: No operation given!" | ||
self.operation = 'none' | ||
return | ||
|
||
def _createPrime(self): | ||
while len(self.numlist): | ||
current_prime = self.numlist[0] | ||
d = lambda x: x% current_prime | ||
self.numlist = filter(d,self.numlist) | ||
self.primes.append(current_prime) | ||
def __iter__(self): | ||
return self | ||
|
||
def next(self): | ||
primeCandidate = self.currentvalue + 2 | ||
primeTestMax = int(math.sqrt(primeCandidate))+1 | ||
|
||
def _getNth(self): | ||
"""Search for the nth prime number""" | ||
nth = self.nth | ||
def primetest(pCandidate): | ||
for i in self.primelist: | ||
if i>primeTestMax: return True | ||
if pCandidate % i == 0 : return False | ||
return True | ||
|
||
while not primetest(primeCandidate): | ||
primeCandidate +=2 | ||
|
||
t = lambda n: n/math.log(n) | ||
#Congratulations! You have found your prime number =) | ||
self.currentvalue = primeCandidate | ||
self.primelist.append(primeCandidate) | ||
return primeCandidate | ||
|
||
i = 2 | ||
while t(i)<nth: | ||
i = i + 1 | ||
|
||
self.numlist = range(2, i) | ||
self._createPrime() | ||
self.nth = self.primes[nth-1] | ||
def __iter__(self): | ||
print "HIIIIIII" | ||
def __repr__(self): | ||
return str(self.value) |