-1

I'm trying to solve 56th problem of Project Euler and I have some troubles while using the Big Integer Library. Here is the code :

#include <iostream>
#include "BigInteger.hh"
#include <cmath>
using namespace std;

int digitSum(BigInteger x){
int num[(int)floor(log10(x))+2];
int n = 0;
BigInteger sum = 0;

while (x != 0){
    sum += x - floor(x/10) * 10;
    x = floor(x/10);
    n++;
}

return sum.toInt();
}


int main(int argc, const char * argv[])
{

int max = 0;

for (int i = 1; i <= 100; i++){
    for (int j = 1; j <= 100; j++){
        cout << "i = %i, j = %i ",i,j;
        if (digitSum(pow(i,j)) < max)
            max = digitSum(pow(i,j));

    }

    }

cout << digitSum(pow(2,63));


return 0;
}

The problem is that when I try building, the compiler gives error on the lines using the log10, floor, and pow functions saying that they are used but not defined. When I comment the line #include "BigInteger.hh", everything goes fine but this time, of course, I can't use the Big Integer library.

Why is there such a problem? How to solve it?

moray95
  • 937
  • 2
  • 9
  • 12
  • you must link a library. – Michał Szczygieł Feb 05 '13 at 20:19
  • This will help http://stackoverflow.com/questions/12802185/pow-isnt-defined/12802198#12802198 – hmjd Feb 05 '13 at 20:22
  • The reason is simple, that Big Integer library doesn't define the functions `log10`, `floor`, or `pow` for its BigUnsigned and BigInteger classes. Either write them yourself or use a different library. – Blastfurnace Feb 05 '13 at 20:36
  • Well, okay I understand this but still, there is not a reason that these function are undefined. I should be able to use them with C++ integer. – moray95 Feb 05 '13 at 20:39
  • You are calling `log10` and `floor` with a BigInteger parameter in your digitSum function. That obviously won't work... – Blastfurnace Feb 05 '13 at 20:43

1 Answers1

0

Is it a compiler or a linker problem? If latter, try adding -lm to the command line to link the math library. Assuming GNU gcc here.

Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88