Example #1
0
def euler49():
    for p in sieve.primerange(1488, 9999):
        for i in range(1, 9999 - p):
            if is_permutation(p, p + i) and is_permutation(
                    p, p + 2 * i) and isprime(p + i) and isprime(p + 2 * i):
                print(str(p) + str(p + i) + str(p + 2 * i))
                return
Example #2
0
def prime_permutations():
    """Input: None.
       Output: The concatenation of three 4-digit numbers
               that are prime permutations."""
    ps = primes(10000)
    ps = set(ps[ps > 1487])
    for p in ps:
        if p + 3330 in ps and p + 6660 in ps:
            if is_permutation(p, p + 3330) and is_permutation(p, p + 6660):
                return cat([p, p + 3330, p + 6660])
Example #3
0
def permuted_multiples():
    """Input: None.
       Output: The smallest positive integer x such that 2x,
               3x, 4x, 5x, and 6x are permutations of each other."""
    n = 125874
    while True:
        n += 1
        if is_permutation(n, 2 * n):
            if is_permutation(n, 3 * n):
                if is_permutation(n, 4 * n):
                    if is_permutation(n, 5 * n):
                        if is_permutation(n, 6 * n):
                            return n
Example #4
0
def permuted_multiples():
    """Input: None.
       Output: The smallest positive integer x such that 2x,
               3x, 4x, 5x, and 6x are permutations of each other."""
    n = 125874
    while True:
        n += 1
        if is_permutation(n, 2 * n):
            if is_permutation(n, 3 * n):
                if is_permutation(n, 4 * n):
                    if is_permutation(n, 5 * n):
                        if is_permutation(n, 6 * n):
                            return n
Example #5
0
def main():
    n = 3
    num_cube_permutations = 5

    is_found = False
    while not is_found:
        N = euler.cubic_number(n)
        num_digits = len(str(N))

        # generate all the cubic numbers with num_digits digits
        cubes = []
        while len(str(N)) <= num_digits:
            cubes.append(N)
            n += 1
            N = euler.cubic_number(n)

        # make a list of lists of permutations
        perms = []
        while cubes:
            c = cubes[0]
            p = [c]
            p.extend([d for d in cubes[1:] if euler.is_permutation(c, d)])
            for x in p:
                cubes.remove(x)
            perms.append(p)
        perms = [p for p in perms if len(p) == num_cube_permutations]
        if perms:
            is_found = True
    print(perms)
    print(min(perms[0]))
Example #6
0
def euler62():
    cubes = SortedSet(i**3 for i in range(10000))
    for i1, c1 in enumerate(cubes):
        count = 1
        for c2 in cubes[i1 + 1:]:
            if is_permutation(c1, c2): count += 1
        if count == 5:
            print(c1)
            return
Example #7
0
def euler62():
    cubes = SortedSet(i**3 for i in range(10000))
    for i1, c1 in enumerate(cubes):
        count = 1
        for c2 in cubes[i1+1:]:
            if is_permutation(c1, c2): count += 1
        if count == 5:
            print(c1)
            return
Example #8
0
def find_cubic_permutations(all_cubes, number):
    # goes through the cubes, and finds those that are are digit permutation of number.
    # to avoid having to go through all of them, we stop once we reach a cube whose
    # length is greater than our number. Assumes all_cubes is sorted
    cubic_permutations = []
    max_length = len(str(number))
    for x in all_cubes:
        if len(str(x)) > max_length:
            return cubic_permutations
        if is_permutation(x, number):
            cubic_permutations.append(x)
    return cubic_permutations
def slow_solve():
    min_ratio = 999999999
    for x in primes[200:LOWER_BOUND]:
        for y in primes:
            n = x*y
            if n > UPPER_BOUND:
                break
            p = phi(n)
            if is_permutation(n, p):
                ratio = n/p
                if ratio < min_ratio:
                    min_ratio = ratio
                    print("(x,y)=({},{}), n={}, phi(n)={}, ratio={}".format(x,y,n,p, n/p))
Example #10
0
def main():
    N = 10000000
    min_ratio = N
    min_n = 2

    for n in range(2, N):
        t = euler.totient(n)
        if euler.is_permutation(n, t):
            ratio = n / t
            if ratio < min_ratio:
                min_ratio = ratio
                min_n = n

    print(min_n)
Example #11
0
def euler70():
    result = 1
    min_ratio = 2
    limit = 10000000
    lowerbound, upperbound = int(0.7 * (limit)**0.5), int(1.3 * (limit)**0.5)
    for p in sieve.primerange(lowerbound, upperbound):
        for q in sieve.primerange(p + 1, upperbound):
            n = p * q
            if n > limit:
                break
            phi = (p - 1) * (q - 1)
            ratio = n / phi
            if is_permutation(n, int(phi)) and min_ratio > ratio:
                result, min_ratio = n, ratio
    print(result)
def fast_solve():
    # find out where to start
    sqrt_index = 0
    for i in range(len(primes)):
        if primes[i] > SQRT_UPPER:
            sqrt_index = i
            break

    x_index = sqrt_index
    while x_index > 0:
        print("Trying with x at index {} with value {}".format(x_index, primes[x_index]))
        y_index = sqrt_index
        while y_index < len(primes):
            n = primes[x_index] * primes[y_index]
            if n > UPPER_BOUND:
                break
            p = phi(n)
            if is_permutation(n, p):
                print("n={}, phi(n)={}, ratio={}".format(n,p, n/p))
                return
            y_index += 1
        x_index -= 1
Example #13
0
# coding=utf-8

'''
Problem 52
12 September 2003

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
'''

import euler
for i in range(1, 10**6):
  valid = True
  for j in range(2, 7):
    if not euler.is_permutation(i, i*j):
      valid = False
      break
  if (valid):
    print i
    break
Example #14
0
def euler52():
    print(next(i for i in count(start=99999, step=9) if is_permutation(i, 2*i, 3*i, 4*i, 5*i, 6*i)))
Example #15
0
def euler52():
    print(
        next(i for i in count(start=99999, step=9)
             if is_permutation(i, 2 * i, 3 * i, 4 * i, 5 * i, 6 * i)))
Example #16
0
# coding=utf-8
'''
Problem 52
12 September 2003

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
'''

import euler
for i in range(1, 10**6):
    valid = True
    for j in range(2, 7):
        if not euler.is_permutation(i, i * j):
            valid = False
            break
    if (valid):
        print i
        break
Example #17
0
def euler49():
    for p in sieve.primerange(1488, 9999):
        for i in range(1, 9999-p):
            if is_permutation(p, p+i) and is_permutation(p, p+2*i) and isprime(p+i) and isprime(p+2*i):
                print(str(p)+str(p+i)+str(p+2*i))
                return