-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerConverter.php
260 lines (216 loc) · 7.1 KB
/
IntegerConverter.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace Labroots\Utility;
/**
*
* Austin Hammer (June 2015)
* Converts odd integers to strings
* and returns the sum of the common factors of even numbers.
*
*
* Input must be integers, or decimals.
* Even number's sum will ignore fractions/decimals.
* Negative numbers also be ignored for even numbers
*
* Uses a modified Sieve of Erastonthenes algorithm
* NOTE: REQUIRES PHP 5.4
*
*/
class IntegerConverter
{
static $dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
protected $evenInts = [];
protected $oddInts = [];
protected $integers = [];
protected $translatedOdd = [];
protected $translatedEven = [];
public function __construct(array $integers)
{
// Make sure values are digits only (can be string digits and floats)
$sanitizedInts = array_filter($integers, 'is_numeric');
$smallest = min($sanitizedInts);
$largest = max($sanitizedInts);
if (abs($smallest) > 1000000000000000000 || abs($smallest) > 1000000000000000000) {
throw new \Exception("Can only process numbers less than 1000000000000000000!");
}
if (abs($largest) > 1000000000000000000 || abs($largest) > 1000000000000000000) {
throw new \Exception("Can only process numbers less than 1000000000000000000!");
}
$this->integers = $sanitizedInts;
$this->execute();
}
public function execute()
{
// first split them up (odd and even)
$this->splitOddAndEven();
$this->processOdd();
$this->processEven();
}
public function displayAsHTML() {
echo "<b>Numbers Inputted:</b> ".implode(", ", $this->integers)."<br />".PHP_EOL;
echo "<b>Odd Numbers:</b> <br />";
echo nl2br(print_r($this->translatedOdd, true)).PHP_EOL;
echo "<p></p><b>Even Number Sum:</b> ".$this->translatedEven.PHP_EOL;
}
public function getValues()
{
return
[
'odd_strings' => $this->translatedOdd,
'even_sum' => $this->translatedEven
];
}
protected function splitOddAndEven()
{
foreach ($this->integers as $int) {
if ($int & 1) {
// odd integers
$this->oddInts[] = floatval($int);
} else {
// @NOTICE: even integers that are not WHOLE NUMBERS will be discarded
if(!is_int($int)) {
continue;
}
$this->evenInts[] = intval($int);
}
}
}
protected function translateIntToWord($integer)
{
switch (true) {
// deal with negative numbers
case $integer < 0:
$string = 'negative ' . $this->translateIntToWord(abs($integer));
return $string;
break;
// smaller numbers are easy
case $integer < 21:
$string = self::$dictionary[$integer];
break;
// hundreds get a little more complicated
case $integer < 100:
$tens = ((int)($integer / 10)) * 10;
$units = $integer % 10;
$string = self::$dictionary[$tens];
if ($units) {
$string .= '-' . self::$dictionary[$units];
}
break;
//
case $integer < 1000:
$hundreds = $integer / 100;
$remainder = $integer % 100;
$string = self::$dictionary[$hundreds] . ' ' . self::$dictionary[100];
if ($remainder) {
$string .= ' and ' . $this->translateIntToWord($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($integer, 1000)));
$numBaseUnits = (int)($integer / $baseUnit);
$remainder = $integer % $baseUnit;
$string = $this->translateIntToWord($numBaseUnits) . ' ' . self::$dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? ' and ' : ', ';
$string .= $this->translateIntToWord($remainder);
}
break;
}
// DEAL WITH DECIMALS/FRACTIONS
if ($this->hasDecimal($integer)) {
list($integer, $decimals) = explode('.', $integer);
$decimals = strval($decimals);
$integer = number_format($integer, 2, '.', ' '); // Truncate if more than 3 decimals
// if zero
if ($decimals == 0) {
$decimals = self::$dictionary[0];
} // ones decimals
elseif ($decimals > 0 && $decimals < 21) {
$decimals = self::$dictionary[$decimals];
} // tens decimals
else {
$tens = (intval($decimals / 10)) * 10;
$units = $decimals % 10;
if ($tens > 0) {
$decimals = '' . self::$dictionary[$tens];
}
if ($units) {
$decimals .= '-' . self::$dictionary[$units];
}
}
$string .= ' point ' . $decimals;
}
return $string;
}
private function hasDecimal($val)
{
return ((float)$val !== floor($val));
}
private function processOdd()
{
// do odd numbers
foreach ($this->oddInts as $int) {
$this->translatedOdd[strval($int)] = $this->translateIntToWord($int);
}
}
private function processEven()
{
$allFactors = [];
// find all even common divsors(factors)
foreach ($this->evenInts as $int) {
if($int > 0) { $allFactors[] = $this->calcFactors($int); }
}
// remove unique values (only get common ones)
$commonFactors = call_user_func_array('array_intersect', $allFactors);
$this->translatedEven = array_sum($commonFactors);
}
// creates a simple Sieve_of_Eratosthenes
function calcFactors($integer)
{
$limit = abs($integer);
$factors = [];
for ($i = 1; $i <= $limit; $i++)
{
if(0 === $limit % $i) {
$factors[] = $i;
}
}
return $factors;
}
}
?>