re: Daily Challenge #111 - 99 Bottles of Beer VIEW POST

FULL DISCUSSION
 

My solution in Swift (without Bonus it's saturday 😛) :

func numberOfBottles(numberOfBottle: Int) -> (restOfBottles: Int, loop:Int) {
    guard numberOfBottle > 0 else { return (0,0) }
    guard numberOfBottle != 1 else { return (1,1) }
    var number = 1
    var loop = 0
    var numberOfBottle = numberOfBottle

    while (numberOfBottle - number) > 0 {
        loop += 1
        numberOfBottle -= number
        number += 1
    }
    return (numberOfBottle,loop)
}

Edit: Thank you to @vo_kononenko for to find my mistake

 

It looks like number *= 2 at the end of the loop should be replaced with number += 1, because the number of removed bottles on each iteration increases by 1, not doubles.

 

It's right ! I do that too fastly. Thank you for your attention.

code of conduct - report abuse