Problem Statement
The dinky is a small fish that breeds monogamously and regularly. Within days of its birth, the male of the species seeks out a mate and bonds with her for life, never dallying with another. At the end of every month, the couple issues exactly two offspring, of which one is a boy and the other a girl. Each of these, in turn, goes off on its amorous quest. Inbreeding is not uncommon in the confines of a fish tank, so a pair of cousins or even siblings may end up mating. If there are more females than males, the excess number who cannot secure a mate will not give birth in that month.
Despite their diminutive dimensions and peaceful nature, a population of dinkies must not be allowed to multiply ad infinitum. Experts recommend that one allot at least half a liter of water per dinky fish. Anything less than that, and the tank is said to be crowded. The solution is either to buy a larger tank or to catch some dinkies for breakfast.
Given the volume of a tank in liters, the number of male dinkies currently inhabiting the tank, and the number of females present, you are to calculate the number of months that can elapse before the tank becomes crowded. Bear in mind that all couples reproduce simultaneously at the end of every month. If the input values are such that the tank is already crowded, the correct answer is 0. If the tank will become crowded at the end of the first month, the answer is 1.
Definition
- Class:
- DinkyFish
- Method:
- monthsUntilCrowded
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int monthsUntilCrowded(int tankVolume, int maleNum, int femaleNum)
- (be sure your method is public)
Notes
- Disregard the effects of mortality. Assume that all dinkies, young and old, live perpetually.
Constraints
- tankVolume is between 1 and 1000000 (one million), inclusive
- maleNum is between 1 and 1000, inclusive
- femaleNum is between 1 and 1000, inclusive
Examples
10
4
6
Returns: 2
Four couples form initially. At the end of the first month, four male and four female dinkies are born. There are now 18 dinky fish in total and eight couples. At the end of the second month, eight dinkies of each sex are born, for a total of 34, which does not allow for half a liter each in a ten-liter tank. Thus, it has taken two months to reach a crowded state.
100
4
6
Returns: 5
At the end of the fourth month, there are 130 dinky fish. At the end of the fifth, there are 258.
5
6
4
Returns: 1
A five-liter tank is just large enough for ten dinkies, but it becomes crowded at the end of the first month.
4
6
4
Returns: 0
This four-liter tank is already crowded with ten dinkies.
1000000
3
2
Returns: 19
431131
764
249
Returns: 11
948885
971
526
Returns: 11
21506
919
520
Returns: 6
118094
183
503
Returns: 10
955277
293
477
Returns: 12
678033
457
171
Returns: 12
268744
210
401
Returns: 11
327864
227
914
Returns: 11
411244
242
31
Returns: 14
307459
388
247
Returns: 11
356731
757
431
Returns: 10
291086
218
728
Returns: 11
835213
94
714
Returns: 14
781165
380
66
Returns: 14
299824
738
976
Returns: 9
700535
808
936
Returns: 10
470158
444
194
Returns: 12
524441
559
757
Returns: 10
977161
411
714
Returns: 12
204060
262
4
Returns: 16
25898
820
553
Returns: 6
426602
95
795
Returns: 13
992799
720
776
Returns: 11
669500
391
502
Returns: 11
641454
430
574
Returns: 11
273734
987
461
Returns: 10
90626
94
201
Returns: 10
812595
404
143
Returns: 13
337808
594
110
Returns: 12
536745
399
480
Returns: 11
523748
697
924
Returns: 10
513296
760
824
Returns: 10
48230
977
477
Returns: 7
173329
871
141
Returns: 11
77533
30
984
Returns: 12
141912
710
247
Returns: 10
545276
523
847
Returns: 11
308105
598
452
Returns: 10
294032
671
807
Returns: 9
557796
607
680
Returns: 10
330247
229
959
Returns: 11
135324
653
333
Returns: 9
582323
924
434
Returns: 11
749403
353
173
Returns: 13
226239
933
673
Returns: 9
459169
526
443
Returns: 11
136628
434
374
Returns: 9
35028
722
924
Returns: 6
349905
804
779
Returns: 9
477094
448
742
Returns: 11
901631
36
22
Returns: 16
540756
482
300
Returns: 11
772601
152
343
Returns: 13
629442
285
997
Returns: 12
992741
611
66
Returns: 14
28071
31
530
Returns: 10
559030
292
325
Returns: 11
434435
742
309
Returns: 11
807007
882
213
Returns: 12
896997
930
343
Returns: 12
774047
590
211
Returns: 12
243000
775
786
Returns: 9
204537
746
493
Returns: 9
920332
399
294
Returns: 12
195867
979
767
Returns: 8
569380
190
944
Returns: 12
539794
375
210
Returns: 12
676669
379
189
Returns: 12
401270
378
123
Returns: 12
825992
655
781
Returns: 11
590151
694
489
Returns: 11
881949
895
306
Returns: 12
974360
732
184
Returns: 13
530324
676
182
Returns: 12
571512
844
674
Returns: 10
561554
417
494
Returns: 11
345175
988
406
Returns: 10
546418
119
348
Returns: 13
356267
660
588
Returns: 10
308511
554
291
Returns: 11
702387
487
432
Returns: 11
34231
89
922
Returns: 9
685142
892
731
Returns: 10
312676
194
246
Returns: 11
395882
426
254
Returns: 11
738096
460
460
Returns: 11
874816
132
359
Returns: 13
576510
761
817
Returns: 10
241065
348
66
Returns: 12
452108
100
207
Returns: 13
641295
341
789
Returns: 11
324032
511
160
Returns: 11
878187
807
644
Returns: 11
65843
68
400
Returns: 10
980453
138
411
Returns: 13
30
4
12
Returns: 3
40
10
10
Returns: 3
1
1
1
Returns: 1
1
1
2
Returns: 0
1
2
1
Returns: 0
10
10
11
Returns: 0
100
3
5
Returns: 6
431131
764
249
Returns: 11
2
1
1
Returns: 2
50
50
50
Returns: 1
3
1
3
Returns: 2
6
6
7
Returns: 0
100
40
60
Returns: 2
6
6
6
Returns: 1
5
6
4
Returns: 1
10
6
4
Returns: 2
10
9
100
Returns: 0
2
2
3
Returns: 0
4
4
5
Returns: 0
8
12
1
Returns: 2
10
2
8
Returns: 2
20
1
20
Returns: 4
17
4
6
Returns: 3
8
9
8
Returns: 0