Statistics

Problem Statement for "AnomalousCancellation"

Problem Statement

Given a fraction, you can divide its numerator and denominator by one of their common divisors to get a fraction with the same value. For example, 8/16 can become 4/8 if you divide the numerator and denominator by 2. With some fractions, you can get a similar result by removing digits that are common to both the numerator and denominator. The removed digits must occur in the same relative order in both the numerator and denominator of the original fraction.
For example, consider 4784/7475. We can remove the 7 in the numerator and the first 7 in the denominator, and then remove the second 4 in the numerator and the 4 in the denominator to get 48/75, which has the same value as the original fraction.
You are given the numerator and denominator of a fraction. Use the procedure described above to get a fraction with the smallest possible numerator. Return the resulting fraction formatted as "NUMERATOR/DENOMINATOR" (quotes for clarity only), where both the numerator and denominator are written without leading zeroes. If the procedure cannot be applied, return the original fraction.

Definition

Class:
AnomalousCancellation
Method:
reducedFraction
Parameters:
int, int
Returns:
String
Method signature:
String reducedFraction(int numerator, int denominator)
(be sure your method is public)

Constraints

  • numerator and denominator will each be between 10 and 9999, inclusive.

Examples

  1. 16

    64

    Returns: "1/4"

    We can cancel the 6's to get 16/64=1/4.

  2. 4784

    7475

    Returns: "48/75"

    This is the example from the problem statement.

  3. 25

    25

    Returns: "2/2"

    The fraction can be reduced to 2/2 or 5/5. We choose 2/2 since it has the smallest numerator.

  4. 95

    19

    Returns: "5/1"

    We can cancel the 9's to get 95/19=5/1.

  5. 100

    1000

    Returns: "1/10"

    We can cancel two 0's to get 100/1000=1/10.

  6. 123

    456

    Returns: "123/456"

    The numerator and denominator have no digits in common, so the cancellation procedure can't be applied.

  7. 151

    1057

    Returns: "1/7"

    After cancelling the 1's and the 5's we get 151/1057=1/07, and we write the denominator without the leading zero.

  8. 776

    197

    Returns: "776/197"

  9. 119

    611

    Returns: "119/611"

  10. 472

    991

    Returns: "472/991"

  11. 693

    307

    Returns: "693/307"

  12. 399

    761

    Returns: "399/761"

  13. 562

    841

    Returns: "562/841"

  14. 929

    26

    Returns: "929/26"

  15. 851

    901

    Returns: "851/901"

  16. 13

    952

    Returns: "13/952"

  17. 61

    766

    Returns: "61/766"

  18. 102

    110

    Returns: "102/110"

  19. 775

    485

    Returns: "775/485"

  20. 104

    708

    Returns: "104/708"

  21. 682

    380

    Returns: "682/380"

  22. 534

    789

    Returns: "534/789"

  23. 774

    450

    Returns: "774/450"

  24. 756

    754

    Returns: "756/754"

  25. 996

    424

    Returns: "996/424"

  26. 819

    371

    Returns: "819/371"

  27. 268

    796

    Returns: "268/796"

  28. 530

    958

    Returns: "530/958"

  29. 705

    799

    Returns: "705/799"

  30. 423

    267

    Returns: "423/267"

  31. 756

    408

    Returns: "756/408"

  32. 530

    528

    Returns: "530/528"

  33. 16

    250

    Returns: "16/250"

  34. 102

    442

    Returns: "102/442"

  35. 26

    65

    Returns: "2/5"

  36. 13

    325

    Returns: "1/25"

  37. 34

    136

    Returns: "4/16"

  38. 49

    392

    Returns: "4/32"

  39. 83

    332

    Returns: "8/32"

  40. 23

    1265

    Returns: "3/165"

  41. 39

    2925

    Returns: "3/225"

  42. 53

    1325

    Returns: "5/125"

  43. 79

    9875

    Returns: "7/875"

  44. 104

    208

    Returns: "14/28"

  45. 130

    325

    Returns: "10/25"

  46. 234

    936

    Returns: "24/96"

  47. 473

    572

    Returns: "43/52"

  48. 424

    742

    Returns: "4/7"

  49. 104

    1508

    Returns: "4/58"

  50. 345

    2415

    Returns: "3/21"

  51. 428

    4922

    Returns: "8/92"

  52. 549

    1098

    Returns: "54/108"

  53. 637

    1638

    Returns: "7/18"

  54. 8436

    9435

    Returns: "836/935"

  55. 7878

    8787

    Returns: "78/87"

  56. 5994

    7992

    Returns: "54/72"

  57. 4932

    8631

    Returns: "492/861"

  58. 4095

    8099

    Returns: "45/89"

  59. 2349

    9396

    Returns: "24/96"

  60. 98

    49

    Returns: "8/4"

  61. 121

    22

    Returns: "11/2"

  62. 195

    39

    Returns: "15/3"

  63. 352

    55

    Returns: "32/5"

  64. 192

    96

    Returns: "12/6"

  65. 1728

    27

    Returns: "128/2"

  66. 1495

    46

    Returns: "195/6"

  67. 3584

    56

    Returns: "384/6"

  68. 5312

    83

    Returns: "512/8"

  69. 616

    112

    Returns: "66/12"

  70. 253

    154

    Returns: "23/14"

  71. 737

    335

    Returns: "77/35"

  72. 994

    497

    Returns: "94/47"

  73. 2461

    214

    Returns: "46/4"

  74. 1396

    349

    Returns: "16/4"

  75. 9796

    496

    Returns: "79/4"

  76. 9328

    583

    Returns: "928/58"

  77. 5192

    649

    Returns: "512/64"

  78. 8918

    8190

    Returns: "98/90"

  79. 6958

    7952

    Returns: "658/752"

  80. 7326

    5328

    Returns: "726/528"

  81. 6005

    4804

    Returns: "605/484"

  82. 5961

    3974

    Returns: "561/374"

  83. 81

    81

    Returns: "1/1"

  84. 29

    29

    Returns: "2/2"

  85. 208

    208

    Returns: "2/2"

  86. 564

    564

    Returns: "4/4"

  87. 9357

    9357

    Returns: "3/3"

  88. 8077

    8077

    Returns: "7/7"

  89. 210

    1180

    Returns: "21/118"

  90. 1300

    1000

    Returns: "13/10"

  91. 600

    7800

    Returns: "6/78"

  92. 500

    250

    Returns: "50/25"

  93. 451

    4059

    Returns: "1/9"

  94. 1083

    1805

    Returns: "3/5"

  95. 1089

    1815

    Returns: "9/15"

  96. 7961

    419

    Returns: "7961/419"

  97. 312

    624

    Returns: "312/624"

  98. 412

    721

    Returns: "412/721"

  99. 12

    48

    Returns: "12/48"


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: