def main(max_n):

    divisors = dict()

    prime_object = ProjectEulerPrime()

    for n in xrange(max_n, 1, -1):

        # from:
        # http://stackoverflow.com/questions/110344/algorithm-to-
        # calculate-the-number-of-divisors-of-a-given-number

        # we know that:
        # divisors(n) = product(each factor's multiplicity + 1)

        factorization = prime_object.factorize(n)

        multiplicities = Counter(factorization)

        divisors[n] = 1

        for factor in multiplicities:
            divisors[n] *= multiplicities[factor] + 1

    count = 0
    for n in divisors:
        if n + 1 in divisors and divisors[n] == divisors[n + 1]:
            count += 1

    print "Adjacent pairs with the same number of divisors: %d" % count
def main():
    
    LIMIT = 10 ** 5
    ITERATION = 10 ** 4
    p = ProjectEulerPrime()
    radDict = {}                        # product(rad(n)) = list of all n with that prime factorization
    for i in xrange(1, LIMIT + 1):
        product = reduce(mul, set(p.factorize(i)))
        if product not in radDict:
            radDict[product] = [i]      # since we iterate from 1..LIMIT, use a list to store n in order.
        else:
            radDict[product].append(i)
    
    print "Solution: ", getSortedValue(ITERATION, radDict)
def main():

    LIMIT = 4 * 10 ** 6
    p = ProjectEulerPrime()

    # using an algo inspired by hk:
    # http://projecteuler.net/thread=108

    # first step: get a product of unique primes
    # such that the number of divisors is >= LIMIT.
    factors = getUniqueFactorization(LIMIT, p)

    # second step: start minimizing the divisor number with a floor of LIMIT
    # do this by removing the largest prime and replacing it with
    # a group of primes s.t. their product is < the largest prime.

    bestDivisorNumber = getNumberOfDivisors(factors)
    bestFactorization = factors

    for largestPrime in reversed(factors):  # we need to try to replace each original prime once.

        iterationBestDivisorNumber = (
            bestDivisorNumber
        )  # these are used so as to not update the absolute minimized factorization
        iterationBestFactorization = bestFactorization  # until we finish a loop.

        for i in xrange(4, largestPrime):  # optimization: skip 2 and 3 as we can't replace primes with another prime.

            if p.isPrime(i):
                continue  # skip primes.

            newFactorization = p.factorize(i)

            newFactorization.extend(bestFactorization)
            newFactorization.remove(largestPrime)
            newDivisorNumber = getNumberOfDivisors(newFactorization)

            if newDivisorNumber >= LIMIT and newDivisorNumber < iterationBestDivisorNumber:
                iterationBestFactorization = newFactorization
                iterationBestDivisorNumber = newDivisorNumber

        bestFactorization = iterationBestFactorization
        bestDivisorNumber = iterationBestDivisorNumber

    print "Lowest number with >=", LIMIT, "possible 2 unit fraction additions: ", reduce(mul, bestFactorization)
    print "Number of additions: ", bestDivisorNumber
def slowAlgorithm():
    
    start = time()
    primeObject = ProjectEulerPrime()
    
    maxValue = LIMIT + 1
    maxRatio = 0
        
    for i in xrange(2, LIMIT + 1):
        # Euler's product formula:
        # phi(n) = n * product((1 - (1 / unique prime divisors))
        phi = i
        for prime in set(primeObject.factorize(i)):
            phi *= 1 - float(1) / prime
        
        if i / phi > maxRatio:
            maxRatio = i / phi
            maxValue = i
            
    end = time()
    print "Value for which ratio is maximized: ", maxValue
    print "Runtime: ", end - start, " seconds."
Exemple #5
0
def slowAlgorithm():

    start = time()
    primeObject = ProjectEulerPrime()

    maxValue = LIMIT + 1
    maxRatio = 0

    for i in xrange(2, LIMIT + 1):
        # Euler's product formula:
        # phi(n) = n * product((1 - (1 / unique prime divisors))
        phi = i
        for prime in set(primeObject.factorize(i)):
            phi *= 1 - float(1) / prime

        if i / phi > maxRatio:
            maxRatio = i / phi
            maxValue = i

    end = time()
    print "Value for which ratio is maximized: ", maxValue
    print "Runtime: ", end - start, " seconds."
Exemple #6
0
def main():

    p = ProjectEulerPrime()

    solutions = set()

    LIMIT = 12000

    for k in xrange(2, LIMIT + 1):

        for i in xrange(
                k, 2 * k + 1
        ):  # found via trial and error to the the range in which this number exists for a given k.

            factorization_generator = FactorizationGenerator(
                k=k, original_number=i, factorization=p.factorize(i))

            if factorization_generator.can_we_select_correct_factorization_using_k_numbers(
            ):
                solutions.add(i)
                break

    print "Solutions:", sum(solutions)
def main():
    
    p = ProjectEulerPrime()
    
    solutions = set()
    
    LIMIT = 12000
        
    for k in xrange(2, LIMIT + 1):
                
        for i in xrange(k, 2 * k + 1):  # found via trial and error to the the range in which this number exists for a given k.
            
            factorization_generator = FactorizationGenerator(k = k, original_number = i, factorization = p.factorize(i))
                                    
            if factorization_generator.can_we_select_correct_factorization_using_k_numbers():
                solutions.add(i)
                break

    print "Solutions:", sum(solutions)