Problem 38

Problem 38

Take the number 192 and multiply it by each of 1, 2, and 3:

192 1 = 192
192 2 = 384
192 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, … , n) where n 1?

Mam rozwiazanie:

#!/usr/bin/env ruby x, y, z = 0 2.upto(9) do |n| a = 10**(9/n-1) while true pan = (1..n).inject("") { |s, i| s+(i*a).to_s }.to_i break if pan >= 1000000000 if pan.to_s.split("").sort.join == "123456789" x, y, z = pan, a, n if x < pan end a += 1 end end puts "pan=#{x}, a=#{y}, n=#{z}"

[code=ruby]p (1…10000).map {|n|
s,i = “”,1
while s.size < 9
s << (i*n).to_s
i += 1
end

i > 2 && s.size == 9 && s.split(’’).sort == (1…9).map(&:to_s) ? s.to_i : 0
}.max[/code]