0

What data type or what ways can I store large integers possibly greater than 10^18 and How can I efficiently improve my approach to the problem?

I am currently working on a problem that asks to find the sum of all divisors d(k) given that:

       N    N
S(N) = ∑    ∑ d(j*i)
      i=1  j=1

with the largest value of N = 10^9 and largest divisor (10^9 * 10^9). Stored in:

long long int

The program solves and slows down at N = 10^3 and anything higher takes up to much memory and crashes.

I used a for loop for the values of i and j that calculates the values of d(k) > d(i * j) and store it in a vector:

{d(1 * 1), d(1 * 2), ... , d(i>N * j>N)}

Then a separate function that finds all divisors of d(k) then adds them up:

d(1) = 1
d(2) = 1 + 2 = 3
d(3) = 1 + 3 = 4
d(4) = 1 + 2 + 4 = 7
...
d(i>N * j>N)

S(N) = d(1) + d(2) + d(3) + d(4) + ... + d(i>N * j>N)

Any values of N greater than 10^5 gets displayed as S(N) mod 10^9.

mark
  • 1
  • 3
    [See this example of using boost::multiprecision to compute factorials](https://stackoverflow.com/questions/67630357/c-factorial-of-number-100/67630658#67630658). But does the solution require such large values, or is there a "math trick" that can be applied? – PaulMcKenzie Feb 12 '22 at 15:18
  • 1
    ***and anything higher takes up to much memory and crashes.*** This part is independent of the size of the integer. If you are storing `s` in memory there will be a point where your system runs out of virtual memory. – drescherjm Feb 12 '22 at 15:20
  • If you only need unsigned integers, a `std::uint64_t` would fit values up to 10^19 (and then some) – Ted Lyngmo Feb 12 '22 at 15:48
  • 1
    do the maths first. Divisors of `a*b` are divisors of `a` and divisors of `b`. I guess there is more you can do to avoid a brute force algorithm – 463035818_is_not_an_ai Feb 12 '22 at 15:54
  • If it wraps around 10^9 then you are using integers for your computation. Besides doing this smarter you need to either use `unsigned long long` for `i` or `j` or promote one of them to that before the multiplying. – Goswin von Brederlow Feb 12 '22 at 16:08
  • `S(N) = d(1) + d(2) + d(3) + d(4) + ... + d(i>N * j>N)` is not the same as `sum(i=1...N) sum(j=1...N) d(i*j)` E.g. `d(4)` appears only once in the former, but should appear three times in the latter (for `(i, j)` of `(1, 4)`, `(2, 2)` and `(4, 1)`) Could you quote the exact problem statement? You might be mis-interpreting it. – Igor Tandetnik Feb 12 '22 at 17:57
  • Not that it is faster to compute the prime factorization of a number rather than finding directly all its dividers since the number of dividers of a number `n` of size `s` takes `O(C^s)` time (where `C` can be assumed to be a constant and `^` is exponentiation). Still, there is no known polynomial algorithms to find a prime factor but this is not a problem as long as `n` is small (1e9 is relatively find with an optimized algorithm). – Jérôme Richard Feb 12 '22 at 18:30
  • Hi @mark, welcome to StackOverflow. I recommend taking the [tour](https://stackoverflow.com/tour), as well as reading how to [ask a good question](https://stackoverflow.com/help/how-to-ask) and what's [on topic](https://stackoverflow.com/help/on-topic). It seems you have code running, so to clear up any doubts, please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), and the actual output with errors. – Arc Feb 14 '22 at 13:26
  • That being said, I highly recommend you use [GNU GMP](https://gmplib.org/), its the fastest big integer library to the best of my knowledge, and it has a C++ [interface](https://gmplib.org/manual/C_002b_002b-Class-Interface). – Arc Feb 14 '22 at 13:30

0 Answers0