How to make a MDC algorithm with just sum and subtraction?

Asked

Viewed 236 times

1

How do I make an algorithm that calculates the MDC of two numbers only using sum and subtraction operations? It is prohibited to use operators other than those requested.

  • https://answall.com/tour

1 answer

1


Without comparison operations, there is no way to stop the algorithm, so I assume that other arithmetic operations are prohibited, not prohibiting comparison operations. OK?

To begin with, we normally use positive integers, so I will consider this condition and in case of arguments outside these conditions we must make the appropriate adaptations.

Second, if one could use calculation of rest of Euclidean division (usually the symbol is used %) could be calculated using the recursion sequence: mdc(a,b)=( b=0 ? a : mdc(b,a%b) ).

There is an algorithm for dividing rest of positive integers that follows recursion a%b=( a<b ? a : (a-b)%b ). It can also be implemented without recursion, thus.

resto( a , b )
    enquanto( a>=b )faça
        a <--- a-b
    retorna a

This means that one can calculate division remainder only with subtraction loop, not using the division operator. Thus, the calculation of mdc can swap the division for loop subtractions.

With this, the recursion of mdc becomes mdc(a,b)=( b=0 ? a : mdc(b,resto(a,b)) ). Refine the mdc algorithm, it gets like this.

mdc( a , b )
    enquanto( b>0 )faça
        enquanto( a>b )faça
            a <--- a-b
        b <-> a
    retorna( a )

Have any questions?

  • Hi, I managed to make an algorithm that solved, comparison operators are allowed. In fact I could not simply I took shame to open a calculus book, kkk. I used Euclid’s algorithm for calculating MDC. Basically it works as you described it here.

  • Okay. Too bad I didn’t help. You can finish the question so it doesn’t get pending?

  • Imagine, man, thanks for the help anyway! I’m kind of impatient.

Browser other questions tagged

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