Program does not execute the While command after reading in the scanf

Asked

Viewed 67 times

0

I was trying to make a program to calculate the mmc of two integer values, but I can’t even test my code, since no tested compiler has ever run , someone knows what might be?a tela fica assim ,sem executar os comandosFollows the code:

#include <stdio.h>
#include <conio.h>
int main()
{
int x,y,aux,primo,mmc,cont=0,i;
scanf("%d%d",&x,&y);
primo=2;
mmc=1;
while(x!=1 || y!=1)
{
    if(x%primo==0)
    {
        x=x/primo;
        cont++;
    }
    if(y%primo==0)
    {
        y=y/primo;
        cont++;
    }
    if(cont==0)
    {
        aux=0;
        while(aux=0)
        {
            cont=0;
            primo++;
            for(i=2;i<primo;i++)
            {
                if(primo%i==0)
                {
                    cont++;
                }
                if(cont=0)
                {
                    aux=1;
                }
                else
                {
                    primo++;
                }
            }
        }
    }   
    else
     mmc=mmc*primo;
}
printf("MMC=%d",mmc);
return 0;
}``
  • I don’t understand what you’re doing here: aux=0; while(aux=0) note that in while you’re assigning 0 to the aux variable, you might want to do: while(aux==0) idem here: if(cont=0)

1 answer

0

First let’s get to the problem of your algorithm:

    if(x%primo==0)
    {
        x=x/primo;
        cont++;
    }
    if(y%primo==0)
    {
        y=y/primo;
        cont++;
    }

We can observe in this section that if an even number is entered in the scanf();he’ll get into one of those first if() and consequently the cout will be increased and will never enter the

if(cont==0)

then the expression (x!=1 || y!=1) will be satisfied, thus creating a loop.

The point is that trying to calculate the MMC using the integer factorization method is an NP problem, there is no algorithm that can do this efficiently.

The simplest way to make an algorithm to compute the MMC of two integers is to use the property that the MMC and the MDC (maximum common divisor) have;

                                 MMC(a,b)= a x b / (MDC(a,b))

That is, we can calculate the MMC using the MDC. The advantage of this is that the MDC can be calculated efficiently through the Euclid algorithm:

int mdc(int a, int b){
    while(b != 0){
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

After that just play the result in the formula and the ice cream is ready !

I hope I helped XD

Browser other questions tagged

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