I do not know why this code explodes at runtime, any ideas?
#include <iostream>
using namespace std;
bool esPrimo(int number, int cont = 1, int div = 0)
{
if (number % cont == 0)
{
div+=1;
}
else if (cont == number) {
return (div < 3) ? 1: 0;
}
return esPrimo(number, cont++, div);
}
int main()
{
int primo = esPrimo(3);
cout << primo << endl;
return 0;
}
I do not know if I understood the part of the parameters esPrimo(int number, int cont = 1, int div = 0)
, the problem I have is with the syntax and the fact that with the recursion I can not store the data once it has been executed the function, so for that I assign again these variables in the parameters, as far as I understood, to initialize them in the same parameter makes it possible for them to no longer call them, because they would be replaced. But I'm not sure.
What I want to achieve is to use an esPrimo function () that does not call more than one parameter in main and that does not use external variables, pointers, references or lists . If you can help me, I would be grateful.