Chinese Remainder Theorem

Chinese Remainder Theorem
Given x = a1 (mod n1), ..., x = ak (mod nk), where n1 through nk are pairwise coprime, the CRT finds a solution for x. import java.util.*; public class CRT { 	static int x, y; 	public static void main(String args[]) { 		int[] a = {3, 1, 6}; int[] n = {5, 11, 17}; int N = 1; //Product of n1, n2, ..., nk 		for(int i=0;i < n.length;i++) N *= n[i]; //Find eis int[] e = new int[a.length]; for(int i=0;i < e.length;i++) { 			exgcd(n[i], N/n[i]); e[i] = y*N/n[i]; } 		int sol = 0; for(int i=0;i < e.length;i++) sol += a[i]*e[i]; //All solutions are congruent to sol (mod N) 		System.out.println("Some solutions: "); for(int i=-5;i <= 5;i++) System.out.println(sol + i*N); } 	public static int exgcd(int a, int b) 	{ int ox = 1; int oy = 0; int nx = 0; int ny = 1; boolean swap = false; if(a < b) 		{ int tmp = a; 			a = b; 			b = tmp; swap = true; } 		while(b > 0) { 			int q = a / b; 			int m = a % b; 			a = b; b = m; 			int nnx = ox - q*nx; int nny = oy - q*ny; ox = nx; nx = nnx; oy = ny; ny = nny; } 		x = (swap? oy : ox); y = (swap? ox : oy); return a; 	} }