Advertisement
ShadowEmbrace

Multiply Big Number

Nov 25th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2.  
  3. $num1 = readline();
  4. $num2 = intval(readline());
  5.  
  6. if ($num1 === '0' || $num2 === 0) {
  7.     echo '0';
  8.     return;
  9. }
  10.  
  11. $digits = [];
  12.  
  13. for ($i = 0; $i < strlen($num1); $i++) {
  14.     $digits[] = intval($num1[$i]);
  15. }
  16.  
  17. $remainder = 0;
  18. $result = $digits;
  19.  
  20. for ($j = 0; $j < $num2 - 1; $j++) {
  21.     for ($k = count($digits) - 1; $k >= 0; $k--) {
  22.         $sum = $result[$k] + $digits[$k] + $remainder;
  23.  
  24.         if ($sum > 9 && $k !== 0) {
  25.             $result[$k] = $sum % 10;
  26.             $remainder = 1;
  27.         } else {
  28.             $result[$k] = $sum;
  29.             $remainder = 0;
  30.         }
  31.     }
  32. }
  33.  
  34. echo implode('', $result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement