Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8
- """
- Antonio Villanueva Segura
- Calculo de MCD de 3 numeros
- """
- #*******************************************************************************
- def esPrimo(num):
- """test si es un numero primo"""
- if num < 1:
- return False
- elif num == 2:
- return True
- else:
- for i in range(2, num):
- if num % i == 0:
- return False
- return True
- #*******************************************************************************
- def factores_primos (num):
- """crea factorizacion de n. primos """
- factores=[] #factores de este num
- x=1
- while (x <=num+1):
- if esPrimo(x) and num%x==0:
- factores.append(x)
- num=num/x
- x=1
- x+=1
- return factores
- #*******************************************************************************
- def mcd(num1,num2,num3):
- """calcula mcd de 2 numeros """
- mcd=[] #lista con mcd
- a=factores_primos (num1) #factores del 1er.
- b=factores_primos (num2) #factores del 2°.
- c=factores_primos (num3) #factores del 3er.
- for aa in a :
- if aa in b and aa in c:
- mcd.append(aa)
- b.remove(aa)
- c.remove(aa)
- return mcd
- #*******************************************************************************
- """ Bucle principal y zona de Tests"""
- """Ejemplo de calculo de mcd 24,36,40 que 2*2=4"""
- print (mcd(24,36,40))#Analisis del mcd de 24 36 y 40
- print (mcd(2,3,5))#Analisis del mcd de 24 36 y 40
- """Ejemplo de calculo de mcd 24,36 que 2*2*3=12"""
- print (mcd(24,36,36))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement