math.gcd, lcm And prod On Integers
math.gcd reduces ratios, math.lcm finds the shared cadence of periodic jobs, and math.prod multiplies a list without an explicit reduce loop.
Code
import math
# gcd:把 1920x1080 化简成最简整数比
w, h = 1920, 1080
g = math.gcd(w, h)
print("aspect ratio:", w // g, ":", h // g)
# lcm:每 30 与每 45 分钟各跑一次的作业,共同的下次同步间隔
print("combined cadence:", math.lcm(30, 45), "minutes")
# prod:三组变体数的笛卡尔组合总数
variants = [3, 4, 5]
print("total variants:", math.prod(variants))Output
Captured from a sandboxed run of this snippet in the build pipeline.
aspect ratio: 16 : 9
combined cadence: 90 minutes
total variants: 60Inside the snippet
Top-level bindings variants hold the demo state — nothing sits inside a function scope.
Putting it to use
The demo output is labeled (aspect ratio:, combined cadence:, total variants:), so every printed line maps back to its originating operation.
Interactive Tools for This Topic
GCD & LCM CalculatorFind the greatest common divisor and least common multiple of two or more integers, computed exactly with BigInt.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Vector Similarity CalculatorCompute Cosine Similarity, Dot Product, Euclidean L2, Manhattan L1, and Chebyshev distances between numeric vectors with dimension checks.
FAQ
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. Its state lives in module-level bindings: `variants`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.