Problem 44513. Add all the numbers between two limits (inclusive)
In this problem you must add up "all of the numbers" between two specified limits, a and b, in which a ≤ b. However, the practical interpretation of "all of the numbers" will depend upon the specified data type, dt.
Mathematically speaking, if a < b then the required sum constitutes an infinite series that does not converge (i.e. the required sum would be infinity). For example, if a=1 and b=2 then we could capture some of those numbers through the series lim n→∞ ⁿ∑ᵢ₌₁{1 + (1/i)} = lim n→∞ {n + ⁿ∑ᵢ₌₁(1/i)} ≈ lim n→∞ {n + γ + ln(n)}, using properties of the harmonic series in the last approximation.
But MATLAB cannot represent numbers with infinite precision. In fact, the precision is determined by the specified data type. For instance, if dt = 'single', then with a=1 and b=2 the summation would comprise the series {(1) + (1+1×2⁻²³) + (1+2×2⁻²³) + (1+3×2⁻²³) + ... + (2−2×2⁻²³) + (2−1×2⁻²³) + (2)} = 12582913.5, which is finite.
Another example:
% INPUT a = 10 b = 12 dt = 'int16' % OUTPUT s = 33 % = 10 + 11 +12
So please add up all the numbers between two limits (inclusive), subject to the precision indicated by the specified data type.
NOTE 1: Terminal values a and b are whole numbers in every case (albeit implicitly defined as of the double data type); they can be positive or negative. However, values 1<x<+1 are never included in the summations.
NOTE 2: All data types specified in the input dt shall be numeric.
Solution Stats
Problem Comments

3 Comments
For single (and double) the test suite result for the sum from 2 to 3 is smaller than that for 1 to 2can that be right?
I think I commented too soon againnever mind.
No worries, Tim. I found it to be an interesting result too :) . . . —DIV
Solution Comments
Show commentsProblem Recent Solvers5
Suggested Problems

4033 Solvers

1231 Solvers

640 Solvers

165 Solvers

Find the largest value in the 3D matrix
1448 Solvers
More from this Author32
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!