Completato esercizio p6.9 con Sequence.
Implementata interfaccia Sequence nella classe PrimeGenerator
This commit is contained in:
parent
eac302e89b
commit
5e9dae5687
|
@ -5,37 +5,41 @@
|
|||
*/
|
||||
public class PrimeGenerator implements Sequence<Integer> {
|
||||
private int max;
|
||||
private int i = 1;
|
||||
private int attuale;
|
||||
|
||||
/**
|
||||
* Costruttore parametrico completo
|
||||
* @param max Numero massimo che deve essere stampato
|
||||
*/
|
||||
public PrimeGenerator(int max) {
|
||||
public PrimeGenerator(int max) {
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
attuale = 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Metodo che restituisce il numero successivo nella sequenza dei numeri primi
|
||||
* @return Numero successivo nella sequenza
|
||||
*/
|
||||
public Integer next() {
|
||||
int counter = 0;
|
||||
int j = 1;
|
||||
public Integer next() {
|
||||
while (true) {
|
||||
boolean primo = true;
|
||||
|
||||
while(j <= i && counter <= 2)
|
||||
{
|
||||
counter = 0;
|
||||
if(i % j == 0)
|
||||
{
|
||||
counter++;
|
||||
for (int i = 2; i < attuale; i++) {
|
||||
if (attuale % i == 0) {
|
||||
primo = false;
|
||||
}
|
||||
}
|
||||
j++;
|
||||
|
||||
if (primo) {
|
||||
int prossimo = attuale;
|
||||
attuale++;
|
||||
return prossimo;
|
||||
}
|
||||
attuale++;
|
||||
}
|
||||
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return true;
|
||||
return attuale < max;
|
||||
}
|
||||
}
|
|
@ -1,16 +1,19 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Classe di test per PrimeGenerator
|
||||
* @author radaelli11353
|
||||
*/
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.print("Inserisci un numero: ");
|
||||
int max = in.nextInt();
|
||||
PrimeGenerator primi = new PrimeGenerator(max);
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.print("Inserisci un numero: ");
|
||||
int max = in.nextInt();
|
||||
PrimeGenerator primi = new PrimeGenerator(max);
|
||||
|
||||
while(primi.hasNext()) {
|
||||
int numero = primi.next();
|
||||
if(numero != 0) System.out.println(numero);
|
||||
}
|
||||
|
||||
}
|
||||
while(primi.hasNext()) {
|
||||
System.out.println(primi.next());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue