-
Notifications
You must be signed in to change notification settings - Fork 82
/
IntToRoman.php
51 lines (44 loc) · 966 Bytes
/
IntToRoman.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Title: Integer to Roman Numerals
* Date: 29/08/2019
*
* Write a function to convert an integer into its roman numeral representation
*
* Execution: php php\IntToRoman.php
*
* For more details, check out http://www.byte-by-byte.com/inttoroman/
*/
<?php
class IntToRoman
{
public static function integerToRoman(int $integer)
{
$result = '';
$lookup = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1,
);
foreach($lookup as $roman => $value)
{
$matches = intval($integer/$value);
$result .= str_repeat($roman, $matches);
$integer = $integer % $value;
}
return $result;
}
}
echo IntToRoman::integerToRoman(1)."\n";
echo IntToRoman::integerToRoman(4)."\n";
echo IntToRoman::integerToRoman(49)."\n";
?>