12345678910111213141516171819202122232425262728 |
- package kata
- import "math"
- // percent is no predivided by 100: 2% is passed as 2.
- func NextPop(pn int, percent float64, aug int) int {
- next := int(math.Floor(float64(pn) * (1 + percent/100))) + aug
- return next
- }
- // Cannot use the power formula because of integer truncation at each step.
- func PopN(p0 int, percent float64, aug int, n int) int {
- pn := p0
- for i := 0; i < n; i++ {
- pn = NextPop(pn, percent, aug)
- }
- return pn
- }
- func NbYear(p0 int, percent float64, aug int, p int) int {
- pn := p0
- year := 0
- // Support population decrease caused by invalid parameters.
- for year = 0; pn < p && pn >= 0; year++ {
- pn = NextPop(pn, percent, aug)
- }
- return year
- }
|