Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Euclid Engine

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Replace a pair with divisor and remainder: The Euclid Engine.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when GCD and LCM matches the clues
  2. 2Keep this true after every move: every Euclidean pair has the same GCD as the original inputs
  3. 3Reach the result within O(log min(a,b)) time and O(1) space

Rules and pressure

  • Target cost: O(log min(a,b)) time and O(1) space
  • State rule: every Euclidean pair has the same GCD as the original inputs
Lesson 1 of 3

Live algorithm trace

GCD and LCM

Complete execution
1 of 5

Start from nonnegative magnitudes 48 and 18.

1x = abs(a); y = abs(b)
2while y is not zero
3x, y = y, x mod y
4gcd = x
5lcm = 0 if either input zero else abs(a/gcd*b)
6return gcd and lcm
x = 48y = 18
Truth to preserve / Cost target
Truth to preserve

every Euclidean pair has the same GCD as the original inputs

Cost target

O(log min(a,b)) time and O(1) space

The greatest common divisor is unchanged when (a,b) becomes (b,a mod b). Once the remainder is zero, use gcd times lcm equals the absolute product.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead