Advertisement
Guest User

Multiply Big Number v4

a guest
Nov 25th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: RazielVonChronos
  5.  * Date: 25-Nov-18
  6.  * Time: 13:34 PM
  7.  */
  8.  
  9. $number = strval(readline());
  10. $n = intval(readline());
  11. // we are doing the match backwards so we need to take the index of the value and multiply it for each step.
  12. $index = 1;
  13. // put them in array so we can observe each record's behaviour
  14. $array = [];
  15.  
  16. for ($i = (strlen($number) - 1); $i >= 0; $i--) {
  17.     $array[] = sprintf('%.0f', ($number[$i] * $n) * $index);
  18.     // $i's position defines how big the number(via $index) example 1, 10,100,100 and etc..
  19.     $index = $index * 10;
  20. };
  21.  
  22. //var_dump($array);
  23. // calling sprintf in order to print the big number normally.
  24. // using doubleval for more memory (big numbers sux)
  25. echo sprintf("%.0f", array_sum(array_map('doubleval', $array)));
  26.  
  27. /*
  28. при вход:
  29. 923847238931983192462832102
  30. 4
  31. изхода се очаква да е
  32. 3695388955727932769851328408
  33. но ми връща
  34. 3695388955727932301651214336
  35. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement