Tarea 2 Program

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

TAREA-2.

DIVISORES, NÚMEROS PRIMOS, MCD Y


LÓGICA.
EJERCICIOS. CICLOS CON DIVISORES Y MÚLTIPLOS.
TRABAJO INDIVIDUAL.

NOMBRE COMPLETO: JOSEFINA FLOR ILLANES ARTEAGA

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 ;
}

public static int anteriorDivisor( int n , int a ){


int n1=n , i = a- 1 , c = 0;
while(c == 0){

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 ;
}

public static boolean esPrimo(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

public static void mostrarPrimos(int n, int a) {


int count = 0;
int current = a;
while (count < n) {
if (esPrimo(current)) {
System.out.print(current + " ");
count++;
}
current++;
}
System.out.println();
}

public static void mostrarDivPrimos(int n) {


for (int i = 1; i <= n; i++) {
if (n % i == 0 && esPrimo(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}

public static int sumaDivisoresPrimos(int n) {


int suma = 0;
for (int i = 2; i <= n; i++) {
if (n % i == 0 && esPrimo(i)) {
suma += i;
}
}
return suma;
}
public static int siguientePrimo(int n2) {
int siguiente = n2 + 1;
while (!esPrimo(siguiente)) {
siguiente++;
}
return siguiente;
}
public static int anteriorPrimo(int n3) {
int anterior = n3 - 1;
while (anterior > 1 && !esPrimo(anterior)) {
anterior--;
}
return anterior;
}
public static int iesimoPrimo(int i) {
int count = 0;
int current = 2;
while (count < i) {
if (esPrimo(current)) {
count++;
}
current++;
}
return current - 1;
}
public static void mostrarFactoresPrimos(int n4) {
for (int i = 2; i <= n4; i++) {
while (n4 % i == 0 && esPrimo(i)) {
System.out.print(i + " ");
n4 /= i;
}
}
System.out.println();
}
// III. IMPLEMENTAR LAS SIGUIENTES FUNCIONES LÓGICAS:
public static boolean dosPrimos(int a, int b) {
int count = 0;
if (esPrimo(a)) {
count++;
}
if (esPrimo(b)) {
count++;
}
return count == 2;
}
public static boolean dosPrimos(int a2, int b2, int c2) {
int count = 0;
if (esPrimo(a2)) {
count++;
}
if (esPrimo(b2)) {
count++;
}
if (esPrimo(c2)) {
count++;
}
return count == 2;
}
public static boolean dosPrimos(int a, int b, int c, int d) {
int count = 0;
if (esPrimo(a)) {
count++;
}
if (esPrimo(b)) {
count++;
}
if (esPrimo(c)) {
count++;
}
if (esPrimo(d)) {
count++;
}
return count == 2;
}
public static boolean tresPrimos(int a, int b, int c) {
int count = 0;
if (esPrimo(a)) {
count++;
}
if (esPrimo(b)) {
count++;
}
if (esPrimo(c)) {
count++;
}
return count == 3;
}
public static boolean tresPrimos(int a, int b, int c, int d) {
int count = 0;
if (esPrimo(a)) {
count++;
}
if (esPrimo(b)) {
count++;
}
if (esPrimo(c)) {
count++;
}
if (esPrimo(d)) {
count++;
}
return count == 3;
}
public static boolean sonDivisores(int a, int b, int... otros) {
for (int num : otros) {
if (a % num != 0) {
return false;
}
}
return true;
}
public static boolean sonDivisores(int a, int b, int c) {
return a % b == 0 && a % c == 0;
}

public static boolean sonDivisores(int a, int b, int c, int d) {


return a % b == 0 && a % c == 0 && a % d == 0;
}
public static boolean sonDivisores(int a, int b, int c, int d, int e) {
return a % b == 0 && a % c == 0 && a % d == 0 && a % e == 0;
}
public static boolean sonMultiplos(int a, int b, int... otros) {
for (int num : otros) {
if (num % a != 0) {
return false;
}
}
return true;
}
public static boolean sonMultiplos(int a, int b, int c) {
return b % a == 0 && c % a == 0;
}
public static boolean sonMultiplos(int a, int b, int c, int d) {
return b % a == 0 && c % a == 0 && d % a == 0;
}
public static boolean sonMultiplos(int a, int b, int c, int d, int e) {
return b % a == 0 && c % a == 0 && d % a == 0 && e % a == 0;
}
public static boolean alterno(int a, int b) {
boolean esPrimoA = esPrimo(a);
boolean esPrimoB = esPrimo(b);
return (esPrimoA && !esPrimoB) || (!esPrimoA && esPrimoB);
}
public static boolean alterno(int a, int b, int c) {
boolean esPrimoA = esPrimo(a);
boolean esPrimoB = esPrimo(b);
boolean esPrimoC = esPrimo(c);
return (esPrimoA && !esPrimoB && esPrimoC) || (!esPrimoA &&
esPrimoB && !esPrimoC);
}
public static boolean alterno(int a, int b, int c, int d) {
boolean esPrimoA = esPrimo(a);
boolean esPrimoB = esPrimo(b);
boolean esPrimoC = esPrimo(c);
boolean esPrimoD = esPrimo(d);
return (esPrimoA && !esPrimoB && esPrimoC && !esPrimoD) ||
(!esPrimoA && esPrimoB && !esPrimoC && esPrimoD);
}
public static boolean alterno(int a, int b, int c, int d, int e) {
boolean esPrimoA = esPrimo(a);
boolean esPrimoB = esPrimo(b);
boolean esPrimoC = esPrimo(c);
boolean esPrimoD = esPrimo(d);
boolean esPrimoE = esPrimo(e);
return (esPrimoA && !esPrimoB && esPrimoC && !esPrimoD &&
esPrimoE) || (!esPrimoA && esPrimoB && !esPrimoC && esPrimoD &&
!esPrimoE);
}
}
LENGUAJE PYTHON
def mostrarDivisoresAsc(n):
i=1
while i <= n:
if n % i == 0:
print(i)
i += 1
def mostrarDivisoresDesc(n):
i=n
while i >= 1:
if n % i == 0:
print(i)
i -= 1

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 mostrarDivisores(n, a, b):


i=a
while i <= b:
if n % i == 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 cantidadDivisores(n, a, b):


i=a
c=0
while i <= b:
if n % i == 0:
c += 1
i += 1
return c
def proximoDivisor(n, a):
i=a
c=0
while c == 0:
if n % i == 0:
c=i
i += 1
return c

def anteriorDivisor(n, a):


i=a-1
c=0
while c == 0:
if n % i == 0:
c=i
i -= 1
return c

def divisoresComunes(a, b):


i=1
while i <= a and i <= b:
if a % i == 0 and b % i == 0:
print(i)
i += 1

def anteriorDivisorComun(a, b, n):


i=n-1
c=0
while c == 0:
if a % i == 0 and b % i == 0:
c=i
i -= 1
return c

def mcdEuclides(a, b):


i=1
maximo = 0
while i <= a and i <= b:
if a % i == 0 and b % i == 0:
maximo = i
i += 1
return maximo

def mcd(a, b):


i=1
maximo = 0
while i <= a and i <= b:
if a % i == 0 and b % i == 0:
maximo = i
i += 1
return maximo

def mcd3(a, b, c):


i=1
maximo = 0
fe = 1
while i < a and i < b and i < c:
if (a % i == 0 and b % i == 0) or (b % i == 0 and c % i == 0) or (a % i ==
0 and c % i == 0):
maximo = i * fe
fe = maximo
i += 1
return fe

def mcd4(a, b, c, d):


i=1
maximo = 0
fe = 1
while i < a and i < b and i < c and i < d:
if (a % i == 0 and b % i == 0) or (b % i == 0 and c % i == 0) or (a % i ==
0 and c % i == 0) or (a % i == 0 and d % i == 0) or (d % i == 0 and b % i ==
0) or (d % i == 0 and c % i == 0):
maximo = i * fe
fe = maximo
i += 1
return fe

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 cantidadPrimos(a, b):


i=a
c=0
while i <= b:
if primo(i):
c += 1
i += 1
return c
def es_primo(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

def mostrarPrimos(n, a):


count = 0
num = a
while count < n:
if es_primo(num):
print(num)
count += 1
num += 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

III. IMPLEMENTAR LAS SIGUIENTES FUNCIONES LÓGICAS:

def es_primo(num):
if num < 2:
return False
for i in range(2, num):
if num % i == 0:
return False
return True

def dosPrimos(a, b, c):


primos = 0
if es_primo(a):
primos += 1
if es_primo(b):
primos += 1
if es_primo(c):
primos += 1
return primos == 2

def dosPrimos(a, b, c, d):


primos = 0
if es_primo(a):
primos += 1
if es_primo(b):
primos += 1
if es_primo(c):
primos += 1
if es_primo(d):
primos += 1
return primos == 2

def tresPrimos(a, b, c):


primos = 0
if es_primo(a):
primos += 1
if es_primo(b):
primos += 1
if es_primo(c):
primos += 1
return primos == 3

def tresPrimos(a, b, c, d):


primos = 0
if es_primo(a):
primos += 1
if es_primo(b):
primos += 1
if es_primo(c):
primos += 1
if es_primo(d):
primos += 1
return primos == 3

def sonDivisores(a, b):


if a == 0 or b == 0:
return False
if a % b == 0 or b % a == 0:
return True
return False

def sonDivisores(a, b, c):


if a == 0 or b == 0 or c == 0:
return False
if a % b == 0 and a % c == 0:
return True
if b % a == 0 and b % c == 0:
return True
if c % a == 0 and c % b == 0:
return True
return False

def sonDivisores(a, b, c, d):


if a == 0 or b == 0 or c == 0 or d == 0:
return False
if a % b == 0 and a % c == 0 and a % d == 0:
return True
if b % a == 0 and b % c == 0 and b % d == 0:
return True
if c % a == 0 and c % b == 0 and c % d == 0:
return True
if d % a == 0 and d % b == 0 and d % c == 0:
return True
return False

def sonDivisores(a, b, c, d, e):


if a == 0 or b == 0 or c == 0 or d == 0 or e == 0:
return False
if a % b == 0 and a % c == 0 and a % d == 0 and a % e == 0:
return True
if b % a == 0 and b % c == 0 and b % d == 0 and b % e == 0:
return True
if c % a == 0 and c % b == 0 and c % d == 0 and c % e == 0:
return True
if d % a == 0 and d % b == 0 and d % c == 0 and d % e == 0:
return True
if e % a == 0 and e % b == 0 and e % c == 0 and e % d == 0:
return True
return False

def sonMultiplos(a, b):


if a == 0 or b == 0:
return False
if a % b == 0 or b % a == 0:
return True
return False

def sonMultiplos(a, b, c):


if a == 0 or b == 0 or c == 0:
return False
if a % b == 0 and a % c == 0:
return True
if b % a == 0 and b % c == 0:
return True
if c % a == 0 and c % b == 0:
return True
return False

def sonMultiplos(a, b, c, d):


if a == 0 or b == 0 or c == 0 or d == 0:
return False
if a % b == 0 and a % c == 0 and a % d == 0:
return True
if b % a == 0 and b % c == 0 and b % d == 0:
return True
if c % a == 0 and c % b == 0 and c % d == 0:
return True
if d % a == 0 and d % b == 0 and d % c == 0:
return True
return False

def sonMultiplos(a, b, c, d, e):


if a == 0 or b == 0 or c == 0 or d == 0 or e == 0:
return False
if a % b == 0 and a % c == 0 and a % d == 0 and a % e == 0:
return True
if b % a == 0 and b % c == 0 and b % d == 0 and b % e == 0:
return True
if c % a == 0 and c % b == 0 and c % d == 0 and c % e == 0:
return True
if d % a == 0 and d % b == 0 and d % c == 0 and d % e == 0:
return True
if e % a == 0 and e % b == 0 and e % c == 0 and e % d == 0:
return True
return False

def alterno(a, b):


if es_primo(a) == es_primo(b):
return False
return True

def alterno(a, b, c):


if es_primo(a) == es_primo(b) or es_primo(b) == es_primo(c) or
es_primo(a) == es_primo(c):
return False
return True

def alterno(a, b, c, d):


if es_primo(a) == es_primo(b) or es_primo(b) == es_primo(c) or
es_primo(c) == es_primo(d) or es_primo(a) == es_primo(c) or es_primo(b)
== es_primo(d):
return False
return True

def alterno(a, b, c, d, e):


if (es_primo(a) == es_primo(b)) or (es_primo(b) == es_primo(c)) or
(es_primo(c) == es_primo(d)) or (es_primo(d) == es_primo(e)) or
(es_primo(a) == es_primo(c)) or (es_primo(b) == es_primo(d)) or
(es_primo(c) == es_primo(e)):
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))

You might also like