Voici l’algorithme permettant de calculer PGCD de deux nombres (Euclide) :
- Soit a et b deux nombres
- Tant que a est différent de b, si a est supérieur à b, a prend comme valeur a-b, sinon b prend la valeur b-a.
Il faudra par ailleurs s’assurer que numérateur et dénominateur ne sont pas nuls et ne traiter que deux nombres positifs. Tester avec une fraction construite avec comme numérateur -75 et comme dénominateur 90 (doit retourner 15).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PGCD
{
class Program
{
static void Main(string[] args)
{
int a, b;
a = -75;
b = 90;
if (a < 0)
{
a = -a;
}
if (b < 0)
{
b = -b;
}
while (a != b)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
Console.WriteLine(a);
Console.ReadKey();
}//fin PGCD
}
}
Flux RSS Clogique.com