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 ab. 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:

 a = 10
 b = 12
 dt = 'int16'
 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

48.0% Correct | 52.0% Incorrect
Last Solution submitted on Dec 23, 2021

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers5

Suggested Problems

More from this Author32

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!