Start learning free

LESSON 136 OF 348 · LOOPS & GLOBAL VARIABLES

Sum of Numbers in Kotlin: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes a number n as argument and returns the sum of all numbers 0 through n (0 + 1 + 2 + ... + n - 1).

So when we called computeSum(4), we expected our code to print:

6

because 0 + 1 + 2 + 3 = 6. But it seems like we made some mistakes because when we run our code, it prints:

16

Assignment:

Your task is to fix our function such that it correctly computes and returns the desired sum.

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · LOOPS & GLOBAL VARIABLES

    Buggy Code Numbers Sum in Kotlin, explained

    What the code is supposed to do

    The function computeSum takes an Int named n and returns the sum of the whole numbers from 0 up to n - 1. For n = 4 that is 0 + 1 + 2 + 3, which is 6. The helper mainFunction prints the result of computeSum(4), so the program should print 6.

    The code you start with prints 16:

    fun computeSum(n: Int): Int {
        var sum = 0
        for (i in 0 until n) {
            sum += n
        }
        return sum
    }
    
    // Do not change this code:
    fun mainFunction() {
        println(computeSum(4))
    }
    

    What goes wrong

    The result 16 is a clue: it equals 4 + 4 + 4 + 4. The loop does run four times, once for each value of i from 0 to 3, so the repetition is right. What is added on each pass is wrong. The line sum += n adds the parameter n, which is 4 during the whole call, instead of the loop variable i, which changes from pass to pass.

    Tracing the buggy loop for computeSum(4):

    isum beforeaddssum after
    0044
    1448
    28412
    312416

    In general the loop runs n times and adds n each time, so it returns n * n. The task wants the sum of 0 through n - 1, which is a different quantity. For n = 1 the buggy code returns 1, while the correct sum, which is just 0, is different.

    Fixing it step by step

    1. Find what should vary. The sum is meant to collect the different numbers 0, 1, 2 and 3. The only thing in the loop that takes those values is i.
    2. Replace n with i in the addition: sum += i. The loop header, the starting value of sum and the return were already right.
    3. Check by tracing. The additions are now 0, 1, 2 and 3, so sum goes through 0, 1, 3 and 6, and the function returns 6.

    The fixed code in Kotlin

    fun computeSum(n: Int): Int {
        var sum = 0
        for (i in 0 until n) {
            sum += i
        }
        return sum
    }
    
    fun mainFunction() {
        println(computeSum(4))
    }
    

    Notes on the Kotlin behind it:

    • until excludes the end. 0 until n covers 0 to n - 1, which matches the task's sum. The range 0..n would also add n.
    • var for the total. sum changes on each pass, so it needs var. A val would not allow +=.
    • The parameter is a constant during the call. It keeps the value it was called with. Only the loop variable moves, so any code that is meant to react to the loop has to mention it.
    • Int can overflow. The return type is a 32-bit Int, which wraps around silently past 2,147,483,647. For very large n the sum would need a Long.

    Checking the fix against a second input

    One example can hide a coincidence, so test another. With computeSum(5) the fixed loop adds 0, 1, 2, 3 and 4, and returns 10. The buggy version would return 25, which is 5 times 5. Seeing the wrong and the right answer side by side for more than one input confirms that the change fixed the logic itself, not only the single example that mainFunction prints.

    Edge cases and common mistakes

    • n = 0 and n = 1. For 0 the range is empty and the function returns 0. For 1 the range holds only 0, so the result is again 0.
    • Negative n. The range 0 until n is empty, so the function returns 0.
    • Using 0..n. It also adds n. For n = 4 that gives 10 instead of 6.
    • Adding n after the loop. Moving the mistake around doesn't help: the sum has to be built from i inside the loop.

    When the output equals one value repeated a number of times, as 16 is 4 four times, look for the loop body using something that never changes.

    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free