I wrote a simple one-line set in julia to solve a little math problem: find a two-digit number, A and a three-digit number B, so that their product A x B is a five-digit number and each digit from 0 to 9 appears exactly once among the numbers A, B and A x B. For example,
54 x 297 = 16,038
Here is my julia code that finds all possible solutions:
println(filter(l -> length(unique(reduce(vcat, (map(digits, l))))) == 10, [[x, y, x*y] for x in Range(10:99), y in Range(100:999)]))
It solves the problem, but then I tried in python and came up with the following:
print filter(lambda y: len(set(''.join([str(x) for x in y])))==10, [[x, y, x*y] for x in range(10, 99) for y in range(100, 999)])
Dates of both of them, I was surprised to find that python code is more than twice as fast as julia code. Any suggestions for a faster approach for julia code (preferably one layer supported)?
Also: I know that I can improve both with quick tuning of ranges to range(12, 98) and range(102, 987) .
Update
Going beyond the limits of one liner, I took the advice that loops can be faster than lists, so I compared the following alternatives:
Julia
ans = Array{Tuple{Int32, Int32, Int32}}(0) for x in 12:98 for y in 102:987 if length(unique(digits(x+y*100+x*y*100_000)))==10 push!(ans, (x, y, x*y) end end end println(ans)
Python
ans = [] for x in range(12,98): for y in range(102,987): if len(set(str(x+y*100+x*y*100000)))==10: ans.append((x, y, x*y)) print ans
The python code is much faster (even if I change the code to just print the results in a loop, rather than collecting them in a list). I expected better performance from julia.
In addition, in case of your interest, a complete list of solutions
39 x 402 = 15,678 27 x 594 = 16,038 54 x 297 = 16,038 36 x 495 = 17,820 45 x 396 = 17,820 52 x 367 = 19,084 78 x 345 = 26,910 46 x 715 = 32,890 63 x 927 = 58,401