Skip to content

Instantly share code, notes, and snippets.

@qti3e
Created January 23, 2016 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qti3e/346d00a94e589f1acc9e to your computer and use it in GitHub Desktop.
Save qti3e/346d00a94e589f1acc9e to your computer and use it in GitHub Desktop.
<?php
/*****************************************************************************
* In the name of God the Most Beneficent the Most Merciful *
*___________________________________________________________________________*
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
*___________________________________________________________________________*
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
*___________________________________________________________________________*
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*___________________________________________________________________________*
* Created by AliReza Ghadimi *
* <http://AliRezaGhadimi.ir> LO-VE <AliRezaGhadimy@Gmail.com> *
*****************************************************************************/
/**
* @param $a
* @param $b
* @return string
*/
function big_sum($a,$b){
$a = (string)$a;
$b = (string)$b;
$big = strlen($a) > strlen($b) ? strlen($a) : strlen($b);
$a = str_split(str_pad($a,$big,"0",STR_PAD_LEFT),5);
$b = str_split(str_pad($b,$big,"0",STR_PAD_LEFT),5);
$return = "";
$remind = 0;
$count = count($a)-1;
for($i = $count;$i >= 0;$i--){
$f = strlen($a[$i]);
$s = (string)((int)$a[$i]+(int)$b[$i]+$remind);
$sl = strlen($s);
if($sl > $f){
$p = $sl-$f;
$remind = (int)(substr($s,0,$p));
$s = (string)(substr($s,$p,$sl));
}else{
$remind = 0;
}
$return = $s.$return;
}
$_return = $remind !== 0 ? (string)$remind : "";
return $_return.$return;
}
/**
* @param $a
* @param $b
* @return int|string
*/
function big_multiplication($a,$b){
$a = (string)$a;
$b = (string)$b;
$big = strlen($a) > strlen($b) ? $a : $b;
$small = strlen($b) >= strlen($a) ? $a : $b;
$f = 1;
$small = str_split($small,$f);
$big = str_split($big,$f);
$count = count($small)-1;
$count_b = count($big)-1;
$remind = 0;
$return = 0;
for($i = $count;$i >= 0;$i--){
$p = "";
$iS = (int)$small[$i];
for($j = $count_b;$j >= 0;$j--){
$iB = (int)$big[$j];
$s = (string)(($iS*$iB)+$remind);
$sl = strlen($s);
if($sl > $f){
$l = $sl-$f;
$remind = (int)(substr($s,0,$l));
$s = (string)(substr($s,$l,$sl));
}else{
$remind = 0;
}
$p = ((string)$s).$p;
}
$return = big_sum($return,($remind !== 0 ? (string)$remind : "").$p.str_repeat("0",$count-$i));
$remind = 0;
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment