Skip to main content

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.

Filter by
Sorted by
Tagged with
5 votes
5 answers
220 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 ...
Sophia Antipolis's user avatar
6 votes
1 answer
249 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 than 23. Output: A list of non-negative integers that violates Shapiro inequality. More precisely, Let s be ...
Lucenaposition's user avatar
14 votes
16 answers
570 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 ...
alephalpha's user avatar
  • 49.4k
10 votes
10 answers
752 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 ...
Dannyu NDos's user avatar
  • 6,299
13 votes
14 answers
4k 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 ...
3-1-4-One-Five's user avatar
11 votes
10 answers
1k 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} < \...
Max Muller's user avatar
10 votes
7 answers
784 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, ...
Anm's user avatar
  • 203
11 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. ...
ATaco's user avatar
  • 11k
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 ...
ATaco's user avatar
  • 11k
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 ...
Parcly Taxel's user avatar
  • 3,935
16 votes
15 answers
1k views

Find Index of Rational Number in Calkin-Wilf Sequence

Related From Wikipedia: In number theory, the Calkin–Wilf tree is a tree in which the vertices correspond one-to-one to the positive rational numbers. The tree is rooted at the number \$1\$, and any ...
97.100.97.109's user avatar
11 votes
5 answers
612 views

Whole Number Groups

Given a list of fractions, group them so that each group sums to a whole number. This should be done in such a way to maximize the number of non-empty groups. You may assume a solution exists. Order ...
mousetail's user avatar
  • 13k
19 votes
10 answers
2k views

Enumerate the rationals

The cardinality of the set \$\mathbb Q\$ of rational numbers is known to be exactly the same as that of the set \$\mathbb Z\$ of integers. This means that it is possible to construct a bijection ...
att's user avatar
  • 21.3k
0 votes
8 answers
415 views

Print ascending proper fractions using integers up to the given input

User inputs an integer. Print out proper fractions using all positive integers up to the user's input, in ascending order. Rule 1: Eliminate equal fractions. Rule 2: Fractions should be in their ...
paki eng's user avatar
  • 175
19 votes
20 answers
3k views

Next digit of rational number

Story: The π was recently computed with accuracy to 100 trillions digits, but it is useless to us. We can't do accurate enough math, because rational numbers are too boring and so we don't know that ...
Jiří's user avatar
  • 1,885
18 votes
12 answers
1k views

In between fractions

Given two positive integer fractions \$x\$ and \$y\$ such that \$x < y\$, give the fraction \$z\$ with the smallest positive integer denominator such that it is between \$x\$ and \$y\$. For example ...
Wheat Wizard's user avatar
  • 99.6k
1 vote
1 answer
532 views

Best performance on x/(y+z) + y/(x+z) + z/(x+y) = N

Consider the equation $$\frac x {y+z} + \frac y {x+z} + \frac z {x+y} = n$$ for positive integers \$x, y, z\$ and \$n \ge 4\$. Your code will receive \$n\$ as an input, and output three integers \$x, ...
Number Basher's user avatar
12 votes
17 answers
2k views

Infinite Candle Sequence

I have a cake shop that specialises in birthday cakes. The cakes that I sell must have candles placed in a circle. You would probably think I can just divide 360° by the number of candles, but the ...
Matthew Jensen's user avatar
8 votes
7 answers
706 views

Factorials of primes decomposition

You have to decompose a positive integer/fraction as a product of powers of factorials of prime numbers. For example ...
DialFrost's user avatar
  • 5,097
37 votes
20 answers
3k views

Egyptian fraction representations of 1

An Egyptian fraction is a representation of a rational number using the sum of distinct unit fractions (a unit fraction is of the form \$ \frac 1 x \$ where \$ x \$ is a positive integer). For all[1] ...
pxeger's user avatar
  • 24.3k
31 votes
17 answers
2k views

Iterate your way to a fraction

I recently learned from a comment by MathOverflow user pregunton that it is possible to enumerate all rational numbers using iterated maps of the form \$f(x) = x+1\$ or \$\displaystyle g(x) = -\frac ...
Peter Kagey's user avatar
  • 8,821
16 votes
21 answers
2k views

Harmonic divisor numbers

Consider the \$4\$ divisors of \$6\$: \$1, 2, 3, 6\$. We can calculate the harmonic mean of these numbers as $$\frac 4 {\frac 1 1 + \frac 1 2 + \frac 1 3 + \frac 1 6} = \frac 4 {\frac {12} 6} = \frac ...
caird coinheringaahin g's user avatar
17 votes
17 answers
2k views

Print this sequence I just made up

To get this sequence I just made up, which will subsequently be referred to as TSIJMU, consider the harmonic series: \$ \frac{1}{2} + \frac{1}{3} + \frac{1}{4} ...\$ But what if you only add a term if ...
emanresu A's user avatar
19 votes
20 answers
1k views

Exact generalised harmonic numbers

The generalised harmonic number of order \$m\$ of \$n\$ is $$H_{n,m} = \sum_{k=1}^n \frac 1 {k^m}$$ For example, the harmonic numbers are \$H_{n,1}\$, and \$H_{\infty,2} = \frac {\pi^2} 6\$. These are ...
caird coinheringaahin g's user avatar
15 votes
17 answers
2k views

Wolstenholme numbers

The generalised harmonic number of order \$m\$ of \$n\$ is $$H_{n,m} = \sum^n_{k=1} \frac 1 {k^m}$$ In this challenge, we'll be considering the generalised harmonic numbers of order \$2\$: $$H_{n,2} = ...
caird coinheringaahin g's user avatar
15 votes
21 answers
2k views

Do I need a win streak?

You have played \$N\$ matches in some game where each match can only result in one of the two outcomes: win or loss. Currently, you have \$W\$ wins. You want to have a win percentage of \$P\$ or more, ...
Manish Kundu's user avatar
  • 5,290
10 votes
2 answers
433 views

Quote a rational number

Quote notation[1] is a way of expressing rational integers in a precise, finite manner, based on the concept of \$p\$-adic numbers. The notation is in the form of a string of digits (\$0123456789\$) ...
caird coinheringaahin g's user avatar
13 votes
9 answers
819 views

Truncate continued fractions

Related: Cleaning up decimal numbers Background A continued fraction is a way to represent a real number as a sequence of integers in the following sense: $$ x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \...
Bubbler's user avatar
  • 78.2k
23 votes
19 answers
2k views

Quoted rational numbers

Quote notation is a way of expressing rational numbers based on the concept of \$p\$-adic numbers, written in the form \$x'y\$. The quote indicates that the number to it's left (\$x\$) is "...
caird coinheringaahin g's user avatar
8 votes
15 answers
751 views

Equalizing fractions

When I was in grade 3, we were taught how to solve a very simple math problem. It was equaling the denominators of two or more fractions. Let's take two proper fractions:- $$ \frac{1}{2},\frac{2}{3} $$...
Wasif's user avatar
  • 12.4k
13 votes
11 answers
2k views

Calculate the probability of getting to the target first (exactly)

Consider the following probability puzzle. We start with a string of bits all set to 0. At each step we choose a bit uniformly and independently at random and flip it. The value your code has to ...
user avatar
26 votes
19 answers
2k views

Convert a decimal to a fraction, approximately

Take the decimal number \$0.70710678\$. As a fraction, it'd be \$\frac{70710678}{100000000}\$, which simplifies to \$\frac{35355339}{50000000}\$. If you were to make the denominator \$1\$, the closest ...
rydwolf's user avatar
  • 18.9k
16 votes
15 answers
663 views

Repetend length in 1/n

This problem is based on non-terminating, repeating decimal points. Let \$n\$ be any positive integer \$(n > 1 \text{ and } n < 10000)\$, say \$7\$. Then, \$1/n = 1/7 = 0.142857142857142857...\$ ...
vrintle's user avatar
  • 2,990
14 votes
13 answers
2k views

Diophantine Approximation: find lowest possible denominator to approximate within given precision

Challenge Given a number x and a precision e, find the lowest positive integer q such that <...
Stef's user avatar
  • 917
16 votes
5 answers
705 views

Cantor Function, Cruel

A ripoff of this challenge. Go upvote it! Objective Given a rational number amongst \$[0,1]\$, apply the Cantor function to it and output the rational number that's produced. The Cantor function The ...
Dannyu NDos's user avatar
  • 6,299
17 votes
13 answers
5k views

Parse a Unicode vulgar fraction

Objective Given a string with single Unicode vulgar fraction, parse it to a rational number. Valid inputs A valid input is one of: ¼ U+00BC; one quarter ...
Dannyu NDos's user avatar
  • 6,299
3 votes
5 answers
214 views

Find the binary period [duplicate]

We know that not all fractions have a terminating binary representation. However every fraction can be written as a leading portion followed by a repeating portion. For example \$1/3\$ starts with \$...
Wheat Wizard's user avatar
  • 99.6k
39 votes
13 answers
3k views

Phony fractions

Context If a0 and b0 are two decimal numbers, with a and ...
RGS's user avatar
  • 14.1k
12 votes
9 answers
1k views

Integer Logarithm

Objective Take \$a \in ℤ_{>1}\$ and \$b \in ℤ_+\$ as inputs. Write a function \$f\$ such that: $$ f(a,b) = \left\{ \begin{array}{ll} \log_ab & \quad \text{if} \space \log_ab \in ℚ \\...
Dannyu NDos's user avatar
  • 6,299
29 votes
11 answers
4k views

Rational Number RNG

The Objective Build a random number generator whose range is \$\left[0,1\right) \cap \mathbb{Q} .\$ This is, build a random number generator that can produce any value that's: at least \$0 \,;\$ ...
Dannyu NDos's user avatar
  • 6,299
11 votes
8 answers
2k views

Approximate the perfect fifth

Starting at 1-TET, give equal temperaments that have better and better approximation of the perfect fifth(just ratio 3/2). (OEIS sequence A060528) The formal description of the sequence, copied from ...
Dannyu NDos's user avatar
  • 6,299
21 votes
71 answers
10k views

Round towards zero

This is a simple task. Given a positive or negative real number, round it to the next whole integer closer to zero. The challenge Take input through any reasonable form (stdin, function, etc.) of one ...
moltarze's user avatar
  • 2,570
6 votes
2 answers
606 views

Surreal Numbers

Surreal Numbers are one way of describing numbers using sets. In this challenge you will determine the value of a surreal number. Intro A surreal number consists of two sets: a left and right. The ...
Quintec's user avatar
  • 2,869
16 votes
7 answers
723 views

Satisfying Rounding

Satisfying Rounding You know when you're in science class, and asked to round to 2 sig figs, but your answer is 5.2501...? You should round to ...
Quintec's user avatar
  • 2,869
13 votes
7 answers
2k views

Ryley's Theorem

S. Ryley proved following theorem in 1825: Every rational number can be expressed as a sum of three rational cubes. Challenge Given some rational number \$r \in \mathbb Q \$ find three rational ...
flawr's user avatar
  • 43.9k
36 votes
46 answers
5k views

Half, Half Half, and, Half

Consider the following number sequence: \$ 0, \frac{1}{2}, \frac{1}{4}, \frac{3}{4}, \frac{1}{8}, \frac{3}{8}, \frac{5}{8}, \frac{7}{8}, \frac{1}{16}, \frac{3}{16}, \frac{5}{16}, \frac{7}{16}, \...
tsh's user avatar
  • 35k
17 votes
8 answers
2k views

Convert a percentage to a "simple" ratio

You run a political website, and have determined that people have a better intuitive understanding when the chance of winning or losing an election is expressed as a ratio ("5 in 7") than when it is ...
BradC's user avatar
  • 6,797
15 votes
25 answers
2k views

Mixed Fraction Equality

In elementary school, children learn about proper fractions, where the numerator is less than the denominator, and thus the value of the fraction is less than one. Later, they are taught about ...
Stephen's user avatar
  • 14k
15 votes
17 answers
2k views

Exact Partial Sum of Harmonic Series

Challenge Given a positive integer N, output the sum of the first N reciprocals as an exact fraction, which is represented as a ...
pizzapants184's user avatar
14 votes
9 answers
892 views

Number the positive rationals

The positive rational numbers can be shown to be numerable with the following process: Zero has the ordinal 0 Arrange the other numbers in a grid so that row a, column b contains a/b Plot a diagonal ...
HAEM's user avatar
  • 671