Форум программистов, компьютерный форум, киберфорум
alhaos
Войти
Регистрация
Восстановить пароль
Карта форума Блоги Сообщество Поиск Заказать работу  
Рейтинг: 1.00. Голосов: 1.

Задача: Growth of a Population

Запись от alhaos размещена 01.08.2021 в 12:27
Обновил(-а) alhaos 01.08.2021 в 13:07

(с) https://www.codewars.com/kata/... b5120000c6

In a small town the population is p0 = 1000 at the beginning of a year. The population regularly increases by 2 percent per year and moreover 50 new inhabitants per year come to live in the town. How many years does the town need to see its population greater or equal to p = 1200 inhabitants?

At the end of the first year there will be:
1000 + 1000 * 0.02 + 50 => 1070 inhabitants

At the end of the 2nd year there will be:
1070 + 1070 * 0.02 + 50 => 1141 inhabitants (** number of inhabitants is an integer **)

At the end of the 3rd year there will be:
1141 + 1141 * 0.02 + 50 => 1213

It will need 3 entire years.
More generally given parameters:

p0, percent, aug (inhabitants coming or leaving each year), p (population to surpass)

the function nb_year should return n number of entire years needed to get a population greater or equal to p.

aug is an integer, percent a positive or null floating number, p0 and p are positive integers (> 0)

Examples:
Код:
nb_year(1500, 5, 100, 5000) -> 15
nb_year(1500000, 2.5, 10000, 2000000) -> 10
Note:
Don't forget to convert the percent parameter as a percentage in the body of your function: if the parameter percent is 2 you have to convert it to 0.02.

PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$DebugPreference = "continue"
 
class Arge {
    static [int] NbYear([int] $p0, [double] $percent, [int] $aug, [int] $p) {
        $percent *= .01
        for ($i = 1; $true ; $i++){
            Write-Debug ("Year: {4,3}, p0: [{0}], percent: [{1}], aug: [{2}], p: [{3,15}], calc: {0}+{0}*{1}+{2}={3}" -f $p0, $percent, $aug, ($p0 + $p0 * $percent + $aug), $i)
            $p0 = $p0 + [Math]::Floor($p0 * $percent) + $aug
            if ($p0 -ge $p){
                return $i
            }
        }
        return -1
    }
}
 
[Arge]::NbYear(1500000, 2.5, 10000, 2000000)
Код:
DEBUG: Year:   1, p0: [1500000], percent: [0,025], aug: [10000], p: [        1547500], calc: 1500000+1500000*0,025+10000=1547500
DEBUG: Year:   2, p0: [1547500], percent: [0,025], aug: [10000], p: [      1596187,5], calc: 1547500+1547500*0,025+10000=1596187,5
DEBUG: Year:   3, p0: [1596188], percent: [0,025], aug: [10000], p: [      1646092,7], calc: 1596188+1596188*0,025+10000=1646092,7
DEBUG: Year:   4, p0: [1646093], percent: [0,025], aug: [10000], p: [    1697245,325], calc: 1646093+1646093*0,025+10000=1697245,325
DEBUG: Year:   5, p0: [1697245], percent: [0,025], aug: [10000], p: [    1749676,125], calc: 1697245+1697245*0,025+10000=1749676,125
DEBUG: Year:   6, p0: [1749676], percent: [0,025], aug: [10000], p: [      1803417,9], calc: 1749676+1749676*0,025+10000=1803417,9
DEBUG: Year:   7, p0: [1803418], percent: [0,025], aug: [10000], p: [     1858503,45], calc: 1803418+1803418*0,025+10000=1858503,45
DEBUG: Year:   8, p0: [1858503], percent: [0,025], aug: [10000], p: [    1914965,575], calc: 1858503+1858503*0,025+10000=1914965,575
DEBUG: Year:   9, p0: [1914966], percent: [0,025], aug: [10000], p: [     1972840,15], calc: 1914966+1914966*0,025+10000=1972840,15
DEBUG: Year:  10, p0: [1972840], percent: [0,025], aug: [10000], p: [        2032161], calc: 1972840+1972840*0,025+10000=2032161
10
Размещено в Без категории
Показов 3012 Комментарии 2
Всего комментариев 2
Комментарии
  1. Старый комментарий
    Do not they die?
    Запись от Elmar_Velihanov размещена 01.08.2021 в 18:12 Elmar_Velihanov вне форума
  2. Старый комментарий
    Аватар для alhaos
    everyone will die in the end
    Запись от alhaos размещена 02.08.2021 в 09:09 alhaos вне форума
 
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru