Skip to content

Instantly share code, notes, and snippets.

@brunoperezm
Created July 29, 2012 23:44
Show Gist options
  • Save brunoperezm/3202618 to your computer and use it in GitHub Desktop.
Save brunoperezm/3202618 to your computer and use it in GitHub Desktop.
Numeros primos
import java.util.Scanner;
public class bruno {
public static void main (String args[]) {
System.out.println("Dime hasta que numero quieres saber los numeros primos:");
Scanner number = new Scanner(System.in);
Scanner print_prime = new Scanner(System.in);
int top_number = number.nextInt();
int total_prime_numbers = 0;
// Empezamos el bucle
for (int x = 1; x<=top_number; x++) {
boolean is_prime = false;
// Si el numero es 1 o 2 le ponemos que es primo manualmente
if(x==1||x==2) {is_prime=true;}
if (x%10000==0) { System.out.printf("Voy por los %d/%d \n", x, top_number); }
for (int subx = 2; subx<x;) {
boolean keep_going = ( (x%subx == 0) ? false : true );
if (keep_going) {
subx++;
if (subx == x) {
is_prime = true;
}
}
else {
subx = x;
}
}
if (is_prime) {
total_prime_numbers++;
}
}
System.out.printf("Hay un total de %d numeros primos del 1 al %d \n", total_prime_numbers, top_number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment