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.
number *= 2
number += 1
It's right ! I do that too fastly. Thank you for your attention.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My solution in Swift (without Bonus it's saturday 😛) :
Edit: Thank you to @vo_kononenko for to find my mistake
It looks like
number *= 2at the end of the loop should be replaced withnumber += 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.