def primab(n): ''' a sieve for primitive abundant numbers less than or equal to n''' plist = primes2.eratosthenes(int(n**0.5)) #the version of pf with a primelist is more efficient for work with ranges pab = [] #initiate result lst = [] #initiate array i = 0 #initiat index while i < n+1: #fill the array with bools lst += [True] i += 1 i = 1 #reset index to 1, so as not to test 0 #end of initialization while i < n + 1: if lst[i]: #if it is not a multiple of the previously discovered primitive abundant numbers . . . m = number_theory2.nu(i, plist) #check if it is abundant or perfect. if m >=2: #if it is, ... pab += [i] #then it is primitive abundant. k = i #cancel out all multiples of i; this initiates the index, ... while k < n +1: #and this actually makes the array values "false" lst[k] = False k += i else: lst[i] = False i +=1 return pab
def isprimab(n, primelist = None, primefactorization = None): '''test whether n is primitive abundant''' if n == 0: return False else: if primefactorization == None: #initiate prime factorization p = primes2.pf(n, primelist) else: p = primefactorization m = number_theory2.nu(n, None, p) #we need to see if n is at all a valid candidate if m == 2: return True #all perfect numbers are primitive abundant elif m > 2: #abundant case more complex f = impfact(n, None, p) #find the necessary factors x = True #initiate bool results for i in range(len(f)): #check each factor if x: #if the last factor was deficient . . . x = number_theory2.isdeficient(f[i], primelist) #check if this one is else: #if the last one already wasn't abundant . . . break #no need to check further return x #return our bool result else: return False #clearly, no deficient numbers are primitive abundant