Sum Pairwise Remainders Across Two Integer Ranges
You are given two non-negative integers upperX and upperY. Return the sum of x mod y over every integer x in [0, upperX] and every valid divisor y in [1, upperY].
Implement sumRangeRemainders(upperX, upperY).
Input and Output
-
Return
∑x=0upperX∑y=1upperY(xmody)
.
-
The original range description includes zero at both lower bounds. Because modulo by zero is undefined, this practice version excludes
y = 0
while retaining
x = 0
.
Constraints
-
0 <= upperX <= 1,000,000
-
1 <= upperY <= 1,000,000
-
The result fits in a signed 64-bit integer.
-
Iterating over all
(upperX + 1) * upperY
pairs will not finish within the intended limits.
Example 1
Input: upperX = 2, upperY = 2
Output: 1
For divisors 1 and 2, the remainder totals are 0 and 1.
Example 2
Input: upperX = 3, upperY = 3
Output: 5
The totals for divisors 1, 2, and 3 are 0, 2, and 3.