Algoritmia - ALGORITMO MINIMO COMUN MULTIPLO

 
Vista:

ALGORITMO MINIMO COMUN MULTIPLO

Publicado por SONNY (5 intervenciones) el 17/03/2008 22:56:19
Por favor si alguien me puede ayudar con un algoritmo que al ingresar dos numeros me de el minimo comun multiplo.

GRACIAS
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
1
Responder

RE:ALGORITMO MINIMO COMUN MULTIPLO

Publicado por eduardo (55 intervenciones) el 19/03/2008 03:19:52
Conociendo la relacion MCM( a,b ) = a * b / MCD( a,b )
Podemos crear la funcion MCD ya conocida y obtener facilmente la función MCM

/* Función Máximo Común Divisor */
int MCD( int a, int b ) {
while( a != b ) {
if ( a > b )
a -= b;
else
b -= a;
}
return a;
}

/* Función Mínimo Común Múltiplo */
int MCM( int a, int b ) {
return ( a * b / MCD( a,b ) );
}

int main() {
int num1, num2;

do {
printf( " Ingrese primer entero: " );
scanf( "%d", &num1 );
printf( " Ingrese segundo entero: " );
scanf( "%d", &num2 );
} while( num1<= 0 || num2 <= 0 );

printf( " MCD( %d,%d) = %d ", num1, num2,MCD(num1,num2) );
printf( " MCM( %d,%d) = %d ", num1, num2,MCM(num1,num2) );

getch();
return 0;
}

/*Es muy probable q no compile ya que estoy en un ciber y no tengo compilador disponible*/

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:ALGORITMO MINIMO COMUN MULTIPLO

Publicado por Manusoftar® (1 intervención) el 08/04/2009 17:53:38
Otra posibilidad sin bucles del algoritmo para minimo común múltiplo:

#include <stdio.h>
#include <stdlib.h>

int mcd(int,int);

main(){
int a,b,mcm;
char s,t='s',u;
while (t=='s' || t=='S'){
printf(" Primer número:");
scanf("%d",&a);
fflush(stdin);
printf(" Segundo número:");
scanf("%d",&b);
fflush(stdin);
if (a==0 || b==0){
printf("No existe ningún mcm entre 0 y 0, o bien ha generado un error de división por 0 ");
exit(0);
}
mcm=(a*b)/mcd(a,b);
printf("El mínimo común múltiplo entre %d y %d es: %d ",a,b,mcm);
printf("Nuevo cálculo (s/n):");
scanf("%c%c%c",&s,&t,&u);

}
}

int mcd(int n, int m){
int result;
if (m > n){
m -= ((int)(m/n))*n;
n -= m;
result = n;
} else {
n -= ((int)(n/m))*m;
m -= n;
result = m;
}
return result;
}

Lo interesante de este algoritmo es que, si bien es más largo (tiene más líneas), la complejidad se ve reducida considerablemente ya que, independientemente de los números que se ingresen, solo se realisará una ejecución para calcular el mcd.

Este ejemplo está probado y compila sin errores.

PD: Lo programé en Linux, por eso no utiliso conio.h ni getch();

Atte. Manuel Fernández.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:ALGORITMO MINIMO COMUN MULTIPLO

Publicado por nerea (1 intervención) el 28/02/2010 16:02:18
esto es un ejercicio: 1Simplifica estas fracciones aplicando el m.c.d: Las fracciones-64 102 avos ,325 175avos , 105 315avos,350 210 avos,96 228 avos mi pregunta es ¿Como se hace?yo soy una niña de 11 alos y voy a 6º de primaria
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:ALGORITMO MINIMO COMUN MULTIPLO

Publicado por edith (1 intervención) el 17/12/2009 22:30:53
lagoritmop del mcm
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

ALGORITMO MINIMO COMUN MULTIPLO

Publicado por Rey (1 intervención) el 10/04/2010 22:24:34
using System;
using System.Collections.Generic;
using System.Text;

namespace Probando
{
class Program
{
static int EuclidesMCD(int a, int b)
{
int iaux; //auxiliar
a = Math.Abs(a); //tomamos valor absoluto
b = Math.Abs(b);
int i1 = Math.Max(a, b); //i1 = el más grande
int i2 = Math.Min(a, b); //i2 = el más pequeño
do
{
iaux = i2; //guardar divisor
i2 = i1 % i2; //resto pasa a divisor
i1 = iaux; //divisor pasa a dividendo
} while (i2 != 0);
return i1; //ultimo resto no nulo
}

static int EuclidesMCM(int a, int b)
{
return (a / EuclidesMCD(a, b)) * b;
}

static void Main(string[] args)
{
int a= int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());

Console.WriteLine(EuclidesMCM(a,b));

Console.ReadKey();
}
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar