Skip to content

Instantly share code, notes, and snippets.

@mikestratton
Created April 18, 2019 14:53
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 mikestratton/0528aa076078afb23da8e659aecf7be8 to your computer and use it in GitHub Desktop.
Save mikestratton/0528aa076078afb23da8e659aecf7be8 to your computer and use it in GitHub Desktop.
Lattice Multiplication Algorithm to Multiply Extremely Large Numbers in the Browser
<?php
// NOT COMPLETE YET!
//<li>Store number1 and number2 as variables (num1, num2)</li>
echo '<p>Store number1 and number2 as variables: <br>';
$numX = $_POST["numX"];
$numY = $_POST["numY"];
echo $numX . '<br>' . $numY . '</p>';
// <li>num1 and num2 toArray</li>
echo '<p>Number 1 and number 2 as array: <br>';
$arrX = str_split($numX, 1);
$arrY = str_split($numY);
var_dump($arrX);
echo '<br>';
var_dump($arrY);
echo '</p>';
// <li>Variables to count the number of characters in each number (cnt, cnt2)</li>
echo '<p>Variables to count the number of characters in each number (cnt, cnt2): <br>';
$cntX = count($arrX);
$cntY = count($arrY);
echo $cntX . '<br>' . $cntY . '</p>';
// <li>Array variable to store results of lattice (lattice[]) equation</li>
echo '<p>Array variable to store results of lattice (lattice[]) equation</p>';
$lattice = array();
// <li>Array variable to store 2 digits or more. (2plus[])</li>
echo '<p>Array variable to store 2 digits or more. (plus[])</p>';
$plus = array();
// <li>If cnt1 < cnt2 then counter = cnt2 else counter = cnt1</li>
echo '<p>If cnt1 < cnt2 then counter = cnt2 else counter = cnt1 <br>';
$counter = 0;
function greater($a, $b){
if($a > $b){
$counter = $a;
}
else{$counter = $b;}
echo $counter . '</p>';
return $counter;
}
greater($cntX, $cntY);
echo 'multiply<br>' . $equals . '<br><br>';
// <li>function count <br>
function lattice1($n1, $n2, $c){
$lattice = array();
$x = array();
$y = array();
for($i=1; $i<$c; $i++){
$x = $n1[$i];
$y = $n2[$i];
$equals = $y * $x;
echo $i . 'equals: ' . $equals . '<br><br>';
array_push($lattice, $equals);
}
var_dump($lattice);
echo '<br>';
}
lattice1($cntX, $cntY, $counter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment