WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : multiplication of large positive integer numbers
Categories : PHP, Math. Click here to Update Your Picture
Jeremy Watts
Date : Apr 28th 2004
Grade : 3 of 5 (graded 4 times)
Viewed : 3235
File : 3851.php
Images : No Images for this code example.
Search : More code by Jeremy Watts
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

The next in the series of 'big arithmetic' multiplies together two very large positive integer numbers.

Note that 'big_multiply' uses the other big arithemetic function ,'big_addition', to work, and must be incuded with the function.

<?php

$number_one
= "106674437659978524323534368687676867987987976875764";
$number_two = "1258665764565487687698798744354358677768754234566789765443658798654243587940";

$answer = big_multiply ($number_one, $number_two);
print
$answer;

function
big_multiply ($number_one, $number_two) {

$length_one = strlen($number_one);
$length_two = strlen($number_two);

$row = "";
$digit_string = "";
$answer = "";
$zero_number = 0;
$carry = 0;

for (
$index_two = ($length_two - 1); $index_two >= 0; $index_two-- ) {

$zero_string = "";
for (
$index = 1; $index <= $zero_number; $index++ ) {
   
$zero_string = $zero_string . "0";
}

for (
$index_one = ($length_one - 1); $index_one >= 0; $index_one-- ) {
   
$digit = ((( integer ) $number_two[$index_two]) * (( integer ) $number_one[$index_one])) + $carry;
   
$digit_string = ( string ) $digit;
   
    if (
$digit >= 10) {
       
$row = $row . $digit_string[1];
       
$carry = ( integer ) $digit_string[0];
    }
    else {
       
$row = $row . $digit_string;
       
$carry = 0;
    }
}

if (
$carry != 0) {
   
$row = $row . ( string ) $carry;
}

$row = $zero_string . $row;
$row = strrev($row);

$answer = big_addition ($answer, $row);

$row = "";
$carry = 0;

$zero_number++;
}

$flag = 0;
$length = strlen($answer);
for (
$index = 0; $index <= ($length - 1); $index++ ) {
    if (
$answer[$index] != "0") {
       
$flag = 1;
    }
}

if (
$flag == 0) {
   
$answer = "0";
}

return
$answer;
}



function
big_addition ($number_one, $number_two) {

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);

}

$carry = 0;
$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.
 P C wrote : 1090
I tried out the script without changing an iota, and it worked perfectly, meaning that I transcribed the result to my desk calculator (dc) and repeated the calculations there, subtracted the two results, and I got a difference of zero.
Good script, Jeremy!
 
 jeremy watts wrote : 1091
thanks for the comment cp.  glad it worked for you!  not sure of its limits yet, but they`re pretty big as some of the numbers i`ve entered have been huge.  anyway who gave me the low score and why?????
 
 P C wrote :1092
If they give a low grade without giving a reason, it could be that grade was not for the programming aspects, but more to the usefulness or practicality of the code.

I do understand why not many people are not interested in this highly mathematical challenge.  Those who have tried calculating pi to 20,000 digits, or tried to factorize a big number will understand.
Do not get discouraged by a few negative comments, keep up the good work.