Home python Acceleration of the kneading NOK two numbers

Acceleration of the kneading NOK two numbers

Author

Date

Category

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)))

when

n = 14
M = 21.

Answer:

42

when

n = 147
M = 214.

answer
31458

when

n = 14783678656
M = 21456735976.

answer

39651186209477312512

Working hours – instantly. Check.

p.s.
Math.gcd () Implemented in the Standard Library Starting with Python 3.5

version


Answer 3

in python 3.9 Added function for finding NOK:

import math
Print (Math.lcm (14, 21))

You can also use NUMPY :

import numpy as np
Print (NP.LCM (14, 21))

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions