|
|
|
|
|
|
| |
This is a function that will add positive integer numbers together of almost any length. (in fact up to the maximum string length that php can handle)
This gets around the 'rounding off' that many program languages carry out once a number reaches a certain size.
Note though that both the numbers to be added together must be positive.
Note also that the returned answer will be a string and not a number, so bear that in mind if you intend to use this in conjunction with other functions/procedures.
Keep an eye out for my next submissions as they will be 'big_subtract' , 'big_divide' , and 'big_multiply'. Carrying out subtraction, division and multiplication of very large integers.
Sums two positive numbers together. Note that the two numbers involved
must both be positive
| <?php
$number_one = "457736563527579860946";
$number_two = "244899980006764545657";
$answer = big_addition ($number_one, $number_two);
print $answer;
function big_addition ($number_one, $number_two) {
$carry = 0;
if (strlen($number_one) > strlen($number_two)) {
$longer_length = strlen($number_one);
$shorter_length = strlen($number_two);
$set_one = $number_one;
$zero_number = ($longer_length - $shorter_length);
$zero_string = "";
for ( $index = 1; $index <= $zero_number; $index++ ) {
$zero_string = $zero_string . "0";
}
$set_two = $zero_string . $number_two;
$length = strlen($number_one);
}
if (strlen($number_one) < strlen($number_two)) {
$longer_length = strlen($number_two);
$shorter_length = strlen($number_one);
$set_two = $number_two;
$zero_number = ($longer_length - $shorter_length);
$zero_string = "";
for ( $index = 1; $index <= $zero_number; $index++ ) {
$zero_string = $zero_string . "0";
}
$set_one = $zero_string . $number_one;
$length = strlen($number_two);
}
if (strlen($number_one) == strlen($number_two)) {
$set_one = $number_one;
$set_two = $number_two;
$length = strlen($number_one);
}
$answer = "";
for ( $index = ($length - 1); $index >= 0; $index-- ) {
$digit = $carry + (( integer ) $set_one[$index]) + (( integer ) $set_two[$index]);
$digit_string = (( string ) $digit);
if ($digit >= 10) {
$answer = $answer . $digit_string[1];
$carry = ( integer ) $digit_string[0];
}
else {
$answer = $answer . $digit_string;
$carry = 0;
}
}
if ($carry != 0) {
$carry_string = (( string ) $carry);
$answer = $answer . $carry_string;
}
$answer = strrev($answer);
return $answer;
}
?> | |
|
|
| Math operations on big numbers Categories : PHP, Math. | | | Latitude-Longitude to Miles Categories : PHP, Utilities, Math. | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | | | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | Temperature Conversion Categories : PHP, Math., Beginner Guides | | | decoct -- Decimal to octal Categories : PHP, PHP Functions, Math. | | | How to judge if an integer is odd or is even in Php3? Categories : Math., PHP, Algorithms | | | Calculator for Baroque Violin strings Categories : Math., PHP, Strings | | | Show the steps for converting a number from a given base to base 10. Shows the steps involved in converting a number from a given base to base 10. Categories : PHP, Math., Algorithms | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | 3dLib - a class for drawing in 3D space. Supported functions: Line, SetPixel, Polygon, FilledPolygon, etc. 3dChart() function has been added for one-call drawing of 3d charts. Support of mostly used 3d-transformations. Categories : Graphics, Math., PHP Classes, PHP, Charts and Graphs | | | Easy to use random number function that seeds with uniqid and allows a max value Categories : Math., PHP | | | Arbitrary Precision Math using BCMATH routines Categories : PHP, Math., BC math | | | Greatest Common Denominator - A simple class that finds the greatest common denominator for two integers.
Categories : PHP, PHP Classes, Math. | | | Prime number finder (Sieve of Erastothenes) Categories : PHP, Algorithms, Math. | |
|
|
|