BruceEckel/AtomicKotlinExamples

1.14 Summary 1, Exercise 2: minor efficiency boost

diedummydie opened this issue · 2 comments

while loop given in solution:

    while (worker > 0) {
        result += worker % 10
        worker /= 10
        if (worker != 0) {
            result *= 10
        }
    }

The check performed inside the if is effectively identical to the check performed in the while loop, so this can be simplified:

    while (worker > 0) {
        result *= 10
        result += worker % 10
        worker /= 10
    }

The first iteration of result *= 10 performs no unwanted action since at that point result is still initialized to zero.

svtk commented

That's a good simplification, thank you! Updated the solution svtk/AtomicKotlinCourse@96e21f6

Issue fixed by Svetlana