# 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.

33 questions
Filter by
Sorted by
Tagged with
1k views

### Polynomial Interpolation

Write a program that performs Polynomial Interpolation using true arbitrary precision rational numbers. The input looks like this: f(1) = 2/3 f(2) = 4/5 f(3) = 6/7 ... You may assume that there's ...
2k views

### Closest fraction

Task: Your program is given a proper, positive simple fraction in the format <numerator>/<denominator>. For this input, it must find two fractions. A ...
3k views

### Convert a decimal to a fraction

Write a program that converts a decimal to a mixed, simplified fraction. Sample input: 5.2 Sample output: 5 + (1/5) The ...
884 views

### Find the simplest value between two values

Goal Your goal is to find the simplest value in an open interval. In other words, given two values a,b with a<b, output the ...
2k views

### World Big Dosa​

You are a talented young chef who has just been offered the position of sous chef at the world's most prestigious Indian restaurant. You have little experience with preparing Indian cuisine, but you'...
6k views

### The Bowlers-Golfers Fraction War

Introduction Let's start by arranging all fractions from 0 to 1 in order of lowest denominator, then lowest numerator: 1/2, 1/3, 2/3, 1/4, 3/4, 1/5, 2/5, 3/5, 4/5, 1/6, 5/6, 1/7... Note that ...
3k views

### Determining the continued fractions of square roots

The continued fraction of a number $n$ is a fraction of the following form: $$a_0 + \cfrac 1 {a_1 + \cfrac 1 {a_2 + \cfrac 1 {a_3 + \cfrac 1 {a_4 + \ddots}}}}$$ which converges to $n$. The ...
698 views

### Rational Counting Function

Create a function that takes a natural number (starting from 0 inclusive), and returns a pair of positive integers, which are the numerator and denominator respectively. Use the diagonal traversal. ...
1k views

### Fraction to exact decimal

Write a program or function that given two integers a, b outputs a string containing a decimal number representing the fraction a/b exactly. If a/b is integer, simply output the value, without a ...
2k views

### Find a fraction's position in the Stern-Brocot tree

The Stern-Brocot tree is a binary tree of fractions where each fraction is acquired by adding the numerators and denominators of the two fractions neighbouring it in the levels above. It is generated ...
1k views

### How NOT to reduce fractions

Reducing fractions the wrong way In this code-golf challenge you have to find fractions that can be reduced the wrong way but still end up in the same number. Note: reducing fractions the wrong way ...
3k views

### Simplify a fraction

Winner: Ian D. Scott's answer, by one byte (48 bytes)! Superb! Your program must accept input from a fraction that can be simplified, then simplify it. Rules: If the fraction is already in its most ...
2k views

### Is it within the Cantor set?

The Challenge For this challenge, you are supposed to determine if a given number is in the Cantor set. So first, let's define the Cantor set. First, start with the numbers between 0 and 1. Any ...
4k views

### Convert a repeated decimal to a fraction

This question doesn't need to apply to just terminating decimals - repeating decimals can also be converted to fractions via an algorithm. Your task is to make a program that takes a repeated decimal ...
3k views

### Output a list of all rational numbers

Out of all of mathematics, there will always be a few theorems that go beyond all common sense. One of these is the fact that there are different sizes of infinity. Another interesting fact is the ...
1k views

### Calculating Resistance (Nerd Sniping)

Good Afternoon Golfers, Our challenge for today is inspired by XKCD comics 356 and 370. We're going to write a program to calculate the resistance of a group of resistors. A forewarning that this is ...
2k views

### Output the nth rational number according to the Stern-Brocot sequence

The Stern-Brocot sequence is a Fibonnaci-like sequence which can be constructed as follows: Initialise the sequence with s(1) = s(2) = 1 Set counter ...
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 "...
5k 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 ...
574 views

### Can a fraction be simplified using Anomalous Cancellation?

Anomalous Cancellation (from Wolfram Alpha): Anomalous cancellation is a "canceling" of digits of a and b in the numerator and denominator of a fraction a/b which results in a fraction equal to the ...
2k views

### Good rational approximations of pi

Write a program that prints out all the good rational approximations of pi with denominator < 1000000, in increasing denominator order. a/b is a "good rational ...
1k views

### LCM of Rational Numbers

The least common multiple (LCM) of a set of numbers A is the smallest integer b such that b/a...
825 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 ...
2k views

### In-between fractions

In-between fractions The challenge: You will need to create code that takes atleast 3 inputs; 2 integers and "a fraction representation" - whichever type suits your language for representing the ...
551 views

### Calculate the p-adic norm of a rational number

Calculate the p-adic norm of a rational number Write a function or a program, that takes 3 integers m,n,p (where p is a ...
5k views

1k 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 ...