Skip to content

Commit 9f9916f

Browse files
committed
Added chinese remainder theorem
1 parent 130ac52 commit 9f9916f

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env perl
2+
# https://en.wikipedia.org/wiki/Chinese_remainder_theorem
3+
4+
use strict;
5+
use warnings;
6+
7+
# This is a very basic approch
8+
sub chinese_remainder_theorem {
9+
my ($num, $rem) = @_;
10+
my $x = 1;
11+
my $size = scalar(@{$num});
12+
13+
# Though this looks like infinite loop, but there will always exists an x
14+
# that satisfies the given criteria
15+
while (1) {
16+
# Check if remainder of x/num[j] is equal to rem[j] or not
17+
my $j = 0;
18+
while ($j < $size) {
19+
if ($x % $num->[$j] != $rem->[$j]) {
20+
last;
21+
}
22+
$j += 1;
23+
}
24+
25+
# If all remainders get matched, we have found x
26+
if ($j == $size) {
27+
return $x;
28+
}
29+
30+
# else try with next number
31+
$x += 1;
32+
}
33+
}
34+
35+
my @num = (4, 7, 11);
36+
my @rem = (1, 5, 2);
37+
print chinese_remainder_theorem(\@num, \@rem); # Output - 145
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env perl
2+
# https://en.wikipedia.org/wiki/Chinese_remainder_theorem
3+
# https://metacpan.org/pod/Math::Prime::Util#chinese
4+
5+
use strict;
6+
use warnings;
7+
use Math::Prime::Util qw(chinese);
8+
9+
print chinese([14, 643], [254, 419], [87, 733]); # OUTPUT - 87041638
10+

README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,18 @@
1414

1515
| Backtracking | Status |
1616
| ---------------- | --------- |
17-
| 8 Queens Problem |
17+
| 8 Queens Problem |
1818
| Combinations | Completed |
1919
| Permutations | Completed |
20-
| Sudoku |
20+
| Sudoku |
2121

22-
| Cryptography/Ciphers | Status |
23-
| -------------------- | --------- |
24-
| Base16 | Completed |
25-
| Base32 | Completed |
26-
| Base64 | Completed |
27-
| Base85 | Completed |
28-
| Caesar Cipher | Completed |
29-
| Hill Cipher |
30-
| Morse Code | Completed |
31-
| Playfair Cipher |
32-
| RSA | Completed |
33-
| Vigenere Cipher | Completed |
22+
| Blockchain | Status |
23+
| ---------- | ------ |
24+
| chinese_remainder_theorem | Completed |
25+
26+
| Client_Server | Status |
27+
| ------------- | ------ |
28+
| Client Server example | Completed |
3429

3530
| Compression | Status |
3631
| --------------- | ------ |
@@ -48,6 +43,19 @@
4843
| Roman -> Integer | Completed |
4944
| Integer -> Roman | Completed |
5045

46+
| Cryptography/Ciphers | Status |
47+
| -------------------- | --------- |
48+
| Base16 | Completed |
49+
| Base32 | Completed |
50+
| Base64 | Completed |
51+
| Base85 | Completed |
52+
| Caesar Cipher | Completed |
53+
| Hill Cipher |
54+
| Morse Code | Completed |
55+
| Playfair Cipher |
56+
| RSA | Completed |
57+
| Vigenere Cipher | Completed |
58+
5159
| Data Structure | Status |
5260
| ------------------------------------------------------------------------------------------- | --------- |
5361
| Arrays | Completed |
@@ -111,3 +119,10 @@
111119
| Problem 6 | Completed |
112120
| Problem 7 | Completed |
113121
| Problem 8 | Completed |
122+
123+
| Web Programming | Status |
124+
| --------------- | --------- |
125+
| Catalyst |
126+
| Dancer2 |
127+
| Mojolicious |
128+
| Web Crawling |

0 commit comments

Comments
 (0)