Problem Statement
The inchworm is a creature of regular habits. She inches forward some distance along the branch of a tree, then stops to rest. If she has stopped at a leaf, she makes a meal of it. Then she inches forward the same distance as before, and repeats this routine until she has reached or passed the end of the branch.
Consider an inchworm traveling the length of a branch whose leaves are spaced at uniform intervals. Depending on the distance between her resting points, the inchworm may or may not be able to eat all of the leaves. There is always a leaf at the beginning of the branch, which is where the inchworm rests before setting out on her journey.
You are given three
Definition
- Class:
- Inchworm
- Method:
- lunchtime
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int lunchtime(int branch, int rest, int leaf)
- (be sure your method is public)
Notes
- The inchworm starts by gobbling up the leaf at the beginning of the branch.
- If there is a leaf at the end of the branch, the inchworm eats it only if it falls at a resting point.
Constraints
- branch is between 1 and 1000000 (one million), inclusive
- rest is between 1 and 1000, inclusive
- leaf is between 1 and 1000, inclusive
Examples
11
2
4
Returns: 3
Leaves grow 0, 4, and 8 inches from the beginning of the branch. The inchworm eats them all.
12
6
4
Returns: 2
The inchworm misses the leaves growing at distances 4 and 8, but eats those at 0 and 12.
20
3
7
Returns: 1
The inchworm eats only the leaf at the outset.
21
7
3
Returns: 2
The inchworm eats the leaves at the beginning and end of the branch.
15
16
5
Returns: 1
The inchworm eats only the leaf at the outset.
1000
3
7
Returns: 48
1000
7
3
Returns: 48
7872
286
64
Returns: 1
17306
139
42
Returns: 3
22207
279
86
Returns: 1
31216
912
77
Returns: 1
44450
25
27
Returns: 66
50310
3
33
Returns: 1525
68852
172
27
Returns: 15
70175
499
82
Returns: 2
87668
819
81
Returns: 12
98020
977
28
Returns: 4
103970
534
91
Returns: 3
116737
387
25
Returns: 13
126533
462
24
Returns: 69
139739
890
93
Returns: 2
141638
102
38
Returns: 74
150112
644
26
Returns: 18
168301
60
91
Returns: 31
175599
736
7
Returns: 35
186508
537
25
Returns: 14
196965
770
52
Returns: 10
203658
459
29
Returns: 16
212006
320
10
Returns: 663
220831
980
80
Returns: 57
233971
313
94
Returns: 8
242959
117
72
Returns: 260
254069
47
46
Returns: 118
266189
595
79
Returns: 6
270824
388
95
Returns: 8
288285
606
36
Returns: 80
291959
124
61
Returns: 39
308550
620
28
Returns: 72
310109
850
26
Returns: 29
328657
98
86
Returns: 78
336738
810
14
Returns: 60
348301
131
78
Returns: 35
356116
325
51
Returns: 22
364757
319
51
Returns: 23
372894
762
24
Returns: 123
387992
247
100
Returns: 16
396711
804
83
Returns: 6
408058
327
83
Returns: 16
415448
289
93
Returns: 16
428714
275
27
Returns: 58
431027
73
68
Returns: 87
440817
675
98
Returns: 7
455523
579
70
Returns: 12
463439
197
76
Returns: 31
476703
824
67
Returns: 9
486475
646
2
Returns: 754
496412
578
92
Returns: 19
508990
78
90
Returns: 436
517569
55
509
Returns: 19
527327
81
327
Returns: 60
531784
55
329
Returns: 30
546447
59
608
Returns: 16
558890
3
933
Returns: 600
565227
31
172
Returns: 107
572565
28
692
Returns: 119
587437
49
594
Returns: 21
598465
2
873
Returns: 343
608562
49
386
Returns: 33
618186
39
502
Returns: 32
621989
27
904
Returns: 26
638644
59
506
Returns: 22
649302
63
663
Returns: 47
659709
7
11
Returns: 8568
660314
40
812
Returns: 82
673601
61
85
Returns: 130
681181
17
479
Returns: 84
692402
42
712
Returns: 47
709292
4
348
Returns: 2039
710104
3
154
Returns: 1538
729917
13
341
Returns: 165
734539
95
428
Returns: 19
744435
90
743
Returns: 12
756509
60
339
Returns: 112
760805
80
994
Returns: 20
772319
33
440
Returns: 586
784781
85
288
Returns: 33
796478
14
345
Returns: 165
801670
45
293
Returns: 61
815867
51
99
Returns: 485
822467
9
688
Returns: 133
835003
29
696
Returns: 1200
840738
31
737
Returns: 37
857463
32
118
Returns: 455
867877
49
573
Returns: 31
875741
98
638
Returns: 29
883104
90
881
Returns: 12
899002
11
936
Returns: 88
906502
86
315
Returns: 34
918346
53
573
Returns: 31
926058
23
76
Returns: 530
10
2
2
Returns: 6
10
1
1
Returns: 11
1000
1
1
Returns: 1001
4
3
2
Returns: 1
1000000
1
1
Returns: 1000001