示例#1
0
def chain_length(n):
    result = 2
    n -= 1
    while n > 1 and result < 26: # no point in computing chains longer than 25
        n = totient(n)
        result += 1
    return result
示例#2
0
def solve_slow():
    result = {}
    for n in xrange(2, 10000000):
        phi = totient(n)
        if is_permutation(n, phi):
            result[n] = float(n) / phi

    return min(result.iteritems(), key=operator.itemgetter(1))[0]
示例#3
0
def solve():
    d = 1
    primes = []
    phi = None

    while True:
        for p in gen_primes():
            d *= p
            primes.append(p)
            phi = totient(d)
                        
            if phi / float(d-1) < LIMIT:
                if p > 2:
                    primes.pop()
                    d /= p
                    phi = totient(d)
                    break
                else:
                    return d
示例#4
0
def solve():
    primes = list_primes(1000000)
    result = {}

    for x in primes[:300:-1]:
        for y in primes[300:]:
            n = x * y
            if n > 10000000:
                break
            phi = totient(n)
            if is_permutation(n, phi):
                result[n] = float(n) / phi

    return min(result.iteritems(), key=operator.itemgetter(1))[0]
示例#5
0
文件: p06x.py 项目: jpaeng/PE
def maximum_totient_ratio_simple(max_n):
    """Return number n < max_n with maximum n/totient(n) ratio.
    Per analysis, the n with maximum n/totient(n) for any given range will be an even number.

    :param max_n:   maximum n to check
    :return:        n with the maximum n/totient(n) ratio in the form:  (n, totient(n), n/totient(n))
    """
    max_ratio = (0, 0, 0)
    for n in range(2, max_n+1, 2):  # Check only even n
        f = common.totient(n)
        ratio = n/f
        if max_ratio[-1] < ratio:
            max_ratio = (n, f, ratio)

    return max_ratio
示例#6
0
文件: p06x.py 项目: jpaeng/PE
def maximum_totient_ratio_odd(max_n):
    """Return number n < max_n with maximum n/totient(n) ratio.
    Per analysis, the n with maximum n/totient(n) for any given range will be an even number that is twice an odd number
    because of the property:
            totient(2m) = 2*totient(m) if m is even
                    = totient(m) if m is odd

    :param max_n:   maximum n to check
    :return:        n with the maximum n/totient(n) ratio in the form:  (n, totient(n), n/totient(n))
    """
    max_ratio = (0, 0, 0)
    for n in range(1, 1+max_n//2, 2):   # Only odd n
        f = common.totient(n)
        ratio_2n = 2*n/f
        if max_ratio[-1] < ratio_2n:
            max_ratio = (2*n, f, ratio_2n)

    return max_ratio
示例#7
0
def solve():
    return sum(totient(i) for i in xrange(2, 1000001))
示例#8
0
def solve():
    result = {n: float(n)/totient(n) for n in xrange(2, 1000001)}
    return max(result.iteritems(), key=operator.itemgetter(1))[0]
示例#9
0
文件: p07x.py 项目: jpaeng/PE
def generate_totient_list(max_n):
    """

    totient(m*n) = totient(m)*totient(n)*(d/totient(d))   where d = gcd(m, n)
    totient(2*m) = 2*totient(m) if m is even
             = totient(m) if m is odd
    totient(n^m) == n^(m-1) * totient(n)
    :param max_n:
    :return:
    """
    totient_list = [0] * (max_n+1)
    prime_list = [2]

    # Process 2 and multiples of 2 first
    n = 2
    totient_list[n] = n - 1
    prev_phi = totient_list[n]
    n2 = 2 * n
    while n2 < max_n:
        totient_list[n2] = 2 * prev_phi
        prev_phi = totient_list[n2]
        n2 *= 2

    # Loop through rest of numbers
    for n in range(3, max_n+1):
        if totient_list[n] == 0:
            if common.is_prime_mr(n):   # all primes > 2 are odd
                totient_list[n] = n - 1
                n2 = 2 * n
                if n2 < max_n:
                    totient_list[n2] = totient_list[n]
                    prev_phi = totient_list[n]
                    n2 *= 2
                    while n2 < max_n:
                        totient_list[n2] = 2 * prev_phi
                        prev_phi = totient_list[n2]
                        n2 *= 2
                    # for p in prime_list:
                    #     index = p * n
                    #     if index < max_n:
                    #         totient_list[index] = totient_list[p] * totient_list[n]
                    #     else:
                    #         break
                    # prime_list.append(n)
                    m = 2
                    index_prev = n**(m - 1)
                    index = n**m
                    while index < max_n:
                        if totient_list[index] == 0:
                            totient_list[index] = index_prev * totient_list[n]
                        m += 1
                        index_prev = index
                        index = n**m
                    for m in range(3, n):
                        index = n * m
                        if index < max_n:
                            if totient_list[index] == 0:
                                totient_list[index] = totient_list[m] * totient_list[n]
                        else:
                            break
            else:   # n is not prime
                totient_list[n] = common.totient(n)
                prev_phi = totient_list[n]
                n2 = 2 * n
                if (n % 2 == 0) and (n2 < max_n):  # if n is odd
                    if totient_list[n2] == 0:
                        totient_list[n2] = prev_phi
                    n2 *= 2
                while n2 < max_n:
                    if totient_list[n2] == 0:
                        totient_list[n2] = 2 * prev_phi
                    prev_phi = totient_list[n2]
                    n2 *= 2
        else:
            prev_phi = totient_list[n]
            n2 = 2 * n
            if (n % 2 == 1) and (n2 < max_n):  # if n is odd
                if totient_list[n2] == 0:
                    totient_list[n2] = prev_phi
                n2 *= 2
            while n2 < max_n:
                if totient_list[n2] == 0:
                    totient_list[n2] = 2 * prev_phi
                prev_phi = totient_list[n2]
                n2 *= 2

    return totient_list
示例#10
0
文件: p07x.py 项目: jpaeng/PE
        print('left_ordered_fraction((2, 5), 8) =', left_ordered_fraction((2, 5), 8))
        print('left_ordered_fraction((3, 7), 8) =', left_ordered_fraction((3, 7), 8))
        print('left_ordered_fraction((1, 2), 8) =', left_ordered_fraction((1, 2), 8))
        print('left_ordered_fraction((3, 7), 10**6) =', left_ordered_fraction((3, 7), 10**6))
    elif problem_num == 72:
        print()
        for z in range(2, 9):
            print('count_reduced_fractions(', z, ') =', count_reduced_fractions(z))
#        print('count_reduced_fractions(10**3) =', count_reduced_fractions(10**3))
        print()
        for z in range(2, 9):
            print('list_reduced_fractions(', z, ') =', list_reduced_fractions(z))
        print()
        zlist = generate_totient_list(40)
        for z in range(2, 41):
            print('    ', z, zlist[z], common.totient(z))
        print()
        z = 10**3
        print('sum(generate_totient_list(', z, ')) =', sum(generate_totient_list(z)))
    elif problem_num == 73:
        print()
    elif problem_num == 74:
        print()
    elif problem_num == 75:
        print()
    elif problem_num == 76:
        print()
    elif problem_num == 77:
        print()
    elif problem_num == 78:
        print()