-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_numbers.php
executable file
·67 lines (56 loc) · 1.89 KB
/
add_numbers.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
#!/usr/bin/php
<?php
/**
* Hey fellows, do not EVER use this shitty algorithm! It's slower than a snail!
* Use ASM! Chuck Norris uses only ASM! There is a Carry Flag CF (0x0001)
* in a flags register in Your x86 CPU. Go ahead, use it! Be a man!
*/
list($a, $b) = get_args($argv);
debug_regular_addition($a, $b);
echo "{$a} + {$b} = " . large_numbers_sum($a, $b) . "\n";
function get_args(array $argv) : array
{
if(!isset($argv[1]) || !isset($argv[2])) {
echo "I want two arguments! Mi piacciono gli argomenti molto!\n";
die();
}
if(!is_numeric($argv[1]) || !is_numeric($argv[2])) {
echo "Numbers, please!\n";
die();
}
return [(int)$argv[1], (int)$argv[2]];
}
function large_numbers_sum(int $a, int $b) : string
{
$rev_a = strrev(strval($a));
$rev_b = strrev(strval($b));
$length = max(strlen($rev_a), strlen($rev_b)) + 1;
$rev_b = str_pad($rev_b, $length, "0");
$rev_a = str_pad($rev_a, $length, "0");
$result = array_fill(0, $length, 0);
$carry_flag = false;
for ($i = 0; $i < $length; $i++)
{
$result[$i] = (int)$rev_b[$i] + (int)$rev_a[$i];
if($carry_flag) {
$result[$i]++;
$carry_flag = false;
}
if($result[$i] > 9) {
$carry_flag = true;
$result[$i] %= 10;
}
}
return (int)implode(array_reverse($result));
}
function debug_regular_addition(int $a, int $b) : void
{
$bitDepth = strlen(decbin(~0));
$result = $a + $b;
echo "REGULAR {$bitDepth}bit ADDITION RESULT: {$result}\n";
if(is_float($result)) {
echo "Yes, now it's a float and the result would be much more interesting in C ;-)\n";
}
echo "\n";
}
?>