Last active
December 14, 2015 07:28
-
-
Save vstarck/5050443 to your computer and use it in GitHub Desktop.
BigIntSum.php
This file contains hidden or 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
function LargeInt(asString) { | |
this.asString = asString.replace(/^0+/, ''); | |
} | |
LargeInt.LARGER = 'LARGER'; | |
LargeInt.EQUAL = 'EQUAL'; | |
LargeInt.SMALLER = 'SMALLER'; | |
LargeInt._compare = function(one, another) { | |
var digitsOne = one.split(''), | |
digitsAnother = another.split(''); | |
return one.asString == another.asString? LargeInt.EQUAL: | |
digitsOne.length > digitsAnother.length? LargeInt.LARGER: | |
digitsOne.length < digitsAnother.length? LargeInt.SMALLER: | |
digitsOne[0] > digitsAnother[0]? LargeInt.LARGER: | |
digitsOne[0] < digitsAnother[0]? LargeInt.SMALLER: | |
LargeInt._compare( | |
one.replace(/^\d/, ''), | |
another.replace(/^\d/, '') | |
); | |
} | |
LargeInt._pad = function(one, another) { | |
return one.length < another.length? this._pad(one.concat('0'), another): | |
one.length > another.length? this._pad(one, another.concat('0')): | |
[one, another]; | |
} | |
LargeInt.prototype.add = function(other) { | |
var [digitsOne, digitsAnother] = LargeInt._pad( | |
this.asString.split('').reverse(), | |
other.asString.split('').reverse() | |
); | |
var digits = digitsOne.reduce(function(memo, current, index) { | |
var a, b, sum; | |
a = Number(current); | |
b = Number(digitsAnother[index] || 0); | |
sum = a + b + memo.next; | |
return { | |
asString: (sum > 9 ? sum - 10 : sum) + memo.asString, | |
next: sum > 9 ? 1 : 0 | |
} | |
}, { | |
asString: '', | |
next: 0 | |
}).asString; | |
return new LargeInt(digits); | |
} | |
LargeInt.prototype.compare = function(another) { | |
return LargeInt._compare(this.asString, another.asString); | |
} | |
LargeInt.prototype.equalThan = function(another) { | |
return this.compare(another) == LargeInt.EQUAL; | |
} | |
LargeInt.prototype.largerThan = function(another) { | |
return this.compare(another) == LargeInt.LARGER; | |
} | |
LargeInt.prototype.smallerThan = function(another) { | |
return this.compare(another) == LargeInt.SMALLER; | |
} | |
LargeInt.prototype.toString = function() { | |
return 'LargeInt[' + this.asString + ']'; | |
} |
This file contains hidden or 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 | |
class BigInt { | |
private $number; | |
public function __construct($number = '') { | |
$this->number = preg_replace('/^0+/', '', $number); | |
} | |
/** | |
* @return BigInt | |
*/ | |
public function sum() { | |
$memo = $this; | |
foreach(func_get_args() as $another) { | |
if(is_string($another)) { | |
$another = new BigInt($another); | |
} | |
$memo = self::sumTwo($memo, $another); | |
} | |
return $memo; | |
} | |
/** | |
* @param BigInt|string $another | |
* @return bool | |
*/ | |
public function equals($another) { | |
if(is_string($another)) { | |
$another = new BigInt($another); | |
} | |
return $this->__toString() === $another->__toString(); | |
} | |
private static function sumTwo($one, $another) { | |
list($one, $another) = self::pad($one->number, $another->number); | |
$one = str_split(strrev($one)); | |
$another = str_split(strrev($another)); | |
$result = ''; | |
$additional = ''; | |
foreach($one as $index => $a) { | |
$a = (int) $a; | |
$b = (int) $another[$index]; | |
$c = $a + $b + $additional; | |
if(strlen($c) == 2) { | |
list($additional, $c) = str_split($c); | |
} else { | |
$additional = ''; | |
} | |
$result .= $c; | |
} | |
return new self($additional . strrev($result)); | |
} | |
public function __toString() { | |
return 'BigInt[' . $this->number . ']'; | |
} | |
private static function pad($one, $another) { | |
$oneLength = strlen($one); | |
$anotherLength = strlen($another); | |
return array( | |
$oneLength > $anotherLength ? $one : str_pad($one, $anotherLength, '0', STR_PAD_LEFT), | |
$anotherLength > $oneLength ? $another : str_pad($another, $oneLength, '0', STR_PAD_LEFT) | |
); | |
} | |
} | |
$a = new BigInt('1000000000119'); | |
$b = new BigInt('1000000000001'); | |
$c = new BigInt('2000000000120'); | |
$a->sum($b)->equals($c); // true | |
$small = new BigInt('1'); | |
echo $small->sum('2', '3', '4', '5', '100'); // BigInt[115] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment