Professional Documents
Culture Documents
Tarea 2 Program
Tarea 2 Program
Tarea 2 Program
LENGUAJE JAVA
public static void main(String[] args) {
System.out.println("Hello World!");
mostrarDivisoresAsc( 15);
mostrarDivisoresDesc( 15 );
System.out.println(par (1));
mostrarDivisoresPares( 15);
mostrarDivisoresImpares(15);
mostrarDivisores( 15, 2, 10);
System.out.println(cantidadDivisores( 15 ));
System.out.println(cantidadDivisoresPares( 15 ));
System.out.println(cantidadDivisores( 15,2,10 ));
System.out.println(proximoDivisor( 15,2 ));
System.out.println( anteriorDivisor( 15 , 5));
divisoresComunes(15 , 30);
System.out.println(anteriorDivisorComun( 50, 40, 15 ));
System.out.println( mcdEuclides( 10, 15));
System.out.println( mcd( 10, 15));
System.out.println( mcd3( 10, 15,20));
System.out.println( mcd4( 10, 15,20,45));
System.out.println( primo( 10));
mostrarPrimos(3,5 );
System.out.println( cantidadPrimos(3,5));
mostrarPrimos(5, 10);
mostrarDivPrimos(24);
int n = 60;
System.out.println(sumaDivisoresPrimos(n));
int n2 = 17;
System.out.println(siguientePrimo(n2));
int n3 = 19;
System.out.println(anteriorPrimo(n3));
int i = 5;
System.out.println(iesimoPrimo(i));
int n4 = 600;
mostrarFactoresPrimos(n4);
System.out.println(dosPrimos(7, 10));
System.out.println(dosPrimos(7, 10, 11));
System.out.println(dosPrimos(7, 10, 11, 15));
System.out.println(tresPrimos(7, 10, 11));
System.out.println(tresPrimos(7, 10, 11, 13));
System.out.println(sonDivisores(24, 2, 3));
System.out.println(sonDivisores(24, 2, 3));
System.out.println(sonDivisores(24, 2, 3, 4));
System.out.println(sonDivisores(24, 2, 3, 4, 6));
System.out.println(sonMultiplos(5, 2, 10));
System.out.println(sonMultiplos(2, 4, 6));
System.out.println(sonMultiplos(2, 4, 6, 8));
System.out.println(sonMultiplos(2, 4, 6, 8, 10));
System.out.println(alterno(5, 6));
System.out.println(alterno(5, 6, 7));
System.out.println(alterno(5, 6, 7, 8));
System.out.println(alterno(5, 6, 7, 8, 9));
}
public static void mostrarDivisoresAsc( int n ){
int n1=n , i = 1;
while (i <= n1) {
if (n1 % i == 0 )
System.out.println(i);
i=i+1;
}
}
public static void mostrarDivisoresDesc( int n ){
int n1=n , i = n ;
while (i <= n1) {
if (n1 % i == 0 )
System.out.println(i);
i =i-1;
}
}
public static boolean par( int n){
return n % 2 == 0;
}
public static void mostrarDivisoresPares(int n) {
int n1 = n;
int i = 1;
while (i <= n1) {
if (n1 % i == 0 )
if (i % 2 ==0)
System.out.println(i);
i = i + 1;
}
}
public static void mostrarDivisoresImpares(int n) {
int n1 = n;
int i = 1;
while (i <= n1) {
if (n1 % i == 0 )
if (i % 2 !=0)
System.out.println(i);
i = i + 1;
}
}
public static void mostrarDivisores( int n, int a, int b ){
int n1=n , i = a;
while (i <= b ) {
if (n1 % i == 0 )
System.out.println(i);
i=i+1;
}
}
public static int cantidadDivisores( int n ){
int n1=n , i = 1, c = 0;
while (i <= n1) {
if (n1 % i == 0 )
c = c +1 ;
i=i+1;
}
return c;
}
public static int cantidadDivisoresPares(int n) {
int n1 = n ,c = 0;
int i = 1;
while (i <= n1) {
if (n1 % i == 0 )
if (i % 2 ==0)
c = c +1 ;
i = i + 1;
}
return c ;
}
public static int cantidadDivisores( int n, int a, int b ){
int n1=n , i = a, c = 0;
while (i <= b ) {
if (n1 % i == 0 )
c = c +1 ;
i=i+1;
}
return c ;
}
public static int proximoDivisor( int n , int a ){
int n1=n , i = a, c = 0;
while(c == 0){
if (n1 % i == 0 )
c = c+ i ;
i=i+1;
}
return c ;
}
if (n1 % i == 0 )
c = c+ i ;
i = i -1 ;
}
return c ;
}
public static void divisoresComunes( int a , int b){
int i = 1;
while (i <= a && i <= b) {
if (a % i == 0 && b % i == 0 )
System.out.println(i);
i=i+1;
}
}
public static int anteriorDivisorComun( int a , int b , int n ){
int i = n- 1 , c = 0;
while(c == 0){
if (a % i == 0 && b % i == 0 )
c = c+ i ;
i = i -1 ;
}
return c ;
}
public static int mcdEuclides( int a , int b){
int i = 1, maximo = 0;
while (i <= a && i <= b) {
if (a % i == 0 && b % i == 0 )
maximo=i ;
i=i+1;
}
return maximo;
}
public static int mcd( int a , int b){
int i = 1, maximo = 0;
while (i <= a && i <= b) {
if (a % i == 0 && b % i == 0 )
maximo=i ;
i=i+1;
}
return maximo;
}
public static int mcd3(int a, int b , int c ){
int i = 1, maximo = 0, fe = 1;
while (i < a && i < b && i < c ) {
if ((a % i == 0 && b % i == 0) || (b % i == 0 && c % i == 0) || (a
% i == 0 && c % i == 0 ) )
maximo=i * fe ;
fe = maximo;
i=i+1;
}
return fe;
}
public static int mcd4(int a, int b , int c, int d ){
int i = 1, maximo = 0, fe = 1;
while (i < a && i < b && i < c && i < d ) {
if ((a % i == 0 && b % i == 0) || (b % i == 0 && c % i == 0) || (a
% i == 0 && c % i == 0 ) ||(a % i == 0 && d % i == 0 ) ||(d % i == 0 &&
b% i == 0 )||(d % i == 0 && c% i == 0 ))
maximo=i * fe ;
fe = maximo;
i=i+1;
}
return fe;
}
public static boolean primo(int n){
int i= 2, lim= n/2;
while (i <= lim ){
if (n % i == 0)
return false ;
else i = i+1;
}
return true;
}
public static void mostrarPrimos(int a,int b ){
int i= a;
while (i <= b){
if (primo(i))
System.out. println(i);
i = i +1 ;
}
}
public static int cantidadPrimos(int a,int b ){
int i= a , c = 0;
while (i <= b){
if (primo(i))
c= c +1;
i = i +1 ;
}
return c ;
}
def par(n):
return n % 2 == 0
def mostrarDivisoresPares(n):
i=1
while i <= n:
if n % i == 0 and i % 2 == 0:
print(i)
i += 1
def mostrarDivisoresImpares(n):
i=1
while i <= n:
if n % i == 0 and i % 2 != 0:
print(i)
i += 1
def cantidadDivisores(n):
i=1
c=0
while i <= n:
if n % i == 0:
c += 1
i += 1
return c
def cantidadDivisoresPares(n):
i=1
c=0
while i <= n:
if n % i == 0 and i % 2 == 0:
c += 1
i += 1
return c
def primo(n):
i=2
lim = n // 2
while i <= lim:
if n % i == 0:
return False
else:
i += 1
return True
def mostrarPrimos(a, b):
i=a
while i <= b:
if primo(i):
print(i)
i += 1
def mostrarDivPrimos(n):
for i in range(2, n + 1):
if n % i == 0 and es_primo(i):
print(i)
def sumaDivisoresPrimos(n):
suma = 0
for i in range(2, n + 1):
if n % i == 0 and es_primo(i):
suma += i
return suma
def siguientePrimo(n):
next_num = n + 1
while True:
if es_primo(next_num):
return next_num
next_num += 1
def anteriorPrimo(n):
prev_num = n - 1
while prev_num > 1:
if es_primo(prev_num):
return prev_num
prev_num -= 1
return n
def iesimoPrimo(i):
count = 0
num = 2
while True:
if es_primo(num):
count += 1
if count == i:
return num
num += 1
def mostrarFactoresPrimos(n):
factor = 2
while factor <= n:
if n % factor == 0:
print(factor, end=", ")
n = n / factor
else:
factor += 1
def es_primo(num):
if num < 2:
return False
for i in range(2, num):
if num % i == 0:
return False
return True
def main():
mostrarDivisoresAsc(15)
mostrarDivisoresDesc(15)
print(par(1))
mostrarDivisoresPares(15)
mostrarDivisoresImpares(15)
mostrarDivisores(15, 2, 10)
print(cantidadDivisores(15))
print(cantidadDivisoresPares(15))
print(cantidadDivisores(15, 2, 10))
print(proximoDivisor(15, 2))
print(anteriorDivisor(15, 5))
divisoresComunes(15, 30)
print(anteriorDivisorComun(50, 40, 15))
print(mcdEuclides(10, 15))
print(mcd(10, 15))
print(mcd3(10, 15, 20))
print(mcd4(10, 15, 20, 45))
print(primo(10))
mostrarPrimos(3, 5)
print(cantidadPrimos(3, 5))
print = mostrarPrimos(5, 10)
mostrarDivPrimos(30)
print = (sumaDivisoresPrimos(30))
print = (siguientePrimo(19))
print(anteriorPrimo(10))
print(iesimoPrimo(5))
mostrarFactoresPrimos(600)
print(dosPrimos(4, 6))
print(dosPrimos(4, 6, 9))
print(dosPrimos(4, 6, 9, 10))
print(tresPrimos(3, 6, 5))
print(tresPrimos(3, 6, 5, 7))
print(sonDivisores(10, 2, 5))
print(sonDivisores(12, 3, 6, 2))
print(sonDivisores(24, 3, 6, 2, 4))
print(sonMultiplos(9, 3))
print(sonMultiplos(10, 2, 5))
print(sonMultiplos(12, 3, 6, 2))
print(sonMultiplos(24, 3, 6, 2, 4))
print(alterno(3, 4))
print(alterno(3, 4, 5))
print(alterno(3, 4, 5, 6))