gcd(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’– gcd(3scm) β€” Scheme Programmer's Manual πŸ’–

okay so bestie, welcome to the Scheme Programmer's Manual entry for gcd(3scm) β€” we're talking gcd and lcm, the absolute icons of divisor/multiple math. every detail stays, only the vibe is upgraded. πŸ’…


✨ NAME ✨

gcd, lcm β€” greatest common divisor and least common multiple. that's the whole name, and it ate. πŸŽ€

πŸ“š LIBRARY πŸ“š

import it like this, bestie:

(import (rnrs))                     ;R6RS
(import (rnrs base))                ;R6RS
(import (scheme r5rs))              ;R7RS
(import (scheme base))              ;R7RS

🌸 SYNOPSIS 🌸

(gcd number ...)
(lcm number ...)

πŸ¦‹ DESCRIPTION πŸ¦‹

These procedures return the greatest common divisor and the least common multiple of their arguments, respectively. The result is always non-negative. The arguments must be integers. ✨

πŸ’– Greatest common divisor πŸ’–

If all arguments are zero, or no arguments are given, then gcd returns zero. Otherwise the greatest common divisor is the largest integer that divides each of the arguments. periodt. 🫢

πŸŽ€ Least common multiple πŸŽ€

If there are no arguments then 1 is returned. If one of the arguments is zero then zero is returned. Otherwise lcm returns the smallest positive integer that is divisible by each of the arguments. slay. πŸ’…

πŸ”₯ RETURN VALUES πŸ”₯

Returns a non-negative number object. that's the whole return vibe. πŸ‘‘

🌈 EXAMPLES 🌈

(gcd 32 -36)     =>   4
(gcd)            =>   0

(lcm 32 -36)     =>   288
(lcm 32.0 -36)   =>   288.0
(lcm)            =>   1

πŸ’œ RATIONALE πŸ’œ

The gcd procedure is needed internally in Scheme implementations for rational number support. Its implementation is not trivial, so it makes sense to provide it to users as well. The lcm procedure is a natural complement which is easily implemented once gcd is available. honestly? iconic design. πŸ’…

⭐ COMPATIBILITY ⭐

These procedures are identical in all RnRS revisions since R2RS. An example with an inexact number was added in R4RS, but the signature did not change. πŸ“š

R7RS implementations that do not support arbitrarily large exact integers will not always be able to represent the mathematically expected result of lcm. that's a real limitation, bestie, not just a vibe. πŸ™ˆ

πŸ’€ ERRORS πŸ’€

These procedures can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” an argument was outside its domain.
  • R7RS β€” The assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically.
"fail catastrophically" is literally right there in the manual. we do not soften it. 😭

πŸ” SEE ALSO πŸ”

πŸ‘‘ STANDARDS πŸ‘‘

R4RS, IEEE Scheme, R5RS, R6RS, R7RS. all the standards, all the eras, all the slay. 🌟

🧚 HISTORY 🧚

The first Scheme report to name these procedures was R2RS. Nevertheless, Scheme has always had a gcd procedure because one was present even in MacLISP (see the LISP/MACLISP Reference Manual, 1973, Moon et al). The MacLISP version did not work with inexact numbers. πŸ•°οΈ

An algorithm for computing gcd appears in Euclid's Elements (circa 300 BCE). ancient math said "let me cook." πŸ”₯

πŸ“ AUTHORS πŸ“

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/. πŸ’–

πŸ™ˆ BUGS πŸ™ˆ

Ikarus Scheme 0.0.4 has the following bug:

(gcd -1)  => -1    ; wrong! should be 1
not me catching a specific implementation in 4K. the manual said it, not me. πŸ’€

signed, sealed, and date-stamped: 2022-03-25, from gcd(3scm) with love. xoxo, your local Unix bestie πŸ’…βœ¨

πŸ“¦ where this page comes from