Statistics

Problem Statement for "ApproximateDivision"

Problem Statement

Division is an expensive operation for a computer to perform, compared to addition, subtraction, and even multiplication. The exception is when dividing by powers of 2, because this can be done either with a bit shift (for a fixed-point value) or by subtracting 1 from the exponent (for a floating-point value). In this problem, we will approximate the quotient of two numbers using only addition, multiplication, and division by powers of 2.

Consider the following identity:


     1      1      c^0     c^1     c^2
    --- = ----- = ----- + ----- + ----- + ...
     b     t-c     t^1     t^2     t^3

If t is a power of 2, then the denominator of each term will be a power of 2.

Given integers a, b, and terms, approximate a/b by computing the first terms terms of the identity above, and multiplying the result by a. Select t to be the smallest power of 2 greater than or equal to b.

Definition

Class:
ApproximateDivision
Method:
quotient
Parameters:
int, int, int
Returns:
double
Method signature:
double quotient(int a, int b, int terms)
(be sure your method is public)

Notes

  • Your return value must have an absolute or relative error less than 1e-9.

Constraints

  • b will be between 1 and 10000, inclusive.
  • a will be between 0 and b, inclusive.
  • terms will be between 1 and 20, inclusive.

Examples

  1. 2

    5

    2

    Returns: 0.34375

    In this case t is chosen to be 8, and therefore c is 3. The first two terms are 1/8 and 3/64.

  2. 7

    8

    5

    Returns: 0.875

    If b is a power of two, the first term is equal to exactly 1/b, and all other terms are zero.

  3. 1

    3

    10

    Returns: 0.33333301544189453

  4. 1

    10000

    2

    Returns: 8.481740951538086E-5

  5. 1

    7

    20

    Returns: 0.14285714285714285

  6. 0

    4

    3

    Returns: 0.0

  7. 713

    1301

    15

    Returns: 0.5480398217951847

  8. 1

    10000

    1

    Returns: 6.103515625E-5

  9. 1

    10000

    2

    Returns: 8.481740951538086E-5

  10. 1

    10000

    3

    Returns: 9.408412734046578E-5

  11. 1

    10000

    11

    Returns: 9.99968565805973E-5

  12. 10

    9999

    20

    Returns: 0.0010001000034717592

  13. 777

    778

    7

    Returns: 0.9986685331499519

  14. 4

    9

    6

    Returns: 0.4413278102874756

  15. 7

    22

    2

    Returns: 0.287109375

  16. 333

    512

    1

    Returns: 0.650390625

  17. 333

    512

    20

    Returns: 0.650390625

  18. 14

    28

    5

    Returns: 0.4999847412109375

  19. 3

    12

    9

    Returns: 0.2499990463256836

  20. 1

    24

    19

    Returns: 0.041666666666515084

  21. 17

    17

    3

    Returns: 0.897003173828125

  22. 19

    19

    11

    Returns: 0.999950257556668

  23. 50

    50

    1

    Returns: 0.78125

  24. 513

    513

    2

    Returns: 0.7509756088256836

  25. 513

    513

    15

    Returns: 0.9999703643707892

  26. 513

    513

    20

    Returns: 0.999999082895404

  27. 11

    9998

    4

    Returns: 0.0010748269598298554

  28. 9

    10

    8

    Returns: 0.8996480405330658

  29. 5983

    7439

    13

    Returns: 0.8042747681139668

  30. 1823

    9837

    18

    Returns: 0.1853207153579327

  31. 0

    8473

    20

    Returns: 0.0

  32. 1

    30

    2

    Returns: 0.033203125

  33. 2

    31

    1

    Returns: 0.0625

  34. 28

    29

    3

    Returns: 0.9647216796875

  35. 23

    29

    4

    Returns: 0.7930421829223633

  36. 5001

    10000

    1

    Returns: 0.30523681640625

  37. 5001

    10000

    2

    Returns: 0.4241718649864197

  38. 5001

    10000

    15

    Returns: 0.5000996376310812

  39. 5001

    10000

    20

    Returns: 0.5000999967452651

  40. 63

    65

    1

    Returns: 0.4921875

  41. 63

    65

    2

    Returns: 0.73443603515625

  42. 63

    65

    9

    Returns: 0.9675879022119923

  43. 10000

    10000

    20

    Returns: 0.9999999934918318

  44. 1

    10000

    20

    Returns: 9.999999934918317E-5

  45. 1000

    1000

    20

    Returns: 1.0

  46. 798

    799

    20

    Returns: 0.998748435544362

  47. 1

    1

    3

    Returns: 1.0

  48. 1

    8193

    20

    Returns: 1.220552970403139E-4

  49. 8000

    8193

    20

    Returns: 0.9764423763225112

  50. 1

    1

    20

    Returns: 1.0

  51. 4535

    6161

    20

    Returns: 0.736081804901235

  52. 9999

    10000

    20

    Returns: 0.9998999934924826

  53. 20

    997

    20

    Returns: 0.020060180541624874

  54. 8193

    8193

    20

    Returns: 0.9999990486512919

  55. 5

    99

    20

    Returns: 0.050505050505044086

  56. 1

    1

    10

    Returns: 1.0

  57. 2

    999

    20

    Returns: 0.0020020020020020016

  58. 783

    999

    20

    Returns: 0.7837837837837837

  59. 50

    10000

    20

    Returns: 0.0049999999674591586

  60. 1

    1

    1

    Returns: 1.0

  61. 4200

    8400

    20

    Returns: 0.4999997149101312

  62. 1

    8194

    20

    Returns: 1.2204040163186624E-4

  63. 1000

    10000

    20

    Returns: 0.09999999934918317

  64. 3

    4

    2

    Returns: 0.75

  65. 5

    10000

    20

    Returns: 4.999999967459158E-4

  66. 80

    4100

    20

    Returns: 0.019512176873762718

  67. 100

    8190

    20

    Returns: 0.01221001221001221

  68. 4579

    6789

    20

    Returns: 0.674473412873766

  69. 100

    128

    20

    Returns: 0.78125

  70. 1

    9999

    4

    Returns: 9.770321513903613E-5

  71. 1

    1

    5

    Returns: 1.0

  72. 123

    10000

    20

    Returns: 0.01229999991994953

  73. 54

    8193

    20

    Returns: 0.006590986040176951

  74. 1

    9000

    20

    Returns: 1.1111109783128265E-4

  75. 25

    25

    1

    Returns: 0.78125

  76. 3500

    7689

    20

    Returns: 0.4551957341656913

  77. 1

    8

    1

    Returns: 0.125

  78. 1

    1

    7

    Returns: 1.0

  79. 5000

    10000

    20

    Returns: 0.4999999967459159

  80. 7

    2048

    20

    Returns: 0.00341796875

  81. 2

    9

    20

    Returns: 0.22222220755497427

  82. 15

    16

    20

    Returns: 0.9375

  83. 9998

    9999

    20

    Returns: 0.9998999834710648

  84. 1

    1

    2

    Returns: 1.0

  85. 1

    2

    15

    Returns: 0.5

  86. 2

    3

    5

    Returns: 0.666015625

  87. 5

    7

    20

    Returns: 0.7142857142857142

  88. 10

    16

    20

    Returns: 0.625

  89. 4000

    8100

    19

    Returns: 0.4938271604938271


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: