File: smallest-even-multiple/solution.py

Date: 2026-06-06

Time: 19:08

smallest-even-multiple/solution.py

Purpose

This file solves LeetCode 2413: Smallest Even Multiple. Given a positive integer n, it returns the smallest positive integer that is a multiple of both 2 and n — i.e., the LCM of n and 2.

It's a leaf module in the repo: no imports, no dependencies. Its only consumer is smallest-even-multiple/test_solution.py.

Key Components

smallest_multiple(n: int) -> int — the sole public function.

Patterns

Direct mathematical insight over general-purpose algorithms. The problem is technically "find LCM(n, 2)", but since 2 is prime, the solution collapses to a parity check. No math.gcd import needed — the entire solution is a single ternary expression.

Defensive input validation. The function checks type (isinstance) and range bounds before doing any work, which is a pattern used across this repo's solutions.

Docstring with examples. The docstring includes >>> examples that double as informal documentation, though they aren't wired into a doctest runner.

Dependencies

Flow

1. Validate n is an int in [1, 150]. Raise ValueError if not.

2. Check parity: even → return n; odd → return n * 2.

No loops, no recursion, no branching beyond the single ternary. O(1) time and space.

Invariants

Error Handling

A single ValueError with a descriptive f-string message is raised for any input outside the valid domain. No try/except, no silent fallbacks. The caller is expected to pass valid input; violations are loud.