← Back to challenges

Finitely Expressible Rational Numbers in a Number Base

PythonHardalgorithmsmathvalidation

Instructions

A number is finitely expressible if you can write it in floating point form and only use a finite number of digits.

  • 1/3 is not finitely expressible in base 10 as it requires an infinite number of digits to write out: 0.333333 ...
  • 3/2 is finitely expressible in base 10 as it requires only two digits: 1.5

Create a function that takes in a rational number in the form of a tuple and a number base as arguments and returns true if the rational is finitely expressible in the number base and false if not.

isFinitelyExpressible((numerator, denominator), number_base)

Examples

isFinitelyExpressible((1, 3), 10) ➞ False
# 0.3333...

isFinitelyExpressible((4, 1), 10) ➞ True
# 4

isFinitelyExpressible((4, 7), 2)  ➞ False
# 0.1001001...

Notes

  • The rational numbers given to you will be in lowest terms.
  • Python doesn't have infinite floating point precision so writing out a number in a base won't work and the bases in the tests will be quite large.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.