My program needs to use exponents, however, double does not allot enough space for my numbers, is there another way to use the pow(x,y) function or is there another function for exponents that I'm overlooking?

``````#include <iostream>
#include <cmath>
using namespace std;

int main(void)
{

int thresh = 10000;
long long sum = 0;

for(long double n = 2; n <= thresh; n++)
{
long double t1 = ((3*(pow(2,n)))-1);
long double t2 = ((3*(pow(2,(n-1))))-1);
long double t3 = ((9*(pow(2,((2*n)-1))))-1);

long double n1 = (pow(2,n)*t1*t2);
long double n2 = (pow(2,n)*t3);

sum += n1;
sum += n2;
}

cout << sum << endl;

cin.get();
return 0;
}``````
3
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by xtremerocker

Implicit cast to long long int seems to work....(at least on Fedora 8 with g++)

edit: forgot about precision... discard. (you may need to use GMP, if in linux)

I'm using Microsoft Visual C++ 2008 and Windows XP SP3, if that helps with anything at all.

I'm curious as the purpose of this. You're ending up with values on the order of 2^30,000 - that's so incredibly huge I can't even fathom it. I get dizzy on long long int - 2^64.

It's for ProjectEuler.net problem 21.

Otherwise, I couldn't come up with a good reason to have numbers so large.

This problem?

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

Yes, that one.

There's probably a much better way to solve it, isn't there?

Yes, that one.
There's probably a much better way to solve it, isn't there?

Yes, I suppose there is. I don't see where the code you've posted leads to the solution of that problem.

In a simplistic sense, you need to examine every number up to 10000, find the sum of their divisors, the find the numbers where the d(a) = b and d(b) = a relationships exist. Add those a and b values.

There's a pretty obvious brute force approach, I'm not sure if there's a more elegant approach as well.

I guess I didn't fully understand the question and a larger number is not necessary, sorry wasting your time and thanks for the help anyways.

- michael