Last active
August 29, 2015 13:56
-
-
Save vheon/8885800 to your computer and use it in GitHub Desktop.
Stampa i primi n numeri primi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fiji.io.*; | |
/* app che calcola i primi n numeri primi */ | |
// pre: n > 0 | |
class NumPrimi { | |
public static void main(String[] args) { | |
int n; | |
System.out.println("Digita il numero dei primi numeri primi che vuoi visualizzare"); | |
n = Lettore.in.leggiInt(); | |
System.out.println("I primi " + n + " numeri primi sono:"); | |
int num = 1, nPrimi = 0; | |
boolean isPrimo; | |
while(nPrimi < n) { | |
isPrimo = primo(num); | |
if (isPrimo) { | |
System.out.print(num + " "); | |
nPrimi++; | |
} | |
num++; | |
} | |
} | |
public static boolean primo(int num) { | |
int lim = num/2; | |
boolean isPrimo = true; | |
for(int div = 2; div <= lim && isPrimo; div++) | |
if (num % div == 0) | |
isPrimo = false; | |
return isPrimo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment