Exemplo n.º 1
0
Arquivo: p06x.py Projeto: jpaeng/PE
def check_prime_concatenations_mr(prime1, prime2):
    str_prime1 = str(prime1)
    str_prime2 = str(prime2)

    status = True
    prime_cat = int(str_prime1 + str_prime2)
    if not common.is_prime_mr(prime_cat):
        status = False

    prime_cat = int(str_prime2 + str_prime1)
    if not common.is_prime_mr(prime_cat):
        status = False

    return status
Exemplo n.º 2
0
Arquivo: p07x.py Projeto: jpaeng/PE
def count_reduced_fractions(max_d):
    count = 0

    # Even denominators
    for d in range(2, max_d+1, 2):
        count += 1                  # always count 1/d.
        for n in range(3, d, 2):    # Check only odd numerators
            if common.get_gcd(n, d) == 1:
                count += 1

    # Odd denominators
    for d in range(3, max_d+1, 2):
        if common.is_prime_mr(d):
            count += d - 1
        else:
            count += 2                  # always count 1/d and 2/d for odd d.
            for n in range(3, d):
                if common.get_gcd(n, d) == 1:
                    count += 1

    return count
Exemplo n.º 3
0
Arquivo: p07x.py Projeto: 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