def euler(): # number of fractions found match_count = 0 # for each possible numerator n for n in range(2, math.ceil(MAX / 2) + 1): # lowest possible denominator for a fraction in the wanted range start = math.ceil(2 * n + DELTA) # highest possible denominator for a fraction in the wanted range end = min(MAX, int(3 * n - DELTA)) # construct a dictionary whose keys are all the denominators that # do *not* make a reduced fraction with the numerator n table = dict() for p in prime.prime_factors(n): for q in range(p, end + 1, p): table[q] = True # for each denominator d for d in range(start, end + 1): # if n / d is a reduced fraction if not d in table: match_count += 1 # return the number of fractions found return match_count
def totient(n): """Return the number of integers <= n that are relatively prime with n.""" t = n for p in set(prime.prime_factors(n)): t *= 1 - 1 / p return round(t)