From bfab0b1af2423768ba1bf25942953f982637ca81 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Mon, 30 Aug 2021 15:22:38 +0200 Subject: [PATCH] Solution to problem 32 in Julia --- src/Julia/Problem032.jl | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/Julia/Problem032.jl diff --git a/src/Julia/Problem032.jl b/src/Julia/Problem032.jl new file mode 100644 index 0000000..3f66546 --- /dev/null +++ b/src/Julia/Problem032.jl @@ -0,0 +1,47 @@ +#= +Created on 30 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 32 of Project Euler +https://projecteuler.net/problem=32 +=# + +using BenchmarkTools + +function Problem32() + #= + 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. + HINT: Some products can be obtained in more than one way so be sure to only + include it once in your sum. + =# + + ans = Set() + pandigital = join(['1', '2', '3', '4', '5', '6', '7', '8', '9']) + + for x in 1:100 + for y in 100:10_000 + # product = x * y + if join(sort(collect(string(x) * string(y) * string(x * y)))) == pandigital + push!(ans, x * y) + end + end + end + + return sum(ans) +end + + +println("Time to evaluate Problem 32:") +@btime Problem32() +println("") +println("Result for Problem 32: ", Problem32())