k.go 672 B

12345678910111213141516171819202122232425262728
  1. package kata
  2. import "math"
  3. // percent is no predivided by 100: 2% is passed as 2.
  4. func NextPop(pn int, percent float64, aug int) int {
  5. next := int(math.Floor(float64(pn) * (1 + percent/100))) + aug
  6. return next
  7. }
  8. // Cannot use the power formula because of integer truncation at each step.
  9. func PopN(p0 int, percent float64, aug int, n int) int {
  10. pn := p0
  11. for i := 0; i < n; i++ {
  12. pn = NextPop(pn, percent, aug)
  13. }
  14. return pn
  15. }
  16. func NbYear(p0 int, percent float64, aug int, p int) int {
  17. pn := p0
  18. year := 0
  19. // Support population decrease caused by invalid parameters.
  20. for year = 0; pn < p && pn >= 0; year++ {
  21. pn = NextPop(pn, percent, aug)
  22. }
  23. return year
  24. }