1515
1616# Solution by Sam Gerber
1717
18- # This method blah
18+ # This method solves the problem above in the following manner:
19+ # 1.)To shrink the problem, the units places are observed.
20+ # =>There are only 36 ways to write a * b = c % 10 if a, b, and c are uniquely
21+ # =>selected from the digits 1-9. An array of these 36 triples is created by
22+ # =>the method units_arrays.
23+ # 2.)When multiplying an n-digit number by an m-digit number, the smallest
24+ # =>possible result is reached by:
25+ # => 10**(n-1) * 10**(m-1) which reduces to 10**(m + n - 2) which is an
26+ # => (m + n - 1)-digit number.
27+ # =>The largest possible result is reached by:
28+ # =>(10**n - 1) * (10**m - 1) which simplifies to 10**(m+n) - 10**m - 10**n
29+ # =>which is still an (m + n -1)-digit number.
30+ # =>So we can safely say that our equation must take one of two forms,
31+ # =>with a, b, and c being the units digits from the array and # representing
32+ # =>unknown digits:
33+ # => (a) * (###b) = (###c) ie 1-digit * 4-digit = 4-digit or
34+ # => (#a) * (##b) = (###c) ie 2-digit * 3-digit = 4-digit
35+ # 3.) With six unknown digits for each set of units digits triples (a,b,c),
36+ # =>there are 720 possible permutations of these remaining six digits, and
37+ # =>then we must test each of the two forms above. This brings the required
38+ # =>checks to 36 * 720 * 2 = 51_840.
39+
1940def pandigital_products
20- characters = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , 0 ]
21- equations = characters . permutation . sort . uniq
41+ products = [ ]
42+
43+ # Generate units digits array
44+ units_digits = units_arrays
45+
46+
47+ # Iterate through each set of units digits
48+ units_digits . each do |abc |
49+ other_six_digits = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
50+ a = other_six_digits . delete ( abc [ 0 ] )
51+ b = other_six_digits . delete ( abc [ 1 ] )
52+ c = other_six_digits . delete ( abc [ 2 ] )
53+
54+ all_ways_other_six = other_six_digits . permutation . to_a
55+ all_ways_other_six . each do |way |
56+
57+ # Check form (#) * (####) = (####)
58+ one_digit_number = a
59+ four_digit_number = [ way [ 0 ..2 ] , b ] . join . to_i
60+ product = [ way [ 3 ..5 ] , c ] . join . to_i
61+
62+ if one_digit_number * four_digit_number == product
63+ puts "#{ one_digit_number } * #{ four_digit_number } = #{ product } "
64+ products << product
65+ end
66+
67+ # Check form (##) * (###) = (####)
68+ two_digit_number = [ way [ 0 ] , a ] . join . to_i
69+ three_digit_number = [ way [ 1 ..2 ] , b ] . join . to_i
70+ # product is same as above
71+ if two_digit_number * three_digit_number == product
72+ puts "#{ two_digit_number } * #{ three_digit_number } = #{ product } "
73+ products << product
74+ end
75+
76+ end
77+ end
78+ products . uniq . inject ( :+ )
79+ end
80+
81+
82+ def units_arrays
2283 results = [ ]
2384
24- equations . each do |equation |
25- star_index = equation . index ( 0 )
26- break unless star_index > 0 && star_index < 5
27- number1 = equation [ 0 ...star_index ] . join . to_i
28- equation = equation [ star_index + 1 ..-1 ]
29- equal_index = equation . index ( 0 )
30- break unless equal_index > 0 && equal_index < equation . length - 1
31- number2 = equation [ 0 ...equal_index ] . join . to_i
32- product = equation [ equal_index + 1 ..-1 ] . join . to_i
33- results << [ number1 , number2 , product ] if number1 * number2 == product
85+ ( 1 ..9 ) . each do |a |
86+ ( 1 ..9 ) . each do |b |
87+ c = a * b % 10
88+ results << [ a , b , c ] if [ a , b , c ] . uniq . count == 3 && c > 0
89+ end
3490 end
3591
3692 results
3793end
3894
39-
40-
41-
95+ puts pandigital_products
4296
4397# This method returns true if the numbers contain all the
4498# digits from 1 to n
@@ -47,4 +101,5 @@ def is_pandigital?(*numbers, n)
47101 numbers . count == numbers . uniq . count && numbers . first == "1" && numbers . last == n . to_s
48102end
49103
50- print pandigital_products
104+
105+
0 commit comments