Roman Numeral Conversion
Convert between Roman numerals (I, V, X, L, C, D, M) and Arabic numbers. Ancient Roman number system using additive and subtractive notation. Range: 1-3999.
Visualization
Interactive visualization for Roman Numeral Conversion
Interactive visualization with step-by-step execution
Implementation
1class RomanConverter {
2 private values = [
3 { value: 1000, symbol: 'M' },
4 { value: 900, symbol: 'CM' },
5 { value: 500, symbol: 'D' },
6 { value: 400, symbol: 'CD' },
7 { value: 100, symbol: 'C' },
8 { value: 90, symbol: 'XC' },
9 { value: 50, symbol: 'L' },
10 { value: 40, symbol: 'XL' },
11 { value: 10, symbol: 'X' },
12 { value: 9, symbol: 'IX' },
13 { value: 5, symbol: 'V' },
14 { value: 4, symbol: 'IV' },
15 { value: 1, symbol: 'I' }
16 ];
17
18 private symbolValues: { [key: string]: number } = {
19 'I': 1, 'V': 5, 'X': 10, 'L': 50,
20 'C': 100, 'D': 500, 'M': 1000
21 };
22
23 toRoman(num: number): string {
24 if (num < 1 || num > 3999) {
25 throw new Error('Number must be between 1 and 3999');
26 }
27
28 let roman = '';
29 for (const { value, symbol } of this.values) {
30 while (num >= value) {
31 roman += symbol;
32 num -= value;
33 }
34 }
35 return roman;
36 }
37
38 fromRoman(roman: string): number {
39 roman = roman.toUpperCase();
40 let arabic = 0;
41
42 for (let i = 0; i < roman.length; i++) {
43 const current = this.symbolValues[roman[i]];
44 const next = this.symbolValues[roman[i + 1]];
45
46 if (next && current < next) {
47 arabic -= current; // Subtractive notation
48 } else {
49 arabic += current;
50 }
51 }
52
53 return arabic;
54 }
55
56 isValidRoman(roman: string): boolean {
57 const pattern = /^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/;
58 return pattern.test(roman.toUpperCase());
59 }
60}Deep Dive
Theoretical Foundation
Seven symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Additive: VI=6. Subtractive: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900. Larger values left, smaller right. Subtractive only for 4,9,40,90,400,900.
Complexity
Time
O(1)
O(1)
O(1)
Space
O(1)
Applications
Industry Use
Clock faces and timepieces
Book chapters and sections
Movie sequel numbering
Historical date representation
Formal document numbering
Architectural inscriptions
Copyright years in media
Outline and list formatting
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.