Rating

In this article I show you the algorithm of how to perform the reduction or simplification of fractions quickly in NET Framework.

Hello that, today I bring you another function required to perform for newbies in programming, which is the Fractions Simplification. The structure is developed in VB.NET with framework 4.5.

It is much simpler than my previous solution of complex numbers, but it behaves almost in the same way; that is, I added:

-> Normal and character string constructors.

-> Conversion ToString () custom.

-> Overload of operators so that our rational numbers are handled directly in the line of code.

In addition to adding, subtracting, multiplying, dividing and presenting the fractions, add the famous Euclid's algorithm for its simplification, but in its version of reduction by remains, which I show below:

Public Shared Function mcdFast(eN As Integer, eD As Integer) As Integer

Dim DD, DS, R As Integer

'Buscamos cual es mayor divisor, para asignarlo

'a nuestra comprobacion de restos

DD = If(eN > eD, eN, eD)

DS = If(eN <= eD, eN, eD)

R = DD Mod DS 'Primer tanteo

While R > 0

'Dividimos e intercambiamos

'con el resto generado y volvemos a comprobar

DD = DS : DS = R

R = DD Mod DS

End While

Return DS

End Function

The simplified Euclid's algorithm above reads in modern language something like this:

To calculate the greatest common divisor between two positive integers a and b we divide the largest, say a, between the smallest, say b. This division will provide us with a quotient, "c1", and a remainder, "r1".

If "r1" = 0, then the gcd (between a and b) = b. If it is not zero, we divide the divisor, "c1", among the rest, "r1", obtaining another quotient, "c2", and another remainder, "r2". If "r2" = 0, then mcd (between a and b) = "r2". If it is not zero, we divide the divisor between the rest. And so on (we have the iteration loop).

Do not use recursion, because I really do not like it very much because the debugging process is a REAL HEADACHE. But I also included the recursive function that is quite widespread on the internet.

Is the next:

Public Shared Function mcd(eN As Integer, eD As Integer) As Integer

If eN Mod eD = 0 Then

Return eD

Else

Return mcd(eD, eN Mod eD)

End If

End Function

For the inexperienced eye, it will immediately assume that the initial function is slower than the recursive one, since it has more code, but it is not. As you can see, the one that implements without recursividad puts the name of mcdFast, and is that in fact it is almost 50% faster than the original recursive. So not always the recursion is the best option; ).

Advertising.

Creating a rational number in code line is defined as:

We create two fractions 1/2 and 1/3:

Dim NR1 = New Rutional Number(1, 2)

Dim NR2 = New Rutional Number("1/3")

As it is seen before, the constructor can construct the rational number with independent parameters or in presentation of chain of characters. Something important to add is that the fraction is always reduced at the end of the construction.

Once our rational numbers are created we can simply add them in the following way:

Dim Resultado As Rutional Number

Resultado = NR1 + NR2

We can then remove it by console screen:

Console.WriteLine("Resultado {0}", Resultado)

*We will see in console screen the following:*

Resultado 5/6

Below I leave the download link of the NumeroRacional structure program.

**Here the video of the explanation**

**Written by:**

**Elimar D.**

**10/04/2018**

**Totally free of viruses and malicious software, so do not wait any longer to download it now.**

**Do not forget to give us an opinion about the program, to improve the content.**

Advertising.

** ****NOTE:** Do not forget to follow us on our facebook and twitter social networks so that you become part of our community and receive the notification every time we post something new, so you will not miss anything.

**NO registration required!**

**2** Question(s) answered

If the question you have has not yet been answered here, use the form below to ask something about this addon.

- Asked by Gilberto Mendoza

*on 06/24/2018*Puedes subir un ejemplo en visual basic, pero no en console.... quisiera saber un ejemplo. Soy nuevo en programacion.gracias**Answer:**

Claro, esta semana lo hago Gilberto y lo subo. - Asked by Ivanna

*on 06/22/2019*¿Cómo puedo simplificar una fracción pero en lenguaje de visual básico en un formulario? Que no sea en console**Answer:**

Lo voy a hacer, y luego lo dejaré por aqui y por el canal Ivanna.