`Enigma' puzzles in `New Scientist' is really fun to solve. The current one which i just solved is `Digitally Divided Sum'. I am not posting the problem statement, which I am not sure, I can due to `Copyright' laws. But I can post the solution. :-) So here is my ruby code for that. Though not optimized, it gives a solution.
#Reverse engineer the problem statement
def get_digits(i)
tmp = Array.new
i.to_s.each_byte {|x|
tmp.push(x.chr.to_i)
}
return tmp
end
def is_divisible_by_its_digits(i)
list = get_digits(i)
if list.uniq != list
return false
end
list.each {|x|
if i%x != 0
return false
end
}
return true
end
def get_n_digit_list(start_no, end_no)
list = Array.new
while start_no < end_no
if start_no.to_s.include?("0") == false
if is_divisible_by_its_digits(start_no) == true
list.push(start_no)
end
end
start_no += 1
end
return list
end
def has_uniq_digits(a, b, c)
list = Array.new
list = get_digits(a)
list += get_digits(b)
list += get_digits(c)
if list.uniq == list
return true
else
return false
end
end
def main()
digit2 = get_n_digit_list(10, 100)
digit3 = get_n_digit_list(100, 1000)
digit4 = get_n_digit_list(1000, 10000)
digit4.each{|dig4|
digit3.each{|dig3|
digit2.each{|dig2|
if has_uniq_digits(dig2, dig3, dig4) == true
sum = dig2+dig3+dig4
if sum < 10000
if sum.to_s.include?("0") == false
if is_divisible_by_its_digits(sum) == true
list = get_digits(sum)
if list.uniq == list
p "#{dig2} + #{dig3} + #{dig4}"
return
end
end
end
end
end
}
}
}
end
main()
So what is the result?
Comments