Skip to content

Commit 96aa780

Browse files
Create Han_Xin_Counting_Soldiers.c (#91)
* Create Han_Xin_Counting_Soldiers.c Add a example of Han Xin Counting Soldiers. * test Chinese remainder theorem Co-authored-by: shellhub <[email protected]>
1 parent c0e9726 commit 96aa780

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

basics/chinese_remainder_theorem.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://en.wikipedia.org/wiki/Chinese_remainder_theorem
3+
*/
4+
#include <assert.h>
5+
6+
int getSoldiers(int limit) {
7+
for (int i = limit; i >= 0; --i) {
8+
if (i % 3 == 2 && i % 5 == 3 && i % 7 == 2) {
9+
return i;
10+
}
11+
}
12+
return -1; // unreachable code
13+
}
14+
15+
void test() {
16+
assert(getSoldiers(100) == 23);
17+
assert(getSoldiers(1000) == 968);
18+
assert(getSoldiers(10000) == 9998);
19+
}
20+
21+
int main() {
22+
test();
23+
return 0;
24+
}

0 commit comments

Comments
 (0)