def num_of_single_factor_from_begin(begin_list, primes, target_num):

    begin_product = product_of_a_list(begin_list)

    number_of_factors = len(begin_list) + 1
    cnt = 0

    # print begin_product
    if begin_product > target_num:
        return cnt, number_of_factors

    # last in begin_list
    if begin_list != []:
        begin_list_last = begin_list[len(begin_list) - 1]
    else:
        begin_list_last = 1

    # an example of begin_list can be [2, 61]
    # or [3, 7, 11]
    for p in primes:

        # next p must be larger than begin_list_last
        if p <= begin_list_last:
            continue

        product = begin_product * p

        if product <= target_num:
            cnt += 1
            continue

        if product > target_num:
            break

    return cnt, number_of_factors
Beispiel #2
0
def get_primes_to_try(begin_list, primes, target_num):
    begin_product = product_of_a_list(begin_list)

    # this function shall not be called if the following condition not met
    assert(begin_product <= target_num)

    last_in_begin_list = begin_list[len(begin_list) - 1]
    primes_to_try = []

    if(begin_product == target_num):
        return primes_to_try    # this is []

    else:
        product = begin_product

        for p in primes:
            # find the 1st p which is larger than last_in_begin_list
            if (p <= last_in_begin_list):
                continue

            product = begin_product*p

            if (product <= target_num):
                primes_to_try.append(p)

            else:
                break

    return primes_to_try
Beispiel #3
0
def is_this_end_of_tree_leaf(begin_list, primes, target_num):

    begin_product = product_of_a_list(begin_list)

    # from our algorithm, the following must be true
    assert(begin_product <= target_num)

    # find end leaf and it is a unique factorization
    # which means n = p1*p2*p3....*pi
    if(begin_product == target_num):
        is_end_leaf = g_END_LEAF_WITH_UNIQ_FACTORIZATION
        return is_end_leaf

    primes_to_try = get_primes_to_try(begin_list, primes, target_num)

    # if can not find next_prime_to_try,
    if(len(primes_to_try) == 0):
        is_end_leaf = g_END_LEAF_NOT_UNIQ_FACTORIZATION
        return is_end_leaf

    # To test if this is the end leaf, we only need to best with
    # the smallest primes in primes_to_try, because if begin_product*next_prime_to_try > target_num
    # then all other primes in primes_to_try will also fail the test
    next_prime_to_try = primes_to_try[0]

    product_with_extra_prime = begin_product*next_prime_to_try
    if (product_with_extra_prime > target_num):
        is_end_leaf = g_END_LEAF_NOT_UNIQ_FACTORIZATION
        
        # it seems that we will not reach here, let try to catch it to confirm this
        # it will be detected by  if(len(primes_to_try) == 0) in previous lines.
        assert(0)
        
    else:       # Note, even if product_with_extra_prime == target_num, this function still return 0
        is_end_leaf = g_NOT_END_OF_LEAF

    return is_end_leaf