Program to find MMC in Python

Asked

Viewed 9,375 times

0

I was doing a basic exercise for the function While, he asked to create a program that found the MMC between 2 numbers... I managed to do, but the program continues to print the answer non-stop on loop. Where I went wrong?

num1 = int(input("Digite um número inteiro:"))
num2 = int(input("Digite outro número inteiro:"))

if num1 > num2:
    maior = num1
else:
    maior = num2
while True:
    if maior % num1 == 0 and maior % num2 == 0:
        print(maior)
    else:
        maior += 1
  • Maybe a break when finding the desired number? Right after the print?

  • @Jeffersonquesado Yes, it works, but the book still hasn’t taught this break: so there must be some way to do without it

  • So boot as stop condition, instead of making endless loop

5 answers

3

By the way a lazy and inefficient version: calculate the common multiples in [a.. a*b] and select the first...

[x for x in range(a,a*b+1) if x%b==0 and x%a==0][0]

2


You can simply add one break after printing its "major" variable, so it will interomper the while. That way:

num1 = int(input("Digite um número inteiro:"))
num2 = int(input("Digite outro número inteiro:"))

if num1 > num2:
    maior = num1
else:
    maior = num2
while True:
    if maior % num1 == 0 and maior % num2 == 0:
        print(maior)
        break
    else:
        maior += 1

I hope I’ve helped.

EDIT: Like you said you haven’t seen break try to do this way with a bow for that will make your code even more "clean".

num1 = int(input("Digite um número inteiro:"))
num2 = int(input("Digite outro número inteiro:"))

if num1 > num2:
    maior = num1
else:
    maior = num2

for i in range(maior):
    aux = num1 * i
    if (aux % num2) == 0:
        mmc = aux

print(mmc)

I hope I helped friend.

  • It works that way, but there is no way to do it without the break? The book that did the exercise had not taught 'break', so must have.

  • @Matthew has, I’ve done.

  • @Matthew made the changes, take a look.

  • @Victornogueira , this iterative solution seemed to force the break. Does this actually work in Python? As I recall, for Python works on generators, so it should simply ignore changes to the value of i

  • Yes, @Jeffersonquesado was a mistake, that line is irrelevant. I’ll edit.

  • To the entrance 4,6, the output should be 12. Check?

Show 1 more comment

2

I think this code is wrong, but if you’re gonna do it like this, then do it like this:

num1 = int(input("Digite um número inteiro:"))
num2 = int(input("Digite outro número inteiro:"))
maior = num1 if num1 > num2 else num2
while maior % num1 != 0 or maior % num2 != 0:
    maior += 1
print(maior)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • In case, the condition denied.

  • Test, input 2 and 3; expected result 6; entering loop, 3 % 2 will be non-zero, then the value of maior will not be changed, will be returned 3

  • @Jeffersonquesquesado como eu disse, this code is too weird, I answered only what was the focus of the question that is to end the loop endlessly and without use break that is horrible.

  • I also think the parole hearing is weird... wouldn’t it maior = num1 if num1>num2 else num2?

  • I would do the @Mrghost solution, but this incremental, although inefficient, would generate the right result.

  • 1

    I fixed those slips.

  • If it were a simple solution maybe, it seems to be on the right track, but I don’t think it needs all that.

Show 2 more comments

2

Really what was causing this loop was the condition while True: so there is no parameter that tells Python that this condition can be false and will always run the script. Follow an example here without using the break I hope it serves your purpose:

mdc = num1
i = num2

resto = None
while resto is not 0:
    resto = mdc % i
    mdc  = i
    i  = resto

resp = (mdc * i) / mdc
print("Resultado: "+str(resp))
  • Flame x of mdc and everything gets less obscure

  • 1

    Opa worth friend, good observation.

  • 1

    For those who do not know, this is the adaptation of Euclid’s algorithm to calculate the MMC. This works because num1 * num2 = mmc * mdc

1

I saw this question and did this function, the advantage is that it receives a list of numbers, and the MMC can be calculated beyond 2 numbers.

def mmc(nums):
    max_ = max(nums)
    i = 1
    while True:
        mult = max_ * i
        if all(mult%nr == 0 for nr in nums):
            return mult
        i += 1

>>> mmc([5, 6, 8])
>>> 120

Browser other questions tagged

You are not signed in. Login or sign up in order to post.