forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding PHP Implementation for Chinese Rem. Th. (jainaman224#2685)
- Loading branch information
1 parent
8103e94
commit 951da47
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
// Implementation of Chinese Remainder Theorem in PHP | ||
|
||
// function to find modular multiplicative inverse of | ||
// 'val' under modulo 'm' | ||
function modInv($val, $m) { | ||
$val = $val % $m; | ||
for ($x = 1; $x < $m; $x++) | ||
if (($val * $x) % $m == 1) | ||
return $x; | ||
} | ||
|
||
/* | ||
findVal() returns the smallest number reqValue such that: | ||
reqValue % div[0] = rem[0], | ||
reqValue % div[1] = rem[1], | ||
..... | ||
reqValue % div[k-2] = rem[k-1] | ||
It is assumed that the numbers in div[] are pairwise coprime. | ||
*/ | ||
function findVal($div, $rem, $size) { | ||
// $totalProd represents product of all numbers | ||
$totalProd = 1; | ||
for ($i = 0; $i < $size; $i++) | ||
$totalProd *= $div[$i]; | ||
|
||
// $result represents summation of | ||
// (rem[i] * partialProd[i] * modInv[i]) | ||
// for 0 <= i <= size-1 | ||
$result = 0; | ||
for ($i = 0; $i < $size; $i++) { | ||
$partialProd = (int)$totalProd / $div[$i]; | ||
$result += $rem[$i] * $partialProd * modInv($partialProd, $div[$i]); | ||
} | ||
|
||
$reqValue = $result % $totalProd; | ||
return $reqValue; | ||
} | ||
|
||
// Sample case to test the code | ||
$divisor = array(5, 7, 8); | ||
$remainder = array(3, 1, 6); | ||
$size = sizeof($divisor); | ||
echo "Value is ". findVal($divisor, $remainder, $size); | ||
|
||
|
||
/* | ||
Sample Input as specified in the code: | ||
Divisors: 5, 7, 8 | ||
Remainders: 3, 1, 6 | ||
Sample Output: | ||
Value is 78 | ||
*/ | ||
|
||
?> | ||
|