I have this original C++ function called "s":
long s(long n) {
long sum = 0;
long m;
m = (long) sqrt(n);
for (long i = 2; i < m; i++)
if ((n % i) == 0) sum += (i + (n/i));
if (n>1) sum += 1;
if ((m*m) == n) sum += m;
return sum;
}
I've been struggling to convert this function over to using the GMP's mpz type, so that it will allow for arbitrarily long integers.
This is my attempt at it:
void s(mpz_t n, mpz_t *final)
{
mpz_t sum;
mpz_t m;
mpz_t temp;
mpz_init (sum);
mpz_init (m);
mpz_init (temp);
mpz_set_str (sum, "0", 10);
mpz_sqrt(m, n);
for (mpz_t i, mpz_init(i), mpz_set_str(i, "2", 10); mpz_cmp(i,m)< 0; mpz_add_ui(i, i, 1))
{
mpz_mod(temp, n, i);
if (mpz_cmp_si(temp, 0) == 0)
{
// use divexact since we know they are divisable
mpz_divexact(temp, n, i);
mpz_add(temp, temp, i);
mpz_add(sum, sum, temp);
}
}
if (mpz_cmp_si(n, 1) > 0) mpz_add_ui(sum, sum, 1);
mpz_mul(temp, m, m);
if (mpz_cmp(temp, n) == 0) mpz_add(sum, sum, m);
final = sum;
}
The whole original program can be found here: http://pastebin.com/mf751592
What am I doing wrong? I seemed to initially have trouble because I couldn't return type mpz_t. So instead, I passed in a pointer to what I want the function to return.
I'm still struggling with it though. Can someone point me in the right direction?
This line: for (mpz_t i, mpz_init(i), mpz_set_str(i, "2", 10); mpz_cmp(i,m)< 0; mpz_add_ui(i, i, 1))
Gives this error: 23: error: cannot initialize arrays using this syntax