ok, esperare, y cuando lo resulevas pones un reto tu, juguemos a eso, retos de algoritmia y el que resuelve pone reto.
huracancorp dijo:ok, esperare, y cuando lo resulevas pones un reto tu, juguemos a eso, retos de algoritmia y el que resuelve pone reto.
#include <iostream>
#include <cmath>
void primos(double num, double *entero){
if (num < 1)
return;
double div = pow(2, num );
div = div-2;
div = div/num;
if ( modf(div,entero) == 0)
std::cout << num << "\n";
primos(num-1, entero);
}
int main(){
double *entero;
primos(53, entero);
}
public class NumerosPrimos{
private StringBuffer listadoPrimos = new StringBuffer();
public void listarPrimos(int num){
if(num<1){
return;
}
double factNum = factorial(num-1);
int residuo = (int) ( (factNum + 1) % num );
if(residuo == 0){
listadoPrimos.append(" "+num+"\n");
}
listarPrimos(num-1);
}
public double factorial(int n){
if(n==0){
return 1;
}
return n * factorial(n-1);
}
public String getListadoPrimos(){
return listadoPrimos.toString();
}
public static void main(String[] args){
NumerosPrimos np = new NumerosPrimos();
// Maximo antes de fallo en el calculo
int lim = 19;
np.listarPrimos(lim);
System.out.print ("\n Los Numeros Primos son: "+"\n\n");
System.out.print (np.getListadoPrimos()+"\n ");
}
}
//Creado por Elgo 14/11/2006
public class Sieveton {
private static java.util.Set<Integer> pri = new java.util.TreeSet<Integer>();
public static void main(String[] args) {
sieveton(2,1000);
System.out.println(pri);
}
private static void sieveton(int val, int hasta) {
if (val<=hasta) {
boolean es = true;
for (int p:pri)
if (p!=1 && p!=val && val%p==0)
es = false;
pri.add(es?val:1);
sieveton(++val,hasta);
}
}
}
Elgo dijo:Ineficiente pero pequeño y confuso
PHP://Creado por Elgo 14/11/2006 public class Sieveton { private static java.util.Set<Integer> pri = new java.util.TreeSet<Integer>(); public static void main(String[] args) { sieveton(2,1000); System.out.println(pri); } private static void sieveton(int val, int hasta) { if (val<=hasta) { boolean es = true; for (int p:pri) if (p!=1 && p!=val && val%p==0) es = false; pri.add(es?val:1); sieveton(++val,hasta); } } }
huracancorp dijo:jomunoz has ganado! debes colocar el proximo reto.
import java.io.*;
public class Matrices{
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
private int[][] matrizA;
public Matrices(int[][] matrizA){
this.matrizA = matrizA;
}
public int[][] multiplicar (int [][] matrizB) {
int[][] matrizAporB = new int [matrizA.length][matrizA.length];
for (int i = 0; i < matrizA.length; i++){
for (int j = 0; j < matrizB [0].length; j++){
for(int k = 0; k < matrizA [0].length; k++){
matrizAporB [i][j] += matrizA [i][k] * matrizB [k][j];
}
}
}
return matrizAporB;
}
public static int[][] leerMatrizCuadrada(int n) throws IOException{
int[][] matrizAux = new int[n][n];
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
System.out.print (" ["+i+"]["+j+"] = ");
matrizAux[i][j] = Integer.valueOf(br.readLine());
}
}
return matrizAux;
}
public static void imprimirMatriz(int[][] m){
for (int i = 0; i<m.length; i++) {
System.out.print ("\n\n");
for (int j = 0; j<m[0].length; j++) {
System.out.print ("\t"+m[i][j]);
}
}
}
public static void main(String[] args) {
try {
System.out.print ("\n Digite N: ");
int n = Integer.valueOf(br.readLine());
System.out.print ("\n Datos Matriz A\n");
int[][] matrizA = Matrices.leerMatrizCuadrada(n);
Matrices m = new Matrices(matrizA);
System.out.print ("\n Datos Matriz B\n");
int[][] matrizB = Matrices.leerMatrizCuadrada(n);
int[][] matrizAporB = m.multiplicar(matrizB);
System.out.print ("\n Matriz A");
Matrices.imprimirMatriz(matrizA);
System.out.print ("\n\n\n Matriz B");
Matrices.imprimirMatriz(matrizB);
System.out.print ("\n\n\n Matriz A * B");
Matrices.imprimirMatriz(matrizAporB);
System.out.print ("\n\n ");
}
catch (Exception ex) {
System.out.print ("\n NO aprueba de tontos \n\n ");
}
}
}
//by Oesoto - LANeros.com
/*
*El reto es el siguiente:
Un programa que permita rotar los elementos de un arreglo el número de posiciones dadas (arreglo circular) y visualice el nuevo arreglo.
Ej:
arreglo = {1,2,3,4,5}
Posiciones = -2
quedaria: {3,4,5,1,2}
Posiciones = 3
quedaria: {3,4,5,1,2}
Posiciones = 15
quedaria: {1,2,3,4,5}
*/
public class ring {
/*
* Se declara un lector de teclado, un arreglo original, uno de respuesta y unos enteros
* que siempre permaneceran constantes.
*/
lector reader;
int []array;
int []arrayaux;
int lenght;
int rotacion;
//El constructor de la clase de obtener algunos datos y llenar el arreglo
public ring(){
reader = new lector();
System.out.print("Escriba la cantidad de desplazamientos: ");
rotacion = reader.leer();
System.out.print("Escriba el tamano del arreglo: ");
lenght = reader.leer();
array = new int[lenght];
arrayaux = new int[lenght];
int current = 0;
for(int i=0;i<lenght;i++){
System.out.print("Ingrese el elemento " + i + " del arreglo: ");
current = reader.leer();
array[i] = current;
}
}
/*
* Calculo de las posiciones finales luego de la rotación y copia de los elementos
* a esas posiciones.
*/
public void rotar(int rot){
int c;
int posfinal;
if (rotacion>=0){
for(c=0;c<lenght;c++){
posfinal = c;
posfinal = posfinal + rot;
while (posfinal>=lenght){
posfinal = posfinal - lenght;
}
arrayaux[posfinal] = array[c];
}
}
else{
for(c=0;c<lenght;c++){
posfinal = c;
posfinal = posfinal + rot;
while (posfinal<0){
posfinal = posfinal + lenght;
}
arrayaux[posfinal] = array[c];
}
}
}
/*
* Rotación y posterior impresión en pantalla del arreglo
*/
public void procesar(){
rotar(rotacion);
print(lenght);
}
//Recorre el arreglo e imprime elemento por elemento
public void print(int size) {
int j = 0;
System.out.println("\n" + "Contenido del arreglo:" + "\n");
while (j<size) {
System.out.println("Posicion " + (j) + ": " + arrayaux[j]);
j++;
}
}
public static void main(String args[]){
ring r = new ring();
r.procesar();
}
}
//By Oesoto - LANeros.com
import java.io.*;
class lector{
public int leer (){
int valor=0;
String dato="";
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader teclado = new BufferedReader(isr);
try{
dato=teclado.readLine();
valor= new Integer (dato).intValue();
}catch (IOException ioe){}
catch(NumberFormatException e){System.out.print("Dato no válido");
}
return valor;
}
public String leerString(){
String dato="";
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader teclado = new BufferedReader(isr);
try{
dato=teclado.readLine();
}catch (IOException ioe){}
return dato;
}
}
import java.io.*;
public class MoverArreglo{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static int[] leerArreglo(int tamanio) throws IOException{
int[] arrAux = new int[tamanio];
for (int i = 0; i<tamanio; i++) {
System.out.print (" ["+i+"] = ");
arrAux[i] = Integer.valueOf(br.readLine());
}
return arrAux;
}
public static int[] clonarArreglo(int[] arregloOriginal){
int[] arrClon = new int[arregloOriginal.length];
for (int i = 0; i<arregloOriginal.length; i++) {
arrClon[i] = arregloOriginal[i];
}
return arrClon;
}
public static void imprimirArreglo(int[] arreglo){
for (int i = 0; i<arreglo.length; i++) {
System.out.print (" "+arreglo[i]);
}
}
/**
* Retorna un arreglo que representa el arreglo original, pero cuyos elementos
* han sido movidos las casillas indicadas por el parametro posiciones.
*
* @param posiciones indica el numero de casillas que seran movidos los elemetos.
* @param arrOriginal el arreglo en su estado original.
* @return el arreglo resultante de la rotacion de los elementos
*/
public static int[] rotarArreglo(int[] arrOriginal, int posiciones){
int tamanio = arrOriginal.length;
int[] arrAux = MoverArreglo.clonarArreglo(arrOriginal);
// Quitamos movimientos innecesarios, si los hay.
// Ej: siendo el tamaño del arreglo 5, y el numero
// de posiciones a rotar 20500, en realidad es
// lo mismo que mover cada elemento 0 veces
posiciones = posiciones % tamanio;
if(posiciones != 0){
if( posiciones < 0 ){
// Ej: Si el tamaño del arreglo es 5, y posiciones=-3,
// es equivalente a posiciones=2
posiciones = tamanio + posiciones;
}
for(int i=0;i<tamanio;i++){
int posDestino = (i+posiciones) % tamanio;
arrAux[posDestino] = arrOriginal[i];
}
}
return arrAux;
}
public static void main(String arg[])throws IOException{
int resp;
do{
System.out.print("\n Digite el tamanio del arreglo: ");
int tamanio = Integer.parseInt(br.readLine());
int[] arrOriginal = MoverArreglo.leerArreglo(tamanio);
System.out.print("\n Digite el numero de posiciones a rotar: ");
int posiciones = Integer.parseInt(br.readLine());
int[] arrMovido = MoverArreglo.rotarArreglo(arrOriginal, posiciones);
System.out.print ("\n Arreglo Original: ");
MoverArreglo.imprimirArreglo(arrOriginal);
System.out.print ("\n Arreglo Movido : ");
MoverArreglo.imprimirArreglo(arrMovido);
System.out.print("\n\n 1 = Nuevo\t 2 = Salir\n\n Resp = ");
resp = Integer.parseInt(br.readLine());
}
while(resp == 1);
}
}
Oesoto dijo:Que alguien mas ponga el reto, la verdad no se me ocurre que poner
PD: Bacana la solución usando modulo, yo estuve pensando un rato como calcular la posición final facilmente y no se me pasó por la cabeza eso.