Cristiano Magro
2025-10-04 851e6f15b30ddc7636cfe78d85a9b399f4e0d7b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class NumeroPrimo {
    public boolean isPrimo(int numero) {
        if (numero <= 1) {
            return false;
        }
        if (numero % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(numero); i += 2) {
            if (numero % i == 0) {
                return false;
            }
        }
        return true;
    }
 
    public static void main(String[] args) {
        NumeroPrimo np = new NumeroPrimo();
 
        int limite = Integer.parseInt(args[0]);
 
        long startTime = System.nanoTime();
        int conta = 1;
        for (int i = 3; i <= limite; i += 2) {
            if (np.isPrimo(i)) {
                System.out.println(conta + " " + i);
                conta++;
            }
        }
 
        long endTime = System.nanoTime(); // ⏱️ Fine
 
        long duration = endTime - startTime; // in nanosecondi
        System.out.println("Tempo di esecuzione: " + (duration / 1_000_000) + " ms");
    }
}