Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

27
votes
35answers
4k 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}, \...
15
votes
8answers
1k 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 ...
15
votes
25answers
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 ...
14
votes
14answers
1k 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 ...
14
votes
9answers
747 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 ...
21
votes
20answers
4k views

I want my book to be away from this table

Story So I have a book that I want to separate from my table with nothing but other books. I want to know how many books do I need to achieve this with \$n\$ book lengths. Here's a visualization ...
17
votes
6answers
695 views

Delete the first periodic digit

We all know that whenever a rational number is written in decimal, the result is either terminating or (eventually) periodic. For example, when 41/42 is written in decimal, the result is ...
19
votes
2answers
348 views

Bitwise XOR of rational numbers

Introduction Every rational number between 0 and 1 can be represented as an eventually periodic sequence of bits. For example, the binary representation of 11/40 is ...
19
votes
3answers
456 views

Writing rational numbers as ratio of factorials of primes

Note: this challenge has been posted on the sandbox. Introduction This challenge is inspired by 2009 Putnam B1, a problem in an undergraduate mathematics competition. The problem is as follows: ...
12
votes
14answers
479 views

Repeated reciprocal

What you need to do is create a function/program that takes a decimal as input, and outputs the result of repeatedly taking the reciprocal of the fractional part of the number, until the number ...
17
votes
12answers
982 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...
14
votes
25answers
1k views

Adding fractions

Write a program or a function that takes two non-empty lists of the same length as input and does the following: uses elements of first list to get numerators, uses elements of the second list to get ...
27
votes
44answers
4k views

Infinitely Print Zeno's Dichotomy Paradox (1/(2^n))

Wikipedia: Zeno's Dichotomy Paradox An infinite number of mathematicians walk into a bar. The first one orders a beer. The second one orders half a beer. The third one orders a fourth of a beer. ...
21
votes
2answers
371 views

Rational decomposition a = xyz(x+y+z)

Write functions x(a), y(a) and z(a) such that for any rational ...
7
votes
4answers
305 views

The Imperial Ruler

Given a ruler length in inches and fractions of an inch, and a granularity in fractions of an inch, output the values of all marks on the ruler, starting from 0, in ascending order. Input ruler ...
8
votes
1answer
482 views

Fraction Frenzy!

EDIT: I'm getting a lot of comments about this not terminating - I will give the "correct answer" tag to either the first person who gives me FF(3) (as in provides ...
21
votes
9answers
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 ...
21
votes
2answers
465 views

Swap out some periodic and non-periodic parts

In the decimal representation of every rational number p/q, you have a periodic tail, a non-periodic head, and a section before the decimal point in the following ...
15
votes
3answers
250 views

Golfed decimal fractions

Your goal is to write some code that will output the shortest unique decimal sequence for the input fraction. No two fractions with the same denominator may have the same output, although it's ...
26
votes
12answers
609 views

The nth numerator

You can create a list of all rationals 0 < r ≤ 1 by listing them ordered first by denominator and then by numerator: ...
18
votes
19answers
2k views

Simplify a continued fraction

Continued fractions are expressions that describe fractions iteratively. They can be represented graphically: Or they can be represented as a list of values: ...
29
votes
22answers
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 ...
22
votes
6answers
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 ...
4
votes
7answers
380 views

Continued Fraction of a Rational Number

The continued fraction of a number n is a fraction of the following form: which converges to n. The sequence ...
6
votes
7answers
582 views

Calculate the Gamma function for half-integer arguments

Gamma function is defined as It is a well-known fact that for positive integers it coincides with a properly shifted factorial function: Γ(n) = (n - 1)!. However, ...
13
votes
12answers
1k 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 ...
2
votes
3answers
402 views

Solve a quadratic equation using Vieta's formulas

Exposition Your mathematics teacher is a big fan of Vieta's formulas, and he believes that you should use them to solve quadratic equations. Given the equation ...
11
votes
6answers
419 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 ...
19
votes
31answers
982 views

Mixed Number to an Improper Fraction

Mixed Number to an Improper Fraction In this challenge you will be converting a mixed number to an improper fraction. Because improper fractions use fewer numbers, your code will need to be as short ...
16
votes
1answer
406 views

When Bullets Collide

This challenge is based off a riddle I read in some book a while ago, which I found again here. It is about bullets fired from a gun once per second at varying speeds that travel in a straight line ...
11
votes
4answers
407 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 ...
17
votes
12answers
1k views

Lookalike shapes

Similar figures Two rectangles are similar if the ratios of their sides are the same. Consider these two rectangles; a rectangle 5 lines tall and 11 chars wide: ...
37
votes
5answers
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'...
9
votes
4answers
1k views

Calculate a probability exactly

This task is about writing code to compute a probability exactly. The output should be a precise probability written as a fraction in its most reduced form. That is it should never output ...
13
votes
1answer
820 views

XKCD Bracket Probabilities

Today's XKCD is a sports-tournament style bracket, where the contestants are well-known names, put into groups of possibly confusing names. Give the probability that a given contestant will win the ...
9
votes
1answer
167 views

Determine whether rational coordinates are in the right Sierpinski triangle

The Sierpinski triangle is a set of points on the plane which is constructed by starting with a single triangle and repeatedly splitting all triangles into four congruent triangles and removing the ...
17
votes
7answers
1k views

Convert a fraction to a repeated decimal

Almost the polar opposite if this challenge, and I suspect it will be slightly easier. Your task is to take two integers in the format a/b (Forming a rational ...
14
votes
6answers
493 views

Out-of-Control Rounding Errors

Background You have recently been hired by a small accounting firm. The world of accounting is somewhat foreign to you, so you're not sure whether you are following all the professional guidelines. ...
13
votes
3answers
2k views

Computer: you do the math

This challenge is partly an algorithms challenge, involves some math and is partly simply a fastest code challenge. For some positive integer n, consider a ...
16
votes
2answers
705 views

A Good Time to Refuse

The Setup Suppose you're given n fuses, with 1 ≤ n ≤ 5, each of which is a meter long, and where each fuse has an associated burn rate of N meters per D hours. A fuse can be lit at one or ...
6
votes
7answers
810 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 ...
22
votes
14answers
2k views

Un-round fractions

When you convert a fraction to a decimal number and you want to store that number, you often have to round it, because you only want to use a certain amount of memory. Let's say you can only store 5 ...
20
votes
9answers
4k 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 ...
10
votes
4answers
899 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 ...
7
votes
18answers
2k 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 ...
24
votes
6answers
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 ...
0
votes
11answers
692 views

Count the unique fractions with only integers

Count the number of unique fractions with numerators and denominators from 1 to 100, and print the counted number. Example: 2/3 = 4/6 = ... Rules: You must actually count in some way. Only integers ...
7
votes
2answers
824 views

Build a program that creates a minimal one-ohm resistor diagram for a given resistance

You are a contractor for a company who designs electrical circuits for the various products they make. One day, the company accidentally gets a huge shipment of one-ohm resistors and absolutely ...
-1
votes
2answers
294 views

Largest Denominator [closed]

The number 1 can be written as sum of fractions of the form 1/p (where p>1). There are different ways to write it. You will be given an integer k which denotes the number of fractions to be used to ...
23
votes
21answers
3k 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 ...