def largestprimefactor(n): if type(n) is not int and type(n) is not long: raise TypeError allfactors = factors(n) # Now we have a list of factor pairs (non-prime) # Let's find which of these are prime numbers pfactors = [] for factorpair in allfactors: if isprime(factorpair[0]): pfactors.append(factorpair[0]) if isprime(factorpair[1]): pfactors.append(factorpair[1]) # print(pfactors) return max(pfactors)
def d(n): # Sum of proper divisors of n s = 0 pairs = factors(n) for pair in pairs: if pair[0] == pair[1]: # count divisors only once s += pair[0] elif pair[0] == 1: # Don't add n s += pair[0] else: s += pair[0] s += pair[1] return s
def primeFactors(n, conv=False): prime_factors = {} for pair in factors(n): for num in pair: if isprime(num): prime_factors[num] = 1 prod = 1 for prime_factor in prime_factors: prod = prod * prime_factor while prod != n: quotient = n / prod # i.e. what we still need to represent with the prime factors for prime_factor in prime_factors: if quotient % prime_factor == 0: prod = prod * prime_factor prime_factors[prime_factor] += 1 if conv is False: return prime_factors else: return primefactorconverter(prime_factors)
1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? """ from lib import factors triangle=1 last=2 f=factors() while True: triangle=triangle+last last=last+1 k=f.factor_count(triangle) if k>500: break print(triangle)
def method_max(N): return max(factors(N))
def len_fac(n): return len(set(lib.factors(n)))
def smallest_divisible(min_, max_): facts = mset() for i in range(min_, max_+1): facts += mset(factors(i)) - facts return reduce(mul, facts.elements(), 1)