← Back to challenges

Rational Number

PythonHardalgorithmslogicmath

Instructions

Declare a division() function that gets two natural numbers (a, b) and return a string containing the rational number a / b in the form of a decimal fraction, possibly periodic.

Examples

division(2, 5) ➞ "0.4"

division(1, 6) ➞ "0.1(6)"

division(1, 3) ➞ "0.(3)"

division(1, 7) ➞ "0.(142857)"

division(1, 77) ➞ "0.(012987)"

Notes

  • The length of a periodic fraction can be more than 20 numbers.
  • The function should be efficient.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.