prime number with recursion

2

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.

    
asked by Egon Stetmann. 09.09.2017 в 03:01
source

2 answers

3

Solution 1: use static variables .

We do not use anything outside the function, and it only receives 1 parameter:

#include <iostream>

bool esprimo( int x ) {
  static int tmp = 0;

  if( x == 1 ) {
    tmp = 0;
    return true;
  }

  if( !tmp ) {
    tmp = x;
    --x;
  }

  if( !( tmp % x ) ) {
    tmp = 0;
    return false;
  }

  return esprimo( --x );
}

int main( void ) {
  using namespace std;

  cout << "esprimo( 3 ) = " << esprimo( 3 ) << endl;
  cout << "esprimo( 7 ) = " << esprimo( 7 ) << endl;
  cout << "esprimo( 10 ) = " << esprimo( 10 ) << endl;

  return 0;
}
  

esprimo (3) = 1
  esprimo (7) = 1
  esprimo (10) = 0

The variables static have a lifetime equal to the lifetime of your application. It's the same as declaring out of main( ) , and keeping forever the last value you give them. By declaring it inside of a function, we do not ensure that it is not view from other points in the code, so we gain security.

Solution 2: arguments with default value.

If you notice, your problem indicates

  

Do not call more than one parameter in main

No indicates that the function only receives 1, but it is called from main( ) with 1.

#include <iostream>

bool esprimo( int x, int tmp = 0 ) {
  if( x == 1 )
    return true;

  if( !tmp ) {
    tmp = x;
    --x;
  }

  if( !( tmp % x ) )
    return false;

  return esprimo( --x, tmp );
}

int main( void ) {
  using namespace std;

  cout << "esprimo( 3 ) = " << esprimo( 3 ) << endl;
  cout << "esprimo( 7 ) = " << esprimo( 7 ) << endl;
  cout << "esprimo( 10 ) = " << esprimo( 10 ) << endl;

  return 0;
}
  

esprimo (3) = 1
  esprimo (7) = 1
  esprimo (10) = 0

C ++ allows arguments with default values . If when calling the function you do not assign them a value directly, the compiler puts them in your place . This means that the function still receives the number of arguments indicated , even if we do not explicitly indicate it. Its mission is to facilitate the use of functions, saving us writing arguments that, most of the time, will always be the same ... or, as in this case, providing undocumented functionality. For anyone who does not see the code, the esprimo( ) function only receives 1 argument.

    
answered by 09.09.2017 / 07:20
source
0

The div argument is not necessary. Also, you have to start checking for 2 as a divisor, because every number is divisible by 1 and then it would always give false. Also, passing cont++ is incorrect because it updates the value of cont in the recursive call. The correct thing to do is to pass cont+1 or first to do cont++ and then pass cont in the recursive call.

bool esPrimo(int number, int cont = 2)
{
    if (number % cont == 0 || number < 2) 
    {
        return false;
    } 
    else if (cont > number/2)
    {
        return true;
    }
    return esPrimo(number, cont+1);
}
    
answered by 09.09.2017 в 03:32