示例#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))
示例#2
0
def main(args):
    description = '''
    Largest prime factor

    The prime factors of 13195 are 5, 7, 13 and 29.

    What is the largest prime factor of the number 600851475143 ?
    '''

    if args.test:
        number = 13195
    else:
        number = 600851475143

    logging.debug('test with {}'.format(number))

    maxp = round(math.sqrt(number)) + 1
    ts1 = time.time()
    primes = PrimeNumberPool(maxp)
    ts2 = time.time()
    logging.debug('time for build prime number pool: {}'.format(ts2-ts1))
    factors = primes.getPrimeFactor(number)
    logging.debug('factors:{}'.format(factors))
    ts3 = time.time()
    logging.debug('time for factorize: {}'.format(ts3-ts2))
    solution = 'largest prime factor of the number {} is {}'.format(number, factors[-1])
    logging.info(solution)
示例#3
0
def main(args):
    if args.test:
        m = 3000
        max_phi = 1
        max_n = 1
        brute_force = 1
    else:
        m = 1000 * 1000
        max_phi = 3
        max_n = 6
        brute_force = 0

    prime = PrimeNumberPool(m)
    '''
    A strait forward way is to calculate the product of primes
    '''
    answer = 1
    for p in prime.numbers:
        while answer < m:
            answer *= p
    logging.info("answer:{}".format(answer))
    return 0

    max_num_prime_factors = 1
    for n in range(2, m + 1):
        if brute_force:
            phi = Phi(n, prime)
        else:
            '''
            Analysis:
            A lower phi happens when the number n has many prime factors
            '''
            factors = prime.getPrimeFactor(n)
            if len(factors) > max_num_prime_factors:
                max_num_prime_factors = len(factors)
                phi = Phi(n, prime)
                logging.debug("{} factors: {}, phi: {}".format(
                    n, factors, phi))
            else:
                continue
        logging.debug("{} phi = {}".format(n, phi))
        if (phi * max_phi < n):
            max_n = n
            max_phi = float(n) / phi
            logging.debug((max_n, max_phi))

    logging.info("answer:{}".format(max_n))
示例#4
0
def main(args):
    if args.test:
        m = 8 + 1
    else:
        m = 1000 * 12 + 1

    prime = PrimeNumberPool(m // 2)

    num = 0
    for d in range(3, m):
        d1 = d // 2 + 1

        pd = prime.getPrimeFactor(d)
        for n in range(d // 3 + 1, d1):
            if (HCF(n, pd)):
                logging.debug("{}/{}".format(n, d))
                num += 1

    logging.info("answer: {}".format(num))
示例#5
0
def main(args):
    prime = PrimeNumberPool()
    n = 100000
    rod = {}

    for k in range(1, n+1):
        f = prime.getPrimeFactor(k)
        p = np.product(f)
        if (p in rod):
            rod_p = rod[p]
            rod_p.append(k)
        else:
            rod[p] = [k]

    # assume rod's key is sorted
    e_n = []
    for k in sorted(list(rod.keys())):
        e_n += rod[k]

    logger.info("answer: {}".format(e_n[10000-1]))