Questions tagged [rational-numbers]
This challenge involves the manipulation of rational numbers, i.e. those which can be represented as a fraction of integers. Do not use this tag if rational numbers are just one of several admissible input/output formats, but rather if the use of exact rational arithmetic is required.
110 questions
9
votes
4
answers
496
views
Ultra-modular representative of rational numbers
Objective
Given a reduced fraction \$p/q\$ where \$q > 0\$, output the (unique) reduced fraction \$p'/q'\$ such that:
\$p'\$ is nonnegative and less than \$q'\$, and
\$q'\$ is a positive odd ...
11
votes
5
answers
827
views
Show order equivalence between the rationals and the binary fractions
Write a function \$f\$ which takes rational numbers and gives binary fractions (rational numbers whose denominator is a power of two) which is bijective and preserves order.
That means:
Different ...
-5
votes
4
answers
194
views
Determine if a rational number is an integer, without division [closed]
Determine if a rational number is also an integer, without any form of dividing. The input should be any pair of numerator and denominator. Don't forget to consider negative numbers, and division by ...
8
votes
5
answers
813
views
Find a fraction's parent in the Stern-Brocot tree
Objective
Given a positive reduced fraction, output its parent in the Stern-Brocot tree. The outputted fraction shall also be reduced.
The Stern-Brocot tree
The Stern-Brocot tree is an infinite-height ...
10
votes
11
answers
1k
views
Decide symmetry of fractions
Objective
Given a nonnegative fraction whose denominator is a power of 2, decide whether its (finite-length) binary expansion is symmetric by the radix point.
I/O format
It is assumed that the input ...
7
votes
6
answers
435
views
Fractions nobody needs (because they can be reduced to a simpler form)
It happened in the 19th century. Georg was bored and started counting the rational numbers. Surprisingly, he discovered that there were no more of them than natural numbers. This insight made Georg ...
6
votes
1
answer
441
views
Counterexample to Shapiro inequality
Input: A positive integer n such that
n is even and greater than 12 or
n is odd and greater ...
20
votes
21
answers
1k
views
Rabinowitz-Wagon \$\pi\$ formula
In 1995, Stanley Rabinowitz and Stan Wagon found an interesting algorithm to generate the digits of \$\pi\$ one by one without storing the previous results. The algorithm is called the spigot ...
10
votes
10
answers
884
views
Factoriadic Fraction Addition
Objective
Given two rational numbers represented in fractional factoriadic as defined below, add them, and output the result in fractional factoriadic.
Fractional factoriadic
Fractional factoriadic is ...
13
votes
14
answers
5k
views
NaN is not equal to NaN
In many programming languages, the floating-point value NaN, or "not a number", in some programming languages generated by the expression ...
11
votes
10
answers
2k
views
Egyptian fraction representations of 1 without prime denominators
Background
As noted in this question, for all positive integers \$n>2\$ there exists at least one Egyptian fraction representation (EFR) of \$n\$ distinct positive integers \$a_{1} < a_{2} < \...
10
votes
7
answers
906
views
Longest sequence of Egyptian fractions with n as denominator
Background
From Wikipedia: An Egyptian fraction is the sum of distinct unit fractions. That is, each fraction in the expression has a numerator equal to 1 and a denominator that is a positive integer, ...
12
votes
26
answers
2k
views
Find the smallest integer multiple of a Decimal
The Challenge
Given a rational number, determine the smallest number which is a positive integer multiple of it. Eg.
...
10
votes
13
answers
2k
views
Decimalize a Fraction
Preamble
A common pain-point when working with rational numbers and decimals is how infrequently one can represent their rational number as a clean, non-repeating decimal. Let's solve this by writing ...
21
votes
13
answers
2k
views
Minkowski's ?(x) for rational x
Here is Minkowski's question mark function:
It is a strictly increasing and continuous function from the reals to themselves that, among other unusual properties, maps rational numbers to dyadic ...