1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from math import gcd as HCF # consider the two three digit numbers for n1 in range(100, 1000): for n2 in range(n1 + 1, 1000): # find their highest common factor and lowest common multiple hcf = HCF(n1, n2) lcm = n1 * n2 // hcf # make sure that we have four different three digit numbers if hcf < 100 or lcm >= 1000 or len({hcf, n1, n2, lcm}) != 4: continue # Find HCF and LCM digits h, c1, f = hcf // 100, hcf // 10 % 10, hcf % 10 l, c2, m = lcm // 100, lcm // 10 % 10, lcm % 10 # check middle digits of HCF and LCM are the same # and that that h, c, f, l, m are all different if c1 != c2 or len({h, c1, f, l, m}) != 5: continue print(f"1st number = {n1}, 2nd number = {n2}") print(f"HCF = {hcf}, LCM = {lcm}") |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# return the digits of a three digit number def digits(x): ab, c = divmod(x, 10) return divmod(ab, 10) + (c,) # Since the two numbers must be co-prime multiples (m, n) # of the highest common factor, the four numbers are: # # hcf, a = m.hcf, b = n.hcf, lcm = m.n.hcf # # Since m and n are co-prime with a product less than 10, # the only possible values are m = 2, n = 3, which means # that the maximum value of hcf is 166 for hcf in range(100, 167): a, b, lcm = 2 * hcf, 3 * hcf, 6 * hcf # find the digits in the highest common factor # and the lowest common multiple d_hcf, d_lcm = digits(hcf), digits(lcm) # check that they have only one shared digit which is # also the tens digit in each if len(set(d_hcf + d_lcm)) == 5 and d_hcf[1] == d_lcm[1]: print(f"{a}, {b} (hcf = {hcf}, lcm = {lcm})") |