This is just another example of a problem I decided to attempt as a break at work, trying to form the solution before the cookie on the Project Euler site ran out.

Problem

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.

Python Solution

This could be written in C or Cython to perform much more quickly, and I’m sure there are other optimizations that could be made to this code, but this was written and executed in a total of about three minutes using the Sage Notebook.

import time
 
start = time.time()
 
def numDigits(n):
    r"""
    return a sorted list of digits in a number
    """
    L = []
    while True:
        L.append(n % 10)
        n /= 10
        if n == 0: break
    L.sort()
    return L
 
i = 1
while True:
    a = numDigits(i)
    for j in range(1,7):
        b = numDigits(i * j)
        if a != b: break
        if j == 6:
            print i
            i = 0
    if i == 0: break
    i += 1
 
elapsed = time.time() - start
 
print "result found in %s seconds" % elapsed

When executed, we find the following result.

142857
result found in 1.92401385307 seconds

One Comment

  1. Posted May 13, 2014 at 19:57 | Permalink

    Orlando Magic jordan jerseys for kids,Cavaliers White basketball jerseys custom cheap,the best design, the aaa quality, the cheapest price.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">