We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
%run_procs %digit_split %pow %cartesian_product %echo %prime_list %prime_iterator %factor %product %divisors p32()-> Ans=run_procs(9877-1234,fun(I)->[euler,p32,[check,I+1233,self()]] end, fun(A,{done,B})->A+B end,0), io:format("~w~n",[Ans]). p32(check,What,Parent)-> D=divisors(What), p32(check,What,D,Parent,lists:sort(lists:subtract(lists:seq(1,9),digit_split(What)))). p32(check,_,[],Parent,_)->Parent!{done,0}; p32(check,What,[First|WithThese],Parent,Available)-> Other=What div First, case Available==lists:sort(lists:append(digit_split(First),digit_split(Other))) of true-> Parent!{done,What}; false-> p32(check,What,lists:delete(Other,WithThese),Parent,Available) end.
#cartesian_product #PrimeList #factors #divisors #Enumerable #dig_split def condense(a) a.join("").to_i end def p32 a=(1234..9876).select{|i|s=i.to_s;s.length == s.split("").uniq.length and not s.include?("0")}.select do |i| sq=Math.sqrt(i) d=divisors(i).to_a.sort.take_while{|j|j<sq} remaining=(1..9).to_a-dig_split(i) d.any?{|j|(dig_split(j)+dig_split(i/j)).sort==remaining} end puts a.sum end