Created
February 14, 2020 13:49
-
-
Save zavodnoyapl1992/6c5a896e7f611cbac22c47308c9b0cdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sumStrings($a, $b) { | |
$intMaxLength = strlen((string)PHP_INT_MAX) - 2; | |
$aArr = str_split(strrev($a), ($intMaxLength)); | |
$bArr = str_split(strrev($b), $intMaxLength); | |
$add = 0; | |
$res = []; | |
$aArrLength = (count($aArr)); | |
foreach ($aArr as $k => $block) { | |
$i = (int)(strrev($block)) + (int)strrev($bArr[$k] ?? '0') + $add; | |
$add = 0; | |
$i = (string)$i; | |
if (strlen($i) > strlen($block) && $k !== $aArrLength - 1) { | |
$add = 1; | |
$i = substr($i, 1); | |
} elseif (strlen($i) < strlen($block)) { | |
$i = str_repeat('0', strlen($block) - strlen($i)) . $i; | |
} | |
$res[] = $i; | |
} | |
return join(array_reverse($res)); | |
} | |
$a = '99999'; | |
$b = '99999'; | |
if (strlen($a) > strlen($b)) { | |
$c = sumStrings($a, $b); | |
} else { | |
$c = sumStrings($b, $a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment