-1

After compiling the entire code, this is the error i got:

[Error] invalid operands of types '__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}' and 'int' to binary 'operator%'

I have tried type casting for n/2 to int in the last line of the code but still showed the same error:

int rem(int n,int b,int d)
{
    if(n==1)
    {
        return b%d;
    }
    else
    {
        return (pow(rem(n/2,b,d),2)%d);
    }

}
Obsidian
  • 3,195
  • 8
  • 16
  • 28
user173379
  • 21
  • 5

1 Answers1

1

The issue is with pow. It returns a double, and using % with non-intergal type arguments is not allowed in C++ (it is in Java by the way).

But, you should not be using pow anyway to square a number.

Writing

auto r = rem(n/2,b,d);
return r * r % d;

is far better. Some folk might prefer (r * r) % d if they like to use superfluous parentheses.

Bathsheba
  • 227,678
  • 33
  • 352
  • 470