If any solution is of interest, the NOC itself is the trivial (greatest) answer, since max(max(e, d), d) == max(e, d) , see canonical decomposition . If b and lcm(a, b) known, and you need to find any a , then the answer is: a = lcm(a, b) that is:
lcm(a, b) == lcm(lcm(a, b), b)
In order to find the smallest possible solution, in prime factorization, the multiplicity of primes in the lowest common multiple (LCM) is equal to the maximum multiplicity of both numbers (see the canonical decomposition ), therefore the multiplicity in the desired (minimum among several possible solutions) or equal to LCM, or zero, if the multiplicity in the first number is equal to the multiplicity in the LCM:
# a = p**d * ...; b = p**e * ...; lcm(a, b) = p**m ... where m = max(d, e) assert e <= m d = 0 if m == e else m
For small numbers, you can find prime factors by looking at all the divisors:
def a(b, lcm): """Find *a* given *lcm(a, b)* and *b*.""" a = 1 p = 2 # prime factor while p * p <= b: # find multiplicity of p in b e = 0 # b = p**e * ... while b % p == 0: # found prime factor e += 1 # increase multiplicity b //= p assert lcm % p == 0 lcm //= p # find multiplicity of p in lcm # lcm = p**m * ... m = e while lcm % p == 0: # d = max(d, e) m += 1 # increase multiplicity lcm //= p # a = p**d * ... if m > e: # max(d, e) > e a *= p**m # d == m else: # d = 0 assert m == e p += 1 assert lcm % b == 0 # last prime factor or 1 if lcm % b**2 != 0: # e = max(d, e) lcm //= b return a * lcm
<script src="https://cdn.rawgit.com/brython-dev/brython/3.4.0/www/src/brython.js"></script><body onload="brython()"><script type="text/python"> def a(b, lcm): """Find *a* given *lcm(a, b)* and *b*.""" a = 1 p = 2 # prime factor while p * p <= b: # find multiplicity of p in b e = 0 # b = p**e * ... while b % p == 0: # found prime factor e += 1 # increase multiplicity b //= p assert lcm % p == 0 lcm //= p # find multiplicity of p in lcm # lcm = p**m * ... m = e while lcm % p == 0: # d = max(d, e) m += 1 # increase multiplicity lcm //= p # a = p**d * ... if m > e: # max(d, e) > e a *= p**m # d == m else: # d = 0 assert m == e p += 1 assert lcm % b == 0 # last prime factor or 1 if lcm % b**2 != 0: # e = max(d, e) lcm //= b return a * lcm def gcd(a, b): while b: a, b = b, a%b return a def lcm(a, b): return a * b // gcd(a, b) from browser import document @document["mybutton"].bind("click") def on_click(event): b = int(document["b"].value) lcm_ = int(document["lcm"].value) a_ = a(b, lcm_) assert lcm_ == lcm(a_, b) print(f"a={a_}, b={b}, lcm={lcm(a_,b)}") on_click('dummy on start') </script><label for="b">b: </label><input id="b" value="16"> <label for="lcm">lcm: </label><input id="lcm" value="80"> <button id="mybutton">Найти a</button></body>
Examples of using:
>>> a(b=28, lcm=84) 3 >>> a(b=21, lcm=84) 4 >>> lcm(a(b=3, lcm=84), 3) 84 >>> a(b=2, lcm=4) 4 >>> a(b=4, lcm=4) 1 >>> a(b=20, lcm=80) 16 >>> a(b=16, lcm=80) 5 >>> a(b=1, lcm=lcm(2, 1)) 2 >>> a(b=2, lcm=lcm(2, 1)) 1 >>> lcm(a(b=2, lcm=lcm(2, 1)), 2) 2
where lcm(a, b) :
from math import gcd def lcm(a, b): return a * b // gcd(a, b)