std::lcm(3)
π std::lcm(3) β C++ Standard Libary, bestie edition π
okay so this is the std::lcm(3) page, lowkey iconic, and we're about to make least common multiples make sense. No cap. π¦
π NAME π
std::lcm - std::lcm β that's literally the whole name line, bestie. It's giving "the function is called std::lcm and the manual entry is also std::lcm". Periodt. β¨
β¨ Synopsis β¨
Defined in header <numeric> β and here's the signature, copied exactly because we do not play with code:
Defined in header <numeric>
template< class M, class N > (since C++17)
constexpr std::common_type_t<M, N> lcm( M m, N n );
It computes the least common multiple of the integers m and n. That's the main character energy of this function. π
π¨ Type vibe check: if eitherMorNis not an integer type, or if either is (possibly cv-qualified)bool, the program is ill-formed. No cap, that's not a suggestion.
π Undefined behavior alert: the behavior is undefined if|m|,|n|, or the least common multiple of|m|and|n|is not representable as a value of typestd::common_type_t<M, N>. Like, do not test this at home, fr fr.
π¦ Parameters π¦
m,nβ integer values.
πΈ Return value πΈ
If either m or n is zero, returns zero. Otherwise, returns the least common multiple of |m| and |n|.
π Exceptions π
Throws no exceptions. It's giving calm, unbothered, moisturized. β¨
π Notes π
Feature-test macro vibe check:
__cpp_lib_gcd_lcmβ value201606L; std(C++17); featurestd::gcd,std::lcm.
π₯ Example π₯
Run this code, bestie:
// Run this code
#include <iostream>
#include <numeric>
#define OUT(...) std::cout << #__VA_ARGS__ << " = " << __VA_ARGS__ << '\n'
constexpr auto lcm(auto x, auto... xs)
{
return ((x = std::lcm(x, xs)), ...);
}
int main()
{
constexpr int p{2 * 2 * 3};
constexpr int q{2 * 3 * 3};
static_assert(2 * 2 * 3 * 3 == std::lcm(p, q));
static_assert(225 == std::lcm(45, 75));
static_assert(std::lcm( 6, 10) == 30);
static_assert(std::lcm( 6, -10) == 30);
static_assert(std::lcm(-6, -10) == 30);
static_assert(std::lcm( 24, 0) == 0);
static_assert(std::lcm(-24, 0) == 0);
OUT(lcm(2 * 3, 3 * 4, 4 * 5));
OUT(lcm(2 * 3 * 4, 3 * 4 * 5, 4 * 5 * 6));
OUT(lcm(2 * 3 * 4, 3 * 4 * 5, 4 * 5 * 6, 5 * 6 * 7));
}
π Output π
lcm(2 * 3, 3 * 4, 4 * 5) = 60
lcm(2 * 3 * 4, 3 * 4 * 5, 4 * 5 * 6) = 120
lcm(2 * 3 * 4, 3 * 4 * 5, 4 * 5 * 6, 5 * 6 * 7) = 840
β See also β
gcdβ computes the greatest common divisor of two integers;(C++17) (function template).
made with π by your Unix bestie Β· source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::lcm(3) Β· that's the footer, periodt π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms