Problem Statement
My shoelace broke the other day. I was too lazy to take it out and figure out how long it was, so I decided to calculate the length based on the number of eyelets and their separation.
The "horizontal" distance between the eyelets can increase or decrease from the bottom to the top of the shoe. The startWidth is how far apart they are at the bottom (near the toes) and the endWidth is how far apart they are at the top (near the tongue of the shoe). This distance increases (or decreases) linearly from bottom to top. Furthermore, the "vertical" distance, spread, between eyelets is constant.
Write a method, calculate, which calculates the length of the entire shoelace, given the initial distance between the eyelets (startWidth), the final distance between the eyelets (endWidth), distance between each pair of vertically adjacent eyelets (spread) and the number of pairs of eyelets (numPairs).
You should assume all laces follow this general pattern:
|----| <- startWidth O----O - \ / | \/ | <- spread /\ | / \ | O O - \ / \/ /\ / \ O O |----| <- endWidth
In this illustration, startWidth and endWidth are identical, and numPairs is 3.
Definition
- Class:
- ShoelaceLength
- Method:
- calculate
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double calculate(int startWidth, int endWidth, int spread, int numPairs)
- (be sure your method is public)
Notes
- startWidth may be greater than, equal to, or less than endWidth.
- Your return value must have a relative or absolute error less than 1e-9.
- The units of measurement are irrelevant.
- The length of lace that it takes to go around or through each eyelet should be ignored.
- The length of lace needed to actually tie a knot is not included.
Constraints
- numPairs is the number of PAIRS of eyelets, and will be between 2 and 50, inclusive.
- startWidth, endWidth, and spread will each be between 1 and 99, inclusive.
Examples
2
2
1
2
Returns: 6.47213595499958
These are my topsiders. Remember to add the start width across the bottom pair of eyelets.
10
1
1
10
Returns: 111.1786186482248
1
10
1
10
Returns: 102.17861864822481
Nearly the same configuration as the previous case, except the width goes from 1 to 10 instead of 10 to 1.
1
1
1
2
Returns: 3.8284271247461903
1
99
19
2
Returns: 107.97663296253066
2
98
25
4
Returns: 346.86770343916925
3
97
29
6
Returns: 600.7501476283504
4
96
31
9
Returns: 978.2049477141356
5
95
32
10
Returns: 1109.727068795039
6
94
33
13
Returns: 1490.9970789116976
7
93
35
18
Returns: 2148.5801399226502
8
92
39
23
Returns: 2885.718652644393
9
91
50
30
Returns: 4232.975184119522
10
90
55
32
Returns: 4746.163504230501
11
89
62
35
Returns: 5562.216792596627
12
88
73
37
Returns: 6517.759007894257
13
87
84
43
Returns: 8369.861553307757
15
85
95
47
Returns: 10028.668418159381
19
81
99
49
Returns: 10776.746614246898
25
75
2
50
Returns: 4929.303537043948
29
71
3
48
Returns: 4738.006277672702
31
69
4
46
Returns: 4546.128377706249
32
68
5
44
Returns: 4354.437390419365
33
67
6
41
Returns: 4062.8616465306095
35
65
7
40
Returns: 3974.20863404904
39
61
8
37
Returns: 3685.5242417004815
50
50
9
32
Returns: 3199.8196773783734
55
45
10
27
Returns: 2706.6542341425043
62
38
11
20
Returns: 2008.283050442426
73
27
12
18
Returns: 1824.8393831065948
84
16
13
15
Returns: 1539.2198034842877
95
5
15
13
Returns: 1368.815085140135
99
1
20
7
Returns: 763.8651799756235
10
1
1
10
Returns: 111.1786186482248
1
10
1
10
Returns: 102.17861864822481
1
99
19
2
Returns: 107.97663296253066
2
2
1
2
Returns: 6.47213595499958
97
13
3
10
Returns: 1088.913580216457
1
90
1
40
Returns: 3551.882378583914
15
30
5
6
Returns: 245.68293688965883
10
5
2
4
Returns: 56.62213195631012
1
10
5
2
Returns: 15.866068747318506