Esempio n. 1
0
def main(args):
    if args.test:
        m = 10
    else:
        m = 1000*1000

    prime = PrimeNumberPool()

    md = 5
    mn = 2

    for d in range(2, m):
        if d == 7:
            continue
        n = d * 3 // 7
        if (n>mn*d/md and HCF(n, d, prime)):
            mn = n
            md = d
            logging.debug("{}/{}".format(mn, md))

    logging.info("answer: {}/{}".format(mn, md))
    logging.debug(prime.Factorize(mn))
    logging.debug(prime.getPrimeFactor(mn))
    logging.debug(prime.Factorize(md))
    logging.debug(prime.getPrimeFactor(md))
Esempio n. 2
0
def main(args):
    if args.test:
        m = 100 * 1000
        maxprime = 1000
    else:
        m = 10 * 1000 * 1000
        maxprime = 100000

    t1 = time.time()
    prime = PrimeNumberPool(maxprime)
    t2 = time.time()
    logging.debug("time for build prime pool:{}".format(t2 - t1))
    '''
    Analysis
    By observation, the numbers with high φ(n) are product of two prime numbers 
    '''
    max_ratio = (2, 1)
    num_prime = len(prime.numbers)
    for i in range(num_prime):
        p1 = prime.numbers[i]
        if p1 * p1 > m:
            break
        for j in range(i, num_prime):
            p2 = prime.numbers[j]
            if p1 * p2 > m:
                break
            n = p1 * p2
            phi = n - p1 - p2 + 1
            if (IsPermute(n, phi)):
                if (max_ratio[0] * phi > max_ratio[1] * n):
                    logging.debug("{} factor to {}".format(
                        n, prime.Factorize(n)))
                    max_ratio = (n, phi)
                    logging.debug(max_ratio)
    '''
    # brute force way
    for n in range(3,m,2):
        # pre-qualify
        pre_qualify = 1
        for p in prime.numbers:
            if (n % p == 0 and p*max_ratio[1] > (p-1)*max_ratio[0]):
                pre_qualify = 0
                break
            if (p * p > n):
                break
        if (pre_qualify):            
            pn = Phi(n,prime)
            if (IsPermute(n, pn)):
                if (max_ratio[0]*pn > max_ratio[1]*n):
                    logging.debug("{} factor to {}".format(n, prime.Factorize(n)))
                    max_ratio = (n, pn)
                    logging.debug(max_ratio)
    '''

    logging.info("answer: {}".format(max_ratio[0]))