MCPcopy Index your code
hub / github.com/geekcomputers/Python / kgV

Function kgV

primelib/primelib.py:366–432  ·  view source on GitHub ↗

Least common multiple input: two positive integer 'number1' and 'number2' returns the least common multiple of 'number1' and 'number2'

(number1, number2)

Source from the content-addressed store, hash-verified

364
365
366def kgV(number1, number2):
367 """
368 Least common multiple
369 input: two positive integer 'number1' and 'number2'
370 returns the least common multiple of 'number1' and 'number2'
371 """
372
373 # precondition
374 assert (
375 isinstance(number1, int)
376 and isinstance(number2, int)
377 and (number1 >= 1)
378 and (number2 >= 1)
379 ), "'number1' and 'number2' must been positive integer."
380
381 ans = 1 # actual answer that will be return.
382
383 # for kgV (x,1)
384 if number1 > 1 and number2 > 1:
385 # builds the prime factorization of 'number1' and 'number2'
386 primeFac1 = primeFactorization(number1)
387 primeFac2 = primeFactorization(number2)
388
389 elif number1 == 1 or number2 == 1:
390 primeFac1 = []
391 primeFac2 = []
392 ans = max(number1, number2)
393
394 count1 = 0
395 count2 = 0
396
397 done = [] # captured numbers int both 'primeFac1' and 'primeFac2'
398
399 # iterates through primeFac1
400 for n in primeFac1:
401 if n not in done:
402 if n in primeFac2:
403 count1 = primeFac1.count(n)
404 count2 = primeFac2.count(n)
405
406 for i in range(max(count1, count2)):
407 ans *= n
408
409 else:
410 count1 = primeFac1.count(n)
411
412 for i in range(count1):
413 ans *= n
414
415 done.append(n)
416
417 # iterates through primeFac2
418 for n in primeFac2:
419 if n not in done:
420 count2 = primeFac2.count(n)
421
422 for i in range(count2):
423 ans *= n

Callers

nothing calls this directly

Calls 3

primeFactorizationFunction · 0.85
countMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected