Código:
Lo he puesto en el debug y mirando paso a paso lo que hace, y lo que no entiendo es: ¿por qué si llega a la instrucción: resul = false; //Terminación anticipada, realiza también la fase de vuelta de la recursividad? import java.io.*;
public class EsPrimo {
static boolean EsPrimoRecursivo(int n, int divisor, boolean resul){
divisor = divisor-1;
if(divisor>1)
if(n%(divisor) != 0) EsPrimoRecursivo(n, divisor, resul);
else resul= false; //Terminación anticipada.
return resul;
}
static boolean EsPrimo(int num){
int aux;
aux = num;
boolean resultado = true;
return EsPrimoRecursivo(num, aux, resultado);
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader linea = new BufferedReader(new InputStreamReader(System.in));
int numero;
System.out.print("Numero: ");
numero = Integer.parseInt(linea.readLine());
System.out.print("Primo: " + EsPrimo(numero));
}
}
¿No se supone que, si no hago ahí ninguna llamada recursiva, se tiene que romper la recursividad y salir?


Este tema le ha gustado a 1 personas