Skip to content

2023-7: Let's Be Rational

A rational number is a number that can be expressed as the quotient of 2 integers p÷q — a numerator p and a denominator q. For example, for 1.5, p and q would be 3 and 2, respectively.

Write a function that:

  • takes a single non-zero positive number right argument.
  • returns a 2-element "integer" result representing the smallest non-zero positive values for p and q respectively

Notes:

  • We use "integer" in the result description because the result might contain a number larger than can be represented as an integer data type in Dyalog APL. For example, the result when applying the function to ⌊/⍬ would be 1.797693135E308 1 which is represented as a 64-bit floating point array.
  • The test for the correctness of your solution will be that, given
    r ← (your_function) a
    your solution should satisfy both the following:
    • r ≡ ⌊0.5+r
    • a = ÷/r

Hint: The Lowest Common Multiple function or Greatest Common Divisor function could be helpful in solving this problem.

Examples:

      (your_function) 1.2
6 5      

      (your_function) 3.5
7 2

      (your_function) ÷3
1 3
your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub