pokolas wrote: |
---|
Long long doesn't help, still the same problem, i researched different data types but i think none of them can hold number of 10^14 |
Look a bit harder, @pokolas. Try running the code below on your machine.
Unsigned long long should do the trick. Remember that ALL your potentially-large integer values should have that type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
#include <limits>
using namespace std;
int main()
{
cout << "Maximum size of unsigned short: " << numeric_limits<unsigned short >::max() << endl;
cout << "Maximum size of short: " << numeric_limits< short >::max() << endl;
cout << "Minimum size of short: " << numeric_limits< short >::min() << endl;
cout << endl;
cout << "Maximum size of unsigned int: " << numeric_limits<unsigned int >::max() << endl;
cout << "Maximum size of int: " << numeric_limits< int >::max() << endl;
cout << "Minimum size of int: " << numeric_limits< int >::min() << endl;
cout << endl;
cout << "Maximum size of unsigned long: " << numeric_limits<unsigned long >::max() << endl;
cout << "Maximum size of long: " << numeric_limits< long >::max() << endl;
cout << "Minimum size of long: " << numeric_limits< long >::min() << endl;
cout << endl;
cout << "Maximum size of unsigned long long:" << numeric_limits<unsigned long long>::max() << endl;
cout << "Maximum size of long long: " << numeric_limits< long long>::max() << endl;
cout << "Minimum size of long long: " << numeric_limits< long long>::min() << endl;
cout << endl;
cout << "Maximum size of size_t: " << numeric_limits< size_t >::max() << endl;
cout << endl;
cout << "Maximum size of float: " << numeric_limits< float >::max() << endl;
cout << "Minimum size of float: " << numeric_limits< float >::min() << endl;
cout << endl;
cout << "Maximum size of double: " << numeric_limits< double >::max() << endl;
cout << "Minimum size of double: " << numeric_limits< double >::min() << endl;
}
| |
When you have done that, note the following.
Your test for a factor should be
if ( n % i == 0 )
You only have to test up to sqrt( n ), since i is a factor of n if and only if n/i is also a factor. This will reduce your large problem by a factor of ... 10 million. Worth it?
Then
sum += i + n / i;
except for the two following special cases:
> i=1 is a proper factor of n>1, but its pair is n itself, which is a factor, but not a proper factor, so shouldn't be added to the sum.
> If n is a perfect square then i and n/i will be the same if i=sqrt(n) and shouldn't be double-counted.
This is what I get for 10
14:
Sum of proper factors is 149992370597277
100000000000000 is abundant
|