Василина обнови решението на 03.05.2012 09:02 (преди над 12 години)
+import re
+class primes(object):
+
+ def __init__(self):
+ self.prime = 2
+ def __iter__(self):
+ return self
+ def next( self ):
+ regex_str = r'^1?$|^(11+?)\1+$'
+ expression = re.compile( regex_str )
+ if self.prime is 2:
+ self.prime = self.prime + 1
+ return 2
+ else:
+ while re.search( expression , '1'*self.prime ):
+ self.prime = self.prime + 1
+ self.prime = self.prime + 1
+ return self.prime - 1
+
+class semiprimes(object):
+ def __init__(self):
+ self.semiprime = 2*2
+ regex_str = r'^1?$|^(11+?)\1+$'
+ self.expression = re.compile( regex_str )
+ def __iter__(self):
+ return self
+ def primefactors( self ):
+ mockprime = self.semiprime
+ factorlist = 0
+ p = primes()
+ loop = next(p)
+ while loop <= mockprime:
+ if mockprime % loop == 0:
+ mockprime /= loop
+ factorlist += 1
+ if factorlist > 2:
+ return False
+ else:
+ loop = next(p)
+ if factorlist == 2:
+ return True
+ else:
+ return False
+
+ def next( self ):
+ if self.semiprime == 4:
+ self.semiprime += 1
+ return 4
+ else:
+ while self.primefactors() is False:
+ self.semiprime += 1
+ self.semiprime += 1
+ return self.semiprime - 1