Exemplo n.º 1
0
def verify_multiply_properties(start_num, end_num, step_num):

    for n in xrange(start_num, end_num + 1, step_num):

        # factor m into products of prime powers, and use multiplicative properties
        factor_list = factor_a_number(n, prime_list)

        num_of_diff_primes = len(factor_list)

        liouvi_n = liouvi_list[n - 1]

        liouvi_n_by_factor = 1

        # handle all prime factors
        for i in range(0, num_of_diff_primes):

            p = factor_list[i][0]
            l = factor_list[i][1]
            liouv_p = liouvi_list[p - 1]

            #
            if l % 2 == 0:
                liouvi_p_power_l = 1
            else:
                liouvi_p_power_l = liouv_p

            liouvi_n_by_factor *= liouvi_p_power_l

        print "n = %d, liouvi_n = %d, liouvi_n_by_factor = %d" % (n, liouvi_n, liouvi_n_by_factor)

        assert liouvi_n == liouvi_n_by_factor
Exemplo n.º 2
0
def get_g_k_for_GBC_twi_prim(k, N, prime_list):

    # factor a number k to product of prime power
    # k = p1^r1*p2^r2*...
    factor_list = factor_a_number(k, prime_list)
    g_k = 1
    count = len(factor_list)

    # using the multiplicative properties
    for x in range(count):

        p = factor_list[x][0]  # prime p
        r = factor_list[x][1]  # power of p

        if N % p != 0:
            g_p = 2 / float(p)
        else:
            g_p = 1 / float(p)

        # g_p_r = g(p)^r, because of multiplicative properties
        g_p_r = np.power(g_p, r)

        # multiplicative properties
        g_k *= g_p_r

    return g_k
Exemplo n.º 3
0
def get_quadr_recip_using_multiplicative_properties(n, p, prime_list):

    check_p_is_prime  = p in prime_list
    if(check_p_is_prime == False):
        print 'get_quadr_recip_using_multiplicative_properties only work for p is a prime, p %d'%(p)
        assert(0)

    if(gcd(n, p) >1):
        return 0

    # first find reminder of m = n%p
    m = n%p

    # factor m into products of prime powers, and use multiplicative properties
    factor_list = factor_a_number(m, prime_list)
    tmp_qr = 1
    count = len(factor_list)

    # using the multiplicative properties
    for x in range(count):

        # we can use either of the following methods to get (l | p) where l is a prime smaller than p
        # l = factor_list[x]
        #
        # We can further simplify the calculation by using quad_recip formula to reduce
        # (l | p) to (p | l) if NOT (l = 3 mod(4) and p = 3 mod(4))
        # tmp2 = get_quadr_recip_brute_force(factor_list[x][0], p)          # method 1
        tmp2 = eul_criter(factor_list[x][0], p)                             # method 2

        tmp3 = int(round(np.power(tmp2, factor_list[x][1] )))
        tmp_qr *= tmp3

    return tmp_qr
Exemplo n.º 4
0
def check_c_contain_prime_factors_of_ab_gcd(ab_gcd, c):

    # for special case ab_gcd = 2, 3, 5, 7, we will do quick search.
    # we will not call factor_a_number() function. This will speed up program
    if(ab_gcd == 2) or (ab_gcd == 3) or (ab_gcd == 5) or (ab_gcd == 7):
        if (c%ab_gcd == 0):
            return 1
        else:
            return 0

    global g_primes_list

    # prime factors of ab_gcd must also be prime factor of c
    factor_list = factor_a_number(ab_gcd, g_primes_list)

    diff_prime_factor_cnt = len(factor_list)

    for i in range(diff_prime_factor_cnt):
        p = factor_list[i][0]

        # c must contain p as a factor
        if(c%p != 0):
            return 0

    # pass all test, c contains all prime factors of ab_gcd
    return 1
Exemplo n.º 5
0
def dirichi_char_for_n(n):

    prime_list = get_prime_list(n)
    factor_list = factor_a_number(n, prime_list)
    num_of_diff_primes = len(factor_list)

    # this will be a list of 2D matrix, each 2D matrix hold a dirich_char matrix for a prime
    dirich_char_mat_list =[]
    prim_char_stat_list = []

    # handle all prime factor
    for i in range(0, num_of_diff_primes):

        p = factor_list[i][0]
        l = factor_list[i][1]
        dirich_char_p_mat, phi_p_l, coprime_list, prim_char_stat = dirich_char_power_p(p, l)

        print 'p = ', p, ', l = ', l, np.asarray(dirich_char_p_mat).shape

        dirich_char_mat_list.append(dirich_char_p_mat)
        prim_char_stat_list.append(prim_char_stat)

        print prim_char_stat

    # form a product of all of above matrix
    tmp_mat = np.eye(1)

    for i in range(0, num_of_diff_primes):
        tmp_mat = dirich_char_mat_product(tmp_mat, dirich_char_mat_list[i])

    print 'n = ', n, ', matrix shape = ', np.asarray(tmp_mat).shape

    phi_p_n, coprime_list = eul_totie(n)

    # set initial default value for statistics count
    prim_char_stat_all = {}
    prim_char_stat_all['num_of_total_char'] = -1
    prim_char_stat_all['num_of_prim_char'] = -1
    prim_char_stat_all['num_of_non_prim_char'] = -1      # include principle char
    prim_char_stat_all['num_of_real_char'] = -1

    num_of_total_char_all = 1
    num_of_prim_char_all = 1
    num_of_prim_char_real = 1

    for i in range(0, num_of_diff_primes):

        num_of_total_char_all = num_of_total_char_all*prim_char_stat_list[i]['num_of_total_char']
        num_of_prim_char_all = num_of_prim_char_all*prim_char_stat_list[i]['num_of_prim_char']
        num_of_prim_char_real = num_of_prim_char_real*prim_char_stat_list[i]['num_of_real_char']

    prim_char_stat_all['num_of_total_char'] =  num_of_total_char_all
    prim_char_stat_all['num_of_prim_char'] = num_of_prim_char_all
    prim_char_stat_all['num_of_non_prim_char'] = num_of_total_char_all - num_of_prim_char_all
    prim_char_stat_all['num_of_real_char'] = num_of_prim_char_real

    print prim_char_stat_all

    return tmp_mat, phi_p_n, coprime_list, prim_char_stat_all
Exemplo n.º 6
0
def jacob_sym(n, q, prime_list):

    # jacob_sym is only defined for odd number q.
    assert(q%2 == 1)

    # using formula of product to calculate jacob
    factor_list = factor_a_number(q, prime_list)
    tmp_jaco = 1
    count = len(factor_list)

    for x in range(count):
        tmp2 = get_quadr_recip_brute_force(n, factor_list[x][0])
        tmp3 = int(round(np.power(tmp2, factor_list[x][1] )))
        tmp_jaco *= tmp3

    return tmp_jaco
Exemplo n.º 7
0
def krone_sym(a, n, prime_list):

    # print 'a = ', a, ', n = ', n

    # calculate (a/0) and return
    if(n == 0):
        if (a == 1) or (a == -1):
            tmp_krone = 1
        else:
            tmp_krone = 0

        return tmp_krone

    # calculate (a/-1)
    a_minus_1 = 1
    if (n < 0):

        if(a >= 0 ):
            a_minus_1 = 1
        else:
            a_minus_1 = -1

        n_tmp = -n
    else:
        n_tmp = n

    tmp_krone = a_minus_1

    if(n == 1) or (n == -1):
        tmp_krone = a_minus_1
        return tmp_krone

    # print 'a_minus_1 = ', a_minus_1

    # using formula of product to calculate krone_sym
    factor_list = factor_a_number(n_tmp, prime_list)

    count = len(factor_list)

    # i will become 1 if first prime factor is 2, otherwise, it will stay 0
    i = 0

    # calculate (a/2) if 2 is a factor of n
    a_2 = 1

    # if 1st factor = 2, calculate (a/2)
    if(factor_list[0][0] == 2):

        # a is even
        if(a%2 == 0):
            tmp1 = 0

        # a odd and 1 mod(8) or 7 mod(8)
        if(a%8 == 1 )or (a%8 == 7 ):
            tmp1 = 1

        # a odd and 3 mod(8) or 5 mod(8)
        if(a%8 == 3 )or (a%8 == 5 ):
            tmp1 = -1

        # get the power of (a/2)^factor_list[0][1]
        # if a is also even, this will deal with 0^x (where x is power factor).
        #
        a_2 = int(round(np.power(tmp1, factor_list[0][1] )))

        # adjust the starting for other prime factor
        i = 1

    # multiply a_2
    tmp_krone *= a_2

    # print 'a_2 = ', a_2

    # get the rest of prime factor
    for x in range(i, count):
        tmp2 = get_quadr_recip_brute_force(a, factor_list[x][0])
        tmp3 = int(round(np.power(tmp2, factor_list[x][1] )))
        tmp_krone *= tmp3

    # print 'tmp_krone = ', tmp_krone

    return tmp_krone