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: 428
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 # http://projecteuler.net/index.php?section=problems&id=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 = Time.now 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 end true end i = 100_000 answer = 0 while answer == 0 answer = i if contains_same_digits?(i) i+=1 end puts answer puts "Elapsed Time: #{(Time.now - timer_start)*1000} milliseconds"
© Johnny Coder or respective owner