Problem Statement
Your friend, John, is a poor farmer living in the rural area. Each year, he has to produce a certain amount of crops in order to support his family of five.
One unit of crops can be produced from a 1 by 1 square of farm land. John needs to protect his farm from intruders and animals by putting barbed wire fences along the boundary of the farm. Being so poor, John wants to minimize the length of the fence he has to purchase to protect his farm. John can change the shape of his farm to any rectangle with integral width and height in order to achieve this minimal fencing. He turns to you for help.
You are given the amount of crops that John needs to produce each year. Your task is to write a function that returns the minimum length of barbed wire fence needed to protect John's farm. The farm must have exactly the area required to produce the amount of crops specified and must be a rectangle with integral length sides.
Definition
- Class:
- FarmFence
- Method:
- minFencing
- Parameters:
- int
- Returns:
- int
- Method signature:
- int minFencing(int crops)
- (be sure your method is public)
Constraints
- crops is between 1 and 1,000,000, inclusive
Examples
6
Returns: 10
The possible dimensions of the farm are 1x6 and 2x3 (ignoring rotations). The amount of fencing is 14 for 1x6 and 10 for 2x3. 10 is the minimum.
18
Returns: 18
The possible dimensions are 1x18, 2x9, and 3x6 (ignoring rotations). The required fencing are 38, 22, and 18, respectively. 18 is the minimum.
987
Returns: 136
123456
Returns: 1670
1000000
Returns: 4000
999983
Returns: 1999968
667
Returns: 104
687
Returns: 464
533
Returns: 108
332
Returns: 174
235
Returns: 104
680
Returns: 108
274
Returns: 278
799
Returns: 128
134
Returns: 138
298
Returns: 302
469
Returns: 148
586
Returns: 590
992
Returns: 126
487
Returns: 976
349
Returns: 700
186
Returns: 74
567
Returns: 96
564
Returns: 118
254
Returns: 258
931
Returns: 136
89
Returns: 180
11
Returns: 24
707
Returns: 216
662
Returns: 666
556
Returns: 286
944220
Returns: 31594
821337
Returns: 49844
653048
Returns: 3252
150682
Returns: 1574
472766
Returns: 67566
304379
Returns: 11592
6297
Returns: 4204
134427
Returns: 89624
174702
Returns: 5426
474513
Returns: 2852
216264
Returns: 18070
1687
Returns: 496
218176
Returns: 1870
40027
Returns: 6184
548923
Returns: 11512
982845
Returns: 43772
758018
Returns: 758022
323890
Returns: 2302
741497
Returns: 5364
874317
Returns: 582884
92054
Returns: 92058
639413
Returns: 5292
993074
Returns: 15090
699491
Returns: 8616
991771
Returns: 6856
1
Returns: 4
3
Returns: 8
2
Returns: 6
1
Returns: 4
1000000
Returns: 4000
17
Returns: 36
999998
Returns: 8382
14
Returns: 18
99991
Returns: 199984
4
Returns: 8
5
Returns: 12
10
Returns: 14
6
Returns: 10
20
Returns: 18