Esempio n. 1
0
    print 'target_num %d, odd_cnt %d, even_cnt %d, 1 is not included'%(target_num, odd_prime_factors_cnt_comm, even_prime_factors_cnt_comm)

    if(factor_list_option == g_LIOUVI_FACTOR):
        assert( (odd_prime_factors_cnt_comm + even_prime_factors_cnt_comm + 1) == target_num)

        # for Liouvi, unless functions go to very huge number (>10^9), otherwise, it always have negative parity
        assert(odd_prime_factors_cnt_comm > even_prime_factors_cnt_comm)

    # liouvi breaks mert limitation, because it can goes to relative large negative value
    # but let's try to catch any special case
    verify_mert_conjecture(target_num, odd_prime_factors_cnt_comm, even_prime_factors_cnt_comm,  factor_list_option)

    # if need to save
    if(mert_settings.g_log_save_option == g_LOG_YES):

        cnt_nodes, cnt_leaves = log_save_write_results_to_files(target_num, factor_list_option)

        if(factor_list_option == g_LIOUVI_FACTOR):
            assert( (cnt_nodes + cnt_leaves) == target_num)

        # output complete list in 2D
        output_option = 1
        log_save_write_results_in_2d_tree(target_num, factor_list_option, output_option)

        # output only last number in 2D
        output_option = 2
        log_save_write_results_in_2d_tree(target_num, factor_list_option, output_option)

        # name for picture file
        t_range_list = range(MAX_NUM_FACTORS)
        file_result_pic = 'outputs_mert\\mert_target_num_%d.png'%(target_num)
Esempio n. 2
0
def mert_fun_sequences(max_num, factor_list_option, log_save_option):

    mert_settings.g_log_save_option = log_save_option

    detect_recursive_flag = False

    # the length of the follow list is same as the number the program will loop
    parity_1d =[]                               # 1d list
    odd_prime_factors_cnt_comm_1d =[]           # 1d list
    even_prime_factors_cnt_comm_1d = []         # 1d list

    factors_his_2d = []             # 2D list, hold the list of lists, each list hold factors_histogram for a number.
                                    # which has the fix size.

    complete_list_list_3d =[]       # each g_complete_list_list is 2D, a collect of them will be 3D
                                    # first 2 dimension is not fixed, length of last dimension is the number we will
                                    # loop
    one_number_list_2d = []         # each g_one_number_list is 1D, a collect of them will be 2D
                                    # first dimension is not fixed. It is same as the number of square free number < n.
                                    # The length of last dimension is the number we will loop
    binary_list_2d = []             # each g_binary_list is 1D, a collect of them will be 2D
                                    # first dimension is not fixed. It is same as the number of square free number < n.
                                    # The length of last dimension is the number we will loop

    # need 'max_num + 1' to include up to max_num
    range_list = range(2, max_num + 1)

    # Note, our calculation of mert_fun will NOT include mert(1) and mert(2)
    # so, we need to remember that our mert_func() starts from mert(3)
    for target_num in range_list:

        print '================ starting %d =============='%(target_num)

        # Have to reset some global data buffer before each run
        log_save_reset_global_data_buffer()

        # main result will be output of parity, factors_his, odd_prime_factors_cnt_comm, even_prime_factors_cnt_comm
        parity, factors_his, odd_prime_factors_cnt_comm, even_prime_factors_cnt_comm = mert_function(target_num, factor_list_option)

        # save data related to parity, number of odd prime factors, number of even prime factors.
        parity_1d.append(parity)
        odd_prime_factors_cnt_comm_1d.append(odd_prime_factors_cnt_comm)
        even_prime_factors_cnt_comm_1d.append(even_prime_factors_cnt_comm)

        # save factors_his to 2d list, so that we can compare them.
        factors_his_2d.append(factors_his)

        # If we turn on log option, above mert_function() stores tree structure in global data buffer, save them into file
        cnt_nodes, cnt_leaves = log_save_write_results_to_files(target_num, factor_list_option)

        # retrieve global data from buffer
        complete_list_list, one_number_list, binary_list = log_save_get_results(target_num)

        # Put those results in aggregated data structure
        complete_list_list_3d.append(complete_list_list)
        one_number_list_2d.append(one_number_list)
        binary_list_2d.append(binary_list)

    print 'Finish the loop for individual number mert calculation, starting to process aggregated data '

    # factors_his_2d is written out as json file
    f_save_factors_his_2d_name = 'outputs_mert\\factors_his_2d_%d_option_%d.txt'%(max_num, factor_list_option)
    with open(f_save_factors_his_2d_name, 'w') as f:
        json.dump(factors_his_2d, f,  indent=2)

    # parity_1d is written out as json file
    f_save_parity_1d_name = 'outputs_mert\\parity_1d_%d_option_%d.txt'%(max_num, factor_list_option)
    with open(f_save_parity_1d_name, 'w') as f:
        json.dump(parity_1d, f,  indent=2)

    # complete_list_list_3d is written out as json file
    f_complete_list_list_3d_name = 'outputs_mert\\complete_list_list_3d_%d_option_%d.txt'%(max_num, factor_list_option)
    with open(f_complete_list_list_3d_name, 'w') as f:
        json.dump(complete_list_list_3d, f, indent=2)

    # one_number_list_2d is written out as json file
    f_one_number_list_2d_name = 'outputs_mert\\one_number_list_2d_%d_option_%d.txt'%(max_num, factor_list_option)
    with open(f_one_number_list_2d_name, 'w') as f:
        json.dump(one_number_list_2d, f, indent=2)

    # binary_list_2d is written out as json file
    f_binary_list_2d_name = 'outputs_mert\\binary_list_2d_%d_option_%d.txt'%(max_num, factor_list_option)
    with open(f_binary_list_2d_name, 'w') as f:
        json.dump(binary_list_2d, f, indent=2)

    if(detect_recursive_flag == True):
        # run detect recursive algorithm to see if we can find interesting recursive patterns
        print 'Run detect recursive algorithm'
        found_match_num_list, found_match_num_factor_len_list,  num_of_match_found = mert_recursion_detect(binary_list_2d, max_num)

    file_result_pic = 'outputs_mert\\parity_1d_num_%d_option_%d.png'%(max_num, factor_list_option)
    title_str = 'num_%d_option_%d.png'%(max_num, factor_list_option)
    plot_parity_1d(parity_1d, range_list, file_result_pic, title_str, 'parity_his')
    print 'parity_1d (start from 2, skip 1): ', parity_1d

    # moving window to detect min and max
    # We found that if we choose moving_window 40, we will capture major min and max
    # if we choose smaller number, we will get some min and max maybe not important
    moving_window = 40
    fixed_window_option = 1
    len_parity_id = len(parity_1d)

    # try to detect min and max of mert_function if we have enough sample
    # plot min and max of mert_function if we have enough samples
    if (len_parity_id > moving_window*2):
        detect_and_plot_max_min(parity_1d, moving_window, max_num, factor_list_option, fixed_window_option)

    file_result_pic = 'outputs_mert\\factors_his_num_%d_option_%d.png'%(max_num, factor_list_option)
    plot_odd_even_factors(odd_prime_factors_cnt_comm_1d, even_prime_factors_cnt_comm_1d,  max_num, range_list, file_result_pic, factor_list_option)

    return parity_1d