File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Pigeons
2+ When I start to feed my pigeon, a minute later two more fly by. And a minute later another 3.
3+ Then 4, and so on. One portion of food lasts a pigeon for a minute.
4+ In case there’s not enough food for all the birds, the pigeons that came first, eat first. Pigeons are hungry animals and eat without stopping.
5+ How many pigeons I’d have to feed at least once if I have N portions wheat?
6+
7+ -------
8+
9+ ** Enjoy**
10+
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
7+ < title > Pigeons</ title >
8+ </ head >
9+ < body >
10+ < script src ="pigeons.js "> </ script >
11+ </ body >
12+ </ html >
Original file line number Diff line number Diff line change 1+ function feeding ( totalWheat ) {
2+ let pigeonsTotal = 0 ;
3+ let nextPigeonGroup = 1 ;
4+ let currWheat = totalWheat ;
5+ while ( pigeonsTotal < currWheat ) {
6+ if ( pigeonsTotal + nextPigeonGroup > totalWheat ) break ;
7+ pigeonsTotal += nextPigeonGroup ;
8+ currWheat -= pigeonsTotal ;
9+ nextPigeonGroup ++ ;
10+ }
11+ return console . log ( `With ${ totalWheat } wheat you can feed ${ pigeonsTotal } pigeons - last group of pigeons is equal to ${ nextPigeonGroup } ` )
12+ }
13+
14+
15+ // 1 pigeon - 1 wheat
16+ // total wheat 10 - 1 = 9
17+ // 2 next pigeons group + 1 curr pigeon = 3 pigeons - 3 wheat
18+ // total wheat 9 - 3 = 6
19+ // 3 next pigeons group + 3 curr pigeons = 6 pigeons - 6 wheat
20+ // total wheat 6 - 6 = 0
21+ // total wheat < pigeonsTotal = break
22+
23+ feeding ( 10 ) ;
You can’t perform that action at this time.
0 commit comments