#RationalNumbers

2024-04-19

Enumerating all of the (positive) #RationalNumbers in 10 lines of #Haskell code.

---------------------------------------------
data Rational = Integer :/ Integer

properFraction :: Rational -> (Integer, Rational)
properFraction (a:/ b) = if a < b then (0, (a :/ b)) else (q, (r :/ b))
where (q, r) = a `quotRem` b

calkinwilfSuccessor :: Rational -> Rational
calkinwilfSuccessor rat = b :/ (b*n + b - a) where (n, (a :/ b)) = properFraction rat

rationals :: [Rational]
rationals = iterate calkinwilfSuccessor (1 :/ 1)
---------------------------------------------

This comes from the 2004 functional pearl paper by Gibbons, Lester and Bird. The main component is a formula credited to Moshe Newman for generating the next element in the Calkin-Wilf sequence (the sequence is simply a breadth-first search enumeration of the Calkin-Wilf tree). See also: en.wikipedia.org/wiki/Calkin%E

The Gibbons, Lester, Bird paper contains many other neat pearls involving deforesting trees (Stern-Brocot and Calkin-Wilf trees), the above successor idea, and a continued fraction form which avoids arbitrary-precision integer division (at the cost of a continued fraction representation of the rationals).

Quick note: Generating all rationals using the above is easy. Change to prepend 0, then always produce (𝑥, -𝑥) one after the other with 𝑥 are all positive rationals coming from the above function.

2024-02-24

Neulich hab ich ohne besonderen Grund über die reellen Zahlen gegrübelt und ihr Verhältnis zu den rationalen Zahlen. Warum auch immer, ich habe dann die reelle Achse um einen Kreis mit Radius 1/2 gewickelt und mir ist klar geworden, dass keine zwei rationalen Zahlen an derselben Stelle landen. Ist mathematisch im Grunde trivial, aber ich fand's nett. Wer es nochmal ausführlich haben will:
miamao.de/blog/2024-02/24.Wick
#math #mathematik #pi #rationalnumbers #rationalezahlen

2024-01-24

1/5: "Man, I was trying to reason with pi the other day. Cloud't do it."

1/2: "Oh, you can't reason with pi."

1/5: "How come?"

1/2: "Pi is irrational."

#math #MathJoke #IrrationalNumbers #RationalNumbers #dadjoke #joke

2020-03-11

A lovely post on Dirichlet’s approximation theorem, which allows you to approximate irrational number with rational numbers with small denominators with guarantees on how close the irrational number is the approximation.

quantamagazine.org/how-rationa

#Mathematics #Numbers #RationalNumbers #IrrationalNumbers #NumericalApproximations

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst