Euler Problem 52

The problem is to find the smallest number n such that n, 2n, 3n, 4n, 5n, and 6n all contain the same digits. For those who know the 1/7 rule, that's wise of you. For the rest of us, brute force comes to rescue. Python can do it in a few lines:

 

n = 1
while not all( set(str(i*n))==set(str(n)) for i in [2,3,4,5,6] ):
      n+= 1	
print n

 The funny thing is if I change the all function slightly to:

     all( [ set(str(i*n))==set(str(n)) for i in [2,3,4,5,6] ] )

 the runtime increases by 4 times to 5 seconds. I am guessing all() is smart enough to employ short-circuit, but don't take my words for it.

Live and learn.

 

Posted on 2/23/2008 7:38:00 PM by Haoest

Permalink | Comments (0) | Post RSSRSS comment feed |

Categories: Project Euler | Python

Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

December 4. 2008 19:12