def divisor_sum_using_prime_factors(n): pf = list(set(prime_factor.prime_factors(n))) s = 1 for p in pf: k = 0 while n % p == 0: k += 1 n //= p s *= (p**(k + 1) - 1) // (p - 1) return s
def factorise(n): if n < 1: return None factors = [1] pfactors = [x for x in prime_factors(n)] pfactors = [x for x in chain.from_iterable(combinations(pfactors, r) for r in range(len(pfactors) + 1))] for comb in pfactors: product = 1 for x in comb: product *= x if product not in factors: factors.append(product) return sorted(factors)
def totient(num): """ Counts the numbers that are relative prime to the number. This means that if a number 'x' has common divisors with another number 'a' lower than itself, it is, 'a' is not relative prime to 'x'. This means that if a number is prime, its totient function is its value - 1, as all numbers lower than the number itself is relative prime to the number. An example: totient(9): 1, 2, 4, 5, 7, 8 are all relative prime 3, 6, and 9 have at least one common divisor with 9 returns 6, as there are 6 numbers relative prime to 9 If you visit 'https://en.wikipedia.org/wiki/Euler%27s_totient_function' this function uses the function described under euler's product formula. """ # set(prime_factors(num)) to get only unique primes. # it is the (1 - 1/p) part from the wiki-page uniqe_primes = [(1 - 1 / p) for p in set(prime_factors(num))] phi = num * product( uniqe_primes) # The totient function is commonly called phi phi = int(round(phi)) # As phi is a whole number return phi
def phi(n): #Eulers phi-funktion #Giver p-1 for alle primfaktorerne return produkt([p - 1 for p in prime_factors(n)]) #Og ganger dem så sammen
def test_prime(self): self.assertEqual(prime_factors(5), [5,])
def test_known(self): self.assertEqual(prime_factors(13195), [5, 7, 13, 29,])
def test_nonprime_noncomposite(self): self.assertFalse(prime_factors(1))
def test_composite(self): self.assertEqual(prime_factors(6), [2, 3,])