Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remade prime generator

  • Loading branch information...
commit afdf8869bee24a1bd51296b9055dfaa4586c1ac1 1 parent 4400060
@hybridthesis authored
Showing with 27 additions and 35 deletions.
  1. +27 −35 prime.py
View
62 prime.py
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.