Esempio n. 1
0
def main():
    
    start = time()
    
    A = 1777
    B = 1855
    
    phiLimit = phi(LIMIT)
    
    '''
    we use efficient modular exponentiation to do this.
    essentially, there are several highly efficient ways 
    of computing (a ^ b) mod n. Python just so happens to use 
    binary exponentiation in its built-in pow() function.
    http://en.wikipedia.org/wiki/Modular_exponentiation
    
    we also use Euler's theorem:
    http://en.wikipedia.org/wiki/Euler's_theorem
    '''
    residual = A
    for i in xrange(B - 2): residual = pow(A, residual, phiLimit) 
    residual = pow(A, residual, LIMIT)
    
    print "Last ", DIGITS_TO_SEE, " digits from the tetration (", A, ", ", B, "): ", residual
    end = time()
    print "Runtime: ", end - start, " seconds."
Esempio n. 2
0
def main():

    start = time()

    A = 1777
    B = 1855

    phiLimit = phi(LIMIT)
    '''
    we use efficient modular exponentiation to do this.
    essentially, there are several highly efficient ways 
    of computing (a ^ b) mod n. Python just so happens to use 
    binary exponentiation in its built-in pow() function.
    http://en.wikipedia.org/wiki/Modular_exponentiation
    
    we also use Euler's theorem:
    http://en.wikipedia.org/wiki/Euler's_theorem
    '''
    residual = A
    for i in xrange(B - 2):
        residual = pow(A, residual, phiLimit)
    residual = pow(A, residual, LIMIT)

    print "Last ", DIGITS_TO_SEE, " digits from the tetration (", A, ", ", B, "): ", residual
    end = time()
    print "Runtime: ", end - start, " seconds."
Esempio n. 3
0
def main():

    start = time()

    solution = 0
    LOOP_LIMIT = 6
    MOD_LIMIT = 14**8
    PHI_LIMIT = phi(MOD_LIMIT)

    powDict = {}

    for i in xrange(LOOP_LIMIT + 1):
        newSolution = ackermann(i, MOD_LIMIT, PHI_LIMIT, powDict)
        print i, " : ", newSolution
        solution += newSolution

    print "Solution: ", solution % MOD_LIMIT

    end = time()
    print "Runtime: ", end - start, " seconds."
Esempio n. 4
0
def main():
    
    start = time()
    
    solution = 0
    LOOP_LIMIT = 6
    MOD_LIMIT = 14 ** 8
    PHI_LIMIT = phi(MOD_LIMIT)
    
    powDict = {}
    
    for i in xrange(LOOP_LIMIT + 1): 
        newSolution = ackermann(i, MOD_LIMIT, PHI_LIMIT, powDict)
        print i, " : ", newSolution
        solution += newSolution
    
    print "Solution: ", solution % MOD_LIMIT
    
    end = time()
    print "Runtime: ", end - start, " seconds."
Esempio n. 5
0
def main():

    # generate primes.
    p = ProjectEulerPrime()
    primes = {2}
    for i in xrange(3, 100000, 2):
        if p.isPrime(i):
            primes.add(i)

    ''' 
        We use the following facts:
            R(n) mod(n)
            => ((10 ** n - 1) / 9) mod n 
            => (10 ** n) mod (9 * n) = 1

            x ** y (mod n) == x ** (y mod phi(n)) (mod n)
        
        We stop once we detect a repeated residual
        (which means that we've entered a period).
        By sheer luck, I used a similar technique in problem 282
        to collapse much larger power towers.
    '''
    results = set()
    for prime in primes:
        phi_mod = phi(9 * prime)
        mod = 9 * prime
        seen_residuals = set()
        j = 1
        while True:
            residual = pow(10, pow(10, j, phi_mod), mod)
            if residual in seen_residuals:
                break
            if residual == 1:
                results.add(prime)
                break
            seen_residuals.add(residual)
            j += 1

    print "Sum: {}".format(sum(primes - results))
Esempio n. 6
0
def main():

    # generate primes.
    p = ProjectEulerPrime()
    primes = {2}
    for i in xrange(3, 100000, 2):
        if p.isPrime(i):
            primes.add(i)
    ''' 
        We use the following facts:
            R(n) mod(n)
            => ((10 ** n - 1) / 9) mod n 
            => (10 ** n) mod (9 * n) = 1

            x ** y (mod n) == x ** (y mod phi(n)) (mod n)
        
        We stop once we detect a repeated residual
        (which means that we've entered a period).
        By sheer luck, I used a similar technique in problem 282
        to collapse much larger power towers.
    '''
    results = set()
    for prime in primes:
        phi_mod = phi(9 * prime)
        mod = 9 * prime
        seen_residuals = set()
        j = 1
        while True:
            residual = pow(10, pow(10, j, phi_mod), mod)
            if residual in seen_residuals:
                break
            if residual == 1:
                results.add(prime)
                break
            seen_residuals.add(residual)
            j += 1

    print "Sum: {}".format(sum(primes - results))
Esempio n. 7
0
def main():

    RATIO = (15499, 94744)
    cache = dict()

    default = float('inf')
    bestSoFar = default
    '''
    So, here are the facts:
        * phi(n) gives the number of positive integers < n that are relatively prime to n.
        * if m and n are relatively prime to each other, phi(m * n) = phi(m) * phi(n).
        * phi(n) is always nearly n. 
    So we want to focus on doing as much work as possible with small n. So, the algo is as follows:
        1). iterate from 2 to infinity. Call this i.
        2). calculate phi(i). store this in a cache.
        3). find phi(m * i) for each m in the cache.
        4). if we find an element that works, stop the iteration (since the keys are unordered, go through
            all keys first and pick the smallest m * i).
    '''

    i = 2
    while True:
        value = phi(i)
        cache[i] = value
        for num in cache.keys():
            if gcd(num, i) == 1:
                cache[num * i] = cache[num] * value
                if num * i < bestSoFar and (RATIO[1] * cache[num * i]) < (
                    (num * i - 1) * RATIO[0]):
                    bestSoFar = num * i
        if bestSoFar != default:
            break
        else:
            i += 1

    print "Solution: {}".format(bestSoFar)
Esempio n. 8
0
def main():

    RATIO = (15499, 94744)
    cache = dict()

    default = float('inf')
    bestSoFar = default

    '''
    So, here are the facts:
        * phi(n) gives the number of positive integers < n that are relatively prime to n.
        * if m and n are relatively prime to each other, phi(m * n) = phi(m) * phi(n).
        * phi(n) is always nearly n. 
    So we want to focus on doing as much work as possible with small n. So, the algo is as follows:
        1). iterate from 2 to infinity. Call this i.
        2). calculate phi(i). store this in a cache.
        3). find phi(m * i) for each m in the cache.
        4). if we find an element that works, stop the iteration (since the keys are unordered, go through
            all keys first and pick the smallest m * i).
    '''

    i = 2
    while True:
        value = phi(i)
        cache[i] = value
        for num in cache.keys():
            if gcd(num, i) == 1:
                cache[num * i] = cache[num] * value
                if num * i < bestSoFar and (RATIO[1] * cache[num * i]) < ((num * i - 1) * RATIO[0]):
                    bestSoFar = num * i
        if bestSoFar != default:
            break
        else:
            i += 1

    print "Solution: {}".format(bestSoFar)
Esempio n. 9
0
for n in range(2, 1000000):
    #phi_val = mpz(-1)
    #factors = factorize(n, True)
    #for i in factors:
    #    for j in factors:
    #        if i == j:
    #            continue
    #        if i*j == n and gcd(i,j) == 1:
    #            phi_val = phi_cache[i]*phi_cache[j]
    #            print('+ '),
    #            break
    #    if int(phi_val) != -1:
    #        break
    #if int(phi_val) == -1:
    #    print('- '),
    phi_val = phi(n)

    #phi_cache[n] = phi_val
    val = n/mpq(phi_val)

    print('%d  ' % n),
    print('%d  ' % phi_val),
    print('%f' % val)

    if val > max:
        max = val
        maxN = n

print(maxN),
print(max)