DEV Community

loading...

Discussion on: Daily Challenge #271 - Simulate Population Growth

Collapse
bravemaster619 profile image
bravemaster619 • Edited

This is a 7kyu question in codewar but it's a bit tricky.

I remember I solved it after tens of submissions, because one or two tests were failed for some reason.

Turned out that I should have to deal with integers more carefully.

Here is an answer for ruby version:

def nb_year(p0, percent, aug, p)
    year = 0
    while p0 < p
      year += 1
      p0 = spawn(p0, percent, aug)
    end
    year
end

def spawn(p, percent, aug)
  (p + (p * percent / 100).floor + aug)
end