Project Euler 52: Ruby

Posted by Ben Griswold on Johnny Coder See other posts from Johnny Coder or by Ben Griswold
Published on Tue, 14 Sep 2010 03:36:02 +0000 Indexed on 2010/12/06 16:59 UTC
Read the original article Hit count: 245

Filed under:

In my attempt to learn Ruby out in the open, here’s my solution for Project Euler Problem 52

Compared to Problem 51, this problem was a snap. Brute force and pretty quick…

As always, any feedback is welcome.

# Euler 52
# 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.
timer_start =

def contains_same_digits?(n)
  value = (n*2).to_s.split(//).uniq.sort.join
  3.upto(6) do |i|
   return false if (n*i).to_s.split(//).uniq.sort.join != value


i = 100_000
answer = 0

while answer == 0
  answer = i if contains_same_digits?(i)

puts answer

puts "Elapsed Time: #{( - timer_start)*1000} milliseconds"

© Johnny Coder or respective owner

Related posts about languages

Related posts about Project Euler