I wrote the code for finding the NOC of the specified numbers, but it does not fit into the time frame. Please tell me how to speed it up.
b, n = map (int, input (). Split ()) m = max (b, n) While True: IF m% b == 0 and m% n == 0: Print (M) Break ELSE: m + = 1
Time to work program 5 sec.
Answer 1, Authority 100%
found an interesting example on the Internet.
As mentioned on the wiki:
import math N = 14. M = 21. Print ((n * m) // Math.gcd (N, M)) # GCD - Greatest Common Devisor
gcd () appeared only in Python version 3.5
Answer 2, Authority 57%
After a long, non-road-minded discussion –
Here you have a solution from scratch.
def gcd (a, b): While B: a, b = b, a% b Return A. DEF MCD (N, M): RETURN (N / GCD (N, M)) * M Print (int (mcd (n, m)))
n = 14 M = 21.
n = 147 M = 214.
n = 14783678656 M = 21456735976.
Working hours – instantly. Check.
Math.gcd () Implemented in the Standard Library Starting with Python 3.5
python 3.9 Added function for finding NOK:
import math Print (Math.lcm (14, 21))
You can also use
import numpy as np Print (NP.LCM (14, 21))