big m = 770;
big n = 133;

//------------------------------

big euclides(big a, big b)
{
	#trace clear
	
	#: if(LATEX)
		#Trace "$$\\begin{array}{lclc}\n"
	
	while (b > 0)
	{
		#: if(LATEX)
		#: {
			#Trace string.Format("\t\\m" + "cd({0}, {1}) &=&	\\m" + "cd({0}, {0}\\Mmod{1}) &=\\\\ \n", a, b);
		#: }
		#: else
		#: {
			#Trace string.Format("m" + "cd({0}, {1}) \t=\t m" + "cd({0}, {0} mod {1}) \t= \n", a, b);
 		#: }


		big t = b;
		b = a % b;
		a = t;
		
	}
	
	#: if (LATEX)
	#: {
		#Trace string.Format("\t\\m" + "cd({0}, 0) &=& {0} \\\\ \n", a);
		#Trace "\\end{array}$$\n";
	#: }
	#: else
	#: {
		#Trace string.Format("m" + "cd({0}, 0) \t=\t {0} \n", a);
	#: }


	return a;
}

#Return euclides(m, n);big a = 1492;
big b = 1066;

string AEE(big m, big n)
{
	big a = m; big b = n; // para no alterar los valores de entrada

	// Una matriz M de 2×2 servirá para calcular una igualdad de Bézout.
	// Se inicializa a la matriz unidad:
	big m11 = 1; big m12 = 0; big m21 = 0; big m22 = 1;
	int s = 1; // signo
	
	while (b > 0)
	{
		big c = a/b;
		big r = a - b * c;
		a = b;
		b = r; // Euclides
		
		s = -s; // el signo alterna en cada iteración
		// la matriz M se transforma: M = M · {{c, 1}, {1, 1}}
		big t11 = m11 * c + m12; big t12 = m11; big t21 = m21 * c +m22; big t22 = m21;
		m11 = t11; m12 = t12; m21 = t21; m22 = t22;
	}
	
	// Ya hemos obtenido mcd(m,n) = a
	big alfa = s * m22; big beta = -s * m12; // y una igualdad de Bézout: α·m +β·n = mcd(m,n)
	
	string ret = string.Format("({0}) * ({1}) + ({2}) * ({3}) = {4}", alfa, m, beta, n, a);
	return ret;
}

#Return AEE(a, b);
public int CalculaAlgo(int a, int b)  
{
	#Trace clear

	int r = 0; 

	int i = 0;	// Solo para la traza

	#Trace "i\t\ta\t\tb\t\tr\t\ta·b + r\n-----------------------------------------------------------------------------------------------";
	#Trace "\n", i, "\t\t", a, "\t\t", b, "\t\t", r, "\t\t", a*b + r;


	while (a > 0)
	{
		if (a % 2 == 1)
			r += b;
		a = (int) a / 2;
		b *= 2;

		i++;	// solo para la traza

		#Trace "\n", i, "\t\t", a, "\t\t", b, "\t\t", r, "\t\t", a*b + r;

	}

	return r;
	
}

#Return(CalculaAlgo(6, 2));public int HazAlgo(int n, int m) // n >= m
{

	// Valida los argumentos
	#Trace clear
	if (m > n)
	{
		#Trace "Error: n debe ser mayor o igual que m";
		return 0;
	}


	if (m > n)
		return 0;
	if (m == 0)
		return 1;
	

	int p = n;
	int q = 1;

	while (m > 1)
	{
		q = q + 1;
		n = n - 1;
		m = m - 1;
		p= p*n / q;
	}

	return p;
	
}

#Return HazAlgo(6, 5);public int HazAlgo(int n, int m) // n >= m
{
	#Trace clear

	// Valida los argumentos
	if (m > n)
	{
		#Trace "Error: n debe ser mayor o igual que m";
		return 0;
	}

	if (m == 0)
		return 1;
	

	int p = n;
	int q = 1;

	int i = 0; // Solo se usa para numerar las iteraciones y mostrarlas en la traza

	#Trace clear
	#Trace "Paso ", string.Format("{0:000}: ", i), "p·q!·(n-1)! = ", p*fact(q)*fact(n-1);

	while (m > 1)
	{
		q = q + 1;
		n = n - 1;
		m = m - 1;
		p= p*n / q;

		i++;
		#Trace "\nPaso ", string.Format("{0:000}: ", i), "p·q!·(n-1)! = ", p*fact(q)*fact(n-1);
	}

	return p;
	
}

#Return(HazAlgo(6, 5));public big binom(big n, big m)
{
	big result = 0;

	if (n == m)
	{
		result = 1;
	}
	else
	{
		if (2 * m > n)
			m = n - m;
		big num = fact(n);
		big den = fact(m)*fact(n-m);

		result = num / den;
	}
	return result;
}

public int HazAlgo(int n, int m) // n >= m
{
	#Trace clear

	// Valida los argumentos
	if (m > n)
	{
		#Trace "Error: n debe ser mayor o igual que m";
		return 0;
	}

	if (m == 0)
		return 1;
	

	big b = binom(n, m);

	int p = n;
	int q = 1;

	int i = 0; // Solo se usa para numerar las iteraciones y mostrarlas en la traza

	#Trace clear
	#Trace "Paso ", padleft(i, 3), ": p = ", padleft(p, 3), ", q = ", padleft(q, 3), ", n = ", padleft(n, 3),  ", m = ", padleft(m, 3), ", p·q!·(n-1)! = ", padleft(p*fact(q)*fact(n-1), 4);
	while (m > 1)
	{
		q = q + 1;
		n = n - 1;
		m = m - 1;
		p= p*n / q;

		i++;
		#Trace "\nPaso ", padleft(i, 3), ": p = ", padleft(p, 3), ", q = ", padleft(q, 3), ", n = ", padleft(n, 3),  ", m = ", padleft(m, 3), ", p·q!·(n-1)! = ", padleft(p*fact(q)*fact(n-1), 4);
	}

	#Trace "\nEl número binómico n sobre m vale ", b, ". Compáralo con el valor retornado." 
	
	return p;
	
}


#Return(HazAlgo(60, 5));///
/// Hace algo
/// Entrada: dos enteros 0 < a < b
///
/// Cambia aquí los valores de entrada:

int a = 45;
int b = 2321;

///

public int HazAlgo(int a, int b) {

	if (a <= 0 || b<= 0 || a > b)
		throw new Exception ("Los argumentos deben ser positivos y a < b");
	
	int p = 0;

	while (a > 0)
	{
		int c = a / 10;
		int r = a % 10; // mod
		if (r != 0) 
			p = p + r*b;
		a = c;
		b = 10*b;
	}

	return p;
	
}

#Return HazAlgo(a, b);///
/// Hace algo
/// Entrada: dos enteros 0 < a < b
///
/// Cambia aquí los valores de entrada:

int a = 45;
int b = 2321;

///

public int HazAlgo(int a, int b) {

	if (a <= 0 || b<= 0 || a > b)
		throw new Exception ("Los argumentos deben ser positivos y a < b");
	
	int p = 0;

	#Trace clear
	#Trace "a=", a, "\n"
	#Trace "b=", b, "\n"


	while (a > 0)
	{
		int c = a / 10;
		int r = a % 10; // mod
		if (r != 0) 
			p = p + r*b;
		a = c;
		#Trace "r=", r, "\t\t",  "a=", padleft(a,10), "\t\tp=", padleft(p, 10), "\tr·b=", padleft(r*b,10), "\n" 
		b = 10*b;
	}

	return p;
	
}

#Return HazAlgo(a, b);///
/// Hace algo
/// Entrada: dos enteros 0 < a < b
///
/// Cambia aquí los valores de entrada:

int a = 45;
int b = 2321;

///

public int HazAlgo(int a, int b) {

	if (a <= 0 || b<= 0 || a > b)
		throw new Exception ("Los argumentos deben ser positivos y a < b");
	
	int p = 0;

	#Trace clear
	#Trace "a=", a, "\n"
	#Trace "b=", b, "\n"

	#Trace "I=a·b+p=", a*b+p, "\n"
	while (a > 0)
	{
		int c = a / 10;
		int r = a % 10; // mod
		if (r != 0) 
			p = p + r*b;
		a = c;
		b = 10*b;
		#Trace "r=", r, "\t",  "a=", a, "\t",  "r·b=", padleft(r*b/10,10), "\tp=", padleft(p, 10), "\tI=a·b+p=", a*b+p, "\n" 
	}

	return p;
	
}

#Return HazAlgo(a, b);///
/// Hace algo
/// Entrada: dos enteros 0 < a < b
///
/// Cambia aquí los valores de entrada:

int a = 321;
int b = 98765;

///

public int HazAlgo(int a, int b) {

	if (a <= 0 || b<= 0 || a > b)
		throw new Exception ("Los argumentos deben ser positivos y a < b");
	
	int m = a;
	int n = b;

	int p = 0;

	#Trace clear
	#Trace "a=", a, "\n"
	#Trace "b=", b, "\n"

	#Trace "I=a·b+p=", a*b+p, "\n"
	while (a > 0)
	{
		int c = a / 10;
		int r = a % 10; // mod
		if (r != 0) 
			p = p + r*b;
		a = c;
		b = 10*b;
		#Trace "r=", r, "\t",  "a=", a, "\t",  "r·b=", padleft(r*b/10,10), "\tp=", padleft(p, 10), "\tI=a·b+p=", a*b+p, "\n" 
	}

	#: if (m == 321 && n == 98765)
		#Trace "\n\n      98765 \n ×      321 \n------------\n      98765 \n    197530  \n   296295   \n------------\n = 31703565 \n"


	return p;
}

#Return HazAlgo(a, b);class lsign : List<string>
{
	public lsign(List<string> ls) : base()
	{
		foreach(string s in ls)
		{
			if (!this.Contains(s)) this.Add(s);
		}
	}

	public override string ToString()
	{
		int n = 1;
		string ss = "";
		foreach(string s in this)
		{
			ss += (n.ToString() + ": " + s + "\n");
			n++;
		}
		return ss;
	}
} 
			

List<string> AllSign(int n)
{
	if (n == 1)
	{
		List<string> ls = new List<string>();
		ls.Add ("+");
		ls.Add("-");
		return ls;
	}
	else
	{
		List<string> ls2 = new List<string>();
		foreach(string s in AllSign(n-1))
		{
			ls2.Add("+" + s);
			ls2.Add("-" + s);
		}
		return ls2;
	}	
}

#Return new lsign(AllSign(4))		
	 	int s=3;
int p=11;
int q=19;
int n;


int parity (int m)
{
	int p = 0;
	int m1 = m;
	while (m1 > 0)
	{
		int b = m1 % 2;
		m1 = (int) (m1 / 2);
		p = (p + b) % 2;
	}	
	return p;  
}

string BBS(int k)
{
	n = p*q;
	string seq = "";
	
	for (int i=0; i < k; i++)
	{
		s = (s*s) % n;
		#Trace s, "\n"
		seq = seq + parity(s);
	}
	return seq;
}

#Return BBS(100);
/// Calcula la potencia el producto de dos enteros positivos usando sumas y operaciones de bits.
/// Entrada: dos enteros positivos b, e
/// Salida : la potencia b^e
/// Cambia aquí los valores de entrada:

big bas = 2;
big exp = 100;

///

// -----------------------------------------------------------------------------------------------------------

public big BPower(big b, big e) 
{
	if (b <= 0 || e <= 0) 
		throw new Exception ("Los argumentos deben ser positivos.");

	#Trace clear

	#Trace "El exponente expresado en binario es ", e.ToString(2), " (", e.bitCount(), " bits )\n\n"
 	#: int k = 0;
	
	big p = 1;
	#: string op = ""; 
	
	#: int lenp = PotS(b, e).ToString().Length + 5; 
 	#: int lene = e.ToString().Length + 5;
	#: int leno = 5;
	#: int lenk = (e.ToString(2).Length * 2).ToString().Length;
 
	#Trace padleft("#", lenk), padleft("op", leno), padleft("e", lene), padleft("p", lenp), padleft("b", lenp), padleft("p*b^e", lenp), "\n" 
	#Trace padleft("", lenk + leno + lene + lenp + lenp + lenp, '-'), "\n"
	#Trace padleft(k, lenk), padleft(op, leno), padleft(e, lene), padleft(p, lenp), padleft(b, lenp), padleft(p * PotS(b, e), lenp), "\n\n"
	while (e > 0)
	{
		#: op = "";
		#: k++;
		if ((e & 1) == 1)  // El bit menos significativo de e es 1: e es impar
		{
			p *= b;
			#: op += "M";
		}

		e >>= 1; // e = cociente de e / 2
		if (e > 0)
		{
			b = b * b;
			#: op += "C";
		} 
		#Trace padleft(k, lenk), padleft(op, leno), padleft(e, lene), padleft(p, lenp), padleft(b, lenp), padleft(p * PotS(b, e), lenp), "\n"
	}
	return p; 
}

big PotS(big b, big e)
{
	big p = 1;
	while (e > 0)
	{
		if ((e & 1) == 1)  // El bit menos significativo de e es 1: e es impar
			p *= b;

		e >>= 1; // e = cociente de e / 2
		if (e > 0)
			b = b * b;
	}
	return p; 
}

#Return BPower(bas, exp);

/// Calcula el producto de dos enteros positivos usando sumas y operaciones de bits.
/// Entrada: dos enteros positivos, a y b
/// Salida : el producto a * b
/// Cambia aquí los valores de entrada:

big a = 9876;
big b = 405;

// -----------------------------------------------------------------------------------------------------------

public big BProduct(big a, big b) 
{

	if (a <= 0 || b <= 0) 
		throw new Exception ("Los argumentos deben ser positivos.");

	#Trace clear
	
	if (a > b)
	{
		#Trace "■ Intercambiamos a y b para que a sea el menor.\n";
		big t = a;
		a = b;
		b = t;
	}


	big p = 0; // El resultado

	#: int lena = a.ToString(10).Length; // Mira el tamaño, para el relleno en la traza
	#: int leni = (a.ToString(2).Length).ToString().Length; // Mira el tamaño, para el relleno en la traza
	#: int lenp = (a * b).ToString(10).Length; // Mira el tamaño, para el relleno en la traza

	#Trace "■ Vamos a hacer la multiplicación\n"
	#Trace "        ", padleft(b, lenp), "\n"
	#Trace "      * ", padleft(a, lenp), "\n"
	#Trace "     ", padleft("", lenp+3, '-'), "\n"
	#Trace "      = ", padleft("", lenp, '?'), "\n\n"

	#: int s = 0;
	#: int d = 0;
	#Trace "■ a = ", a, " expresado en binario es ", a.ToString(2), " (", a.bitCount(), " bits).\n\n"
	
	#: int i = 0;
	#Trace "■ Antes de entrar el bucle:\n" 
	#Trace "\t", padleft(i, leni), ": p = ", padleft(p, lenp), "\tb = ", padleft(b, lenp), "\ta = ", padleft(a, lena), "\tI = a·b+p = ", a*b+p, "\n\n"

	#Trace "■ Iteraciones del bucle:\n"
	while (a > 0)
	{
		#: string op = "  ";
		if ((a & 1) == 1) // El bit menos significativo de a es 1: a es impar
		{
			#: op = " +"; s++;
			p += b;
		} 

		b <<= 1; op += " «"; // b = 2 * b
		a >>= 1; op += " »"; // a = cociente de a / 2

		#: d += 2;
		#: i++;
		#Trace "\t", padleft(i, leni), ": p = ", padleft(p, lenp), "\tb = ", padleft(b, lenp), "\ta = ", padleft(a, lena), "\tI = a·b+p = ", a*b+p, "   ←  ", op, "\n" 
	}
	
	#Trace "\nOperaciones realizadas: ", s, " sumas y ", d, " desplazamientos de bits."
	return p;
	
}

#Return BProduct(a, b);/// Entrada: dos enteros positivos a y b 
/// Salida : a+b
/// Solo se realizan operaciones de bits (excepto para la traza)
/// Cambia aquí los valores de entrada: 

big a0 = 529;
big b0 = 771;

/// 

public big BSum(big a, big b)
{
	if (a <= 0 || b <= 0) 
		throw new Exception ("Los argumentos deben ser positivos.");

	#Trace clear
	
	bool m = false; 	// Acarreo: "me llevo 1"
	big s = 0;	// Suma parcial 
	big p = 1;	// posición del siguiente bit a la izquierda (potencia de 2)
	
	#: int k = 0; // número de iteración
	#: big I = (a + b + (m ? 1 : 0)) * p + s;
	#: int len = (a + b).bitCount();
	#: int len10 = (a + b).ToString().Length;
	#: int lenk = (a > b ? a : b).ToString().Length;

	#Trace "Suma ", a, " + ", b, " en binario:\n\n"  
	#Trace "   ", padleft(a.ToString(2), len), "\n"
	#Trace " + ", padleft(b.ToString(2), len), "\n"
	#Trace padleft("", len+3, '-'), "\n"
	#Trace " = ", padleft((a+b).ToString(2), len), "\n\n"
	#Trace "Iteraciones:\n\n"

	#: I = (a + b + (m ? 1 : 0)) * p + s;
	#Trace "k = ", padleft(k, lenk),  ": ", "\tm = ", m ? 1 : 0, " \ta = ", padleft(a.ToString(2), len), " \tb = ", padleft(b.ToString(2), len), " \tp = ", padleft(p.ToString(2), len), " \ts = ", padleft(s.ToString(2), len), " \tI = ", padleft(I, len10), "\n"

	// Recorre los bits de los sumandos
	while (a > 0 || b > 0)
	{
		if ((a & 1) == (b & 1)) // 1+1, 0+0
		{
			if (m) s |= p;	// s = s + p
			m = ((a & 1) == 1);
		}		

		else // 1+0, 0+1
			if (!m) s |= p;	// s = s + p

		a >>= 1; // a = cociente de a / 2
		b >>= 1; // b = cociente de b / 2
		p <<= 1; // p = 2 * p	

		#: k++;
		#: I = (a + b + (m ? 1 : 0)) * p + s;
		#Trace "k = ", padleft(k, lenk),  ": ", "\tm = ", m ? 1 : 0, " \ta = ", padleft(a.ToString(2), len), " \tb = ", padleft(b.ToString(2), len), " \tp = ", padleft(p.ToString(2), len), " \ts = ", padleft(s.ToString(2), len), " \tI = ", padleft(I, len10), "\n"
	}

	return (m ? s|p : s);	// m * p + s 
}

#Return(BSum(a0, b0)); 
/// Entrada: dos enteros positivos a y b 
/// Salida : a+b
/// Cambia aquí los valores de entrada: 

int a = 321;
int b = 1;

/// 

public big BSum(big a, big b)
{
	if (a <= 0 || b <= 0) 
		throw new Exception ("Los argumentos deben ser positivos.");

	#Trace clear

	// Los números como cadenas binarias
	string abin = a.ToString(2);
	string bbin = b.ToString(2);
	
	// Completa con ceros a la izquierda hasta que ambos sean del mismo tamaño
	// añade un cero a la izquierda, porque el resultado puede necesitarlo.
	bbin = padleft(bbin, abin.Length, '0');
	abin = padleft(abin, bbin.Length, '0');
	
	bool mellevo1 = false; 	// Acarreo
	string suma = "";		// Resultado 

	// Recorre los bits de las cadenas binarias
	for (int i = abin.Length - 1; i >= 0; i--)
	{
		if (abin[i] != bbin[i]) // 0+1, 1+0
			suma = (mellevo1 ? '0' : '1') + suma;
		
		else // 1+1, 0+0
		{
			suma = (mellevo1 ? '1' : '0') + suma;
			mellevo1 = (abin[i] == '1');
		}
	} 	

	if (mellevo1)
	{
		// El resultado tiene un bit más 
		suma = '1' + suma;

		#: // Para que se muestre bonito en la traza
		#: abin = "0" + abin;
		#: bbin = "0" + bbin;
	}

	#Trace "Sumamos en binario ", a, " + ", b, ":\n\n"
	#Trace "  ", abin, "\n", "+ ", bbin, "\n", padleft("", abin.Length+2, '-'), "\n", "= ", suma;

	return new big(suma, 2);	
}

#Return(BSum(a, b));int bucle()
{
	big n = 1;

	big m = new big(10)^5;

	#Trace clear

	while (true)
	{
		if (n % m == 0)
		{
			#T n, "\t-> Pulsa stop para parar\n"
		}
		n++;
	}

	return 0;
}


string Suma1()
{
	uint n = 4294967292;

	while (n > 0)
		n++;

	return "Sorpresa";
}

#R Suma1(); //bucle();string BucleSorpresa()
{
	int n = 2147483644; 

	while (n > 0)
		n++;

	return "¡Sorpresa!";
}


string BucleSorpresaExplicado()
{
	int n = int.MaxValue - 3; 

	#Trace clear
	#trace "Con enteros con signo:\n"
	#: int j = 0;
	while (n > 0)
	{
		n++;
		#: j++;
		#Trace "n", (j ==0 ? "" : "+" + j.ToString()) , "\t= \t", PadLeft(n, 11), " \t= \t", (j<=3 ? "0" : ""), Convert.ToString(n, 2), (n < 0 ? " -> ¡es negativo!": ""), "\n"
	}

	return "¡Sorpresa!";
}

string BucleSorpresaSinSigno()
{
	uint n = uint.MaxValue - 3; 
	#Trace clear
	#trace "Con enteros sin signo:\n"
	#: int j = 0;
	while (n > 0)
	{
		
		n++;
		#: j++;
		#Trace "n", (j ==0 ? "" : "+" + j.ToString()) , "\t= \t", PadLeft(n, 11), " \t= \t", (j<=3 ? "0" : ""), Convert.ToString(n, 2), (n == 0 ? " -> ¡es cero!": ""), "\n"
	}
	return "¡Sorpresa!";
}


//#R BucleSorpresa();

//#R BucleSorpresaExplicado();

#R BucleSorpresaSinSigno();










































	#: // uint n = 4294967292; // = uint.MaxValue - 3 = 2^32 - 3; ;
BigList BuscaPrimosLejanos(int n)
{
	if (n % 2 == 1)
		throw new Exception("La diferencia entre los dos primos debe ser par");

	#Trace clear
	big p1 = 2;
	big p2 = 3;

	while (p2 - p1 < n) 
	{
		p1 = p2;
		p2 = p2.NextPrime();
		#Trace set p1, "\t", p2, "\t-> ", p2-p1, "\n";
	}

	BigList par = new BigList();
	par.Add(p1);
	par.Add(p2);
	return par;
}

bool sep()
{
	big dos = new big(2);
	big c = 5*11;
	int i = 3;
	
	while (i < 100)
	{
		c = c * 10 + 5;
		big pa = c.PrevPrime();
		big ps = c.NextPrime();
		big s = ps - pa;
		if (s % 10 == 0) // c.isProbablePrime())
			#Trace string.Format("s={0}\ti={1}\t\t{2}\n", s, i, c);
		i++;
	}

	return true;
}

#return BuscaPrimosLejanos(100);// Muestra los cálculos intermedios para hallar la igualdad de Bézout 
// para a y b mediante el Algoritmo de Euclides.
// Retorna una lista (α, β, d) tal que α·a+β·b=d, siendo
// d el máximo común divisor de a y b.
// 
// Cambia aquí los valores:

big a = 328;
big b = 72;

// ----------------------------------------------------------------------

BigList EqBezout(big a, big b)
{
	#Trace clear
	#: string TT = "";
	#: string TL = "";


	big a0 = a;
	big b0 = b;

	// La calcula ya, aunque el objetivo es explicar los pasos en la traza:
	BigList eb = new BigList(bezout(a, b));

	#: int i = 0;

	big r = 1;
	big c = 0;
	big d = 0;

	// Trabajamos con positivos; los signos se ajustarán al final:
	a = a.abs();
	b = b.abs();

	#: string procesando = "Procesando:\n\n";
	#: TT += procesando + "Con "; 
	#: TT += string.Format("{0}=a y {1}=b,", a, b);
	#: TT += " las sucesivas divisiones del \nAlgoritmo de Euclides para calcular d=m" + "cd(a,b) son:\n\n";

	#: TL += "Con "; 
	#: TL += string.Format("${0}=a$ y ${1}=b$, ", a, b);
	#: TL += " las sucesivas divisiones del \nAlgoritmo de Euclides para calcular $d=\\m" + "cd(a,b)$ son:\n\n";

	#: string TTn = "", TTr = "";
	#: string TLn = "", TLr = "";

	do
	{
		#: i++;

		d = r;
		c = a / b;
		r = a % b;

		#: TTn += string.Format("{4}: {0}={1}·{2}+{3}", a, c, b,r, i);
		#: TTn += "\t --» m" + "cd(a, b)=m" + "cd(" + a.ToString() + ", " + b.ToString() + ")\t=" + mcd(a,b).ToString() + "\n";
		#: TTr += string.Format("r{0}={1}r{2}+r{3}@",  i-2, c==1 ? "": c.ToString()+"·",  i-1,  i);
		#: TLn += string.Format("{0} 	& = & {1} 	& \\cdot 	& {2} 	& + & {3}			\\\\\n", a, c, b,r);
		#: TLr += string.Format("r_[{0}] 	& = & {1}	& \\cdot 	& r_[{2}] 	& + & r_[{3}]\\\\@",  i-2, c,  i-1,  i);


		a = b;
		b = r;
	}
	while (r>0);

	#: // Texto: ------------------------------------------------
	#:
	#: // La tabla de divisiones numéricas 
	#: TT += TTn;
	#:
	#: // La tabla de divisiones simbólicas (hay algo mal: prueba con 328 y 72)
	#: TTr = TTr.Replace("r-1", "a");
	#: TTr = TTr.Replace("r0", "b");
	#: TTr = TTr.Replace("r"+(i-2).ToString(), "d");
	#: TTr = TTr.Replace("a=0·b+r1", "a=r1");
	#:		
	#: TT += "\nSi llamamos ";
	#: for (int j=1; j <=i-3; j++)
	#:	TT += "r" + j.ToString() + (j==i-3 ?  "" : ", ");
	#: TT += " a los restos, \nlas igualdades anteriores quedan\n\n";
	#:
	#: string[] TTLines = TTr.Split('@');
	#: for (int j = 0; j < TTLines.Length - 2; j++)
	#:	TT += (TTLines[j]+"\n");
	#:
	#: // Despejamos:
	#: TT+= "\nDespejamos d de la última igualdad y \neliminamos los restos ";
	#: for (int j=i-3; j>=1; j--)
	#:	TT += "r" + j.ToString() + (j==1 ? " " :", ");
	#: TT += "usando las igualdades anteriores \n(de abajo hacia arriba), con lo que \nexpresamos d como combinación lineal de a y b:\n";
	#: TT += string.Format("{0:s}·a+{1:s}·b=d\n", ToN(eb[0]), ToN(eb[1]));
	#: TT += "y así obtenemos la igualdad de Bézout:\n";
	#: TT += string.Format("{0:s}·{1:s}+{2:s}·{3:s}={4:s}\n", ToN(eb[0]) ,ToN(a0), ToN(eb[1]), ToN(b0), eb[2]);

	#: // Latex: ------------------------------------------------
	#:
	#: // La tabla de divisiones numéricas 
	#: TL += "$$\n\\begin{array}{rcrcrcr}\n" + TLn + "\\end{array}\n$$\n";
	#:
	#: // La tabla de divisiones simbólicas 
	#: TLr = TLr.Replace("r_[-1]", "a");
	#: TLr = TLr.Replace("r_[0]", "b");
	#: TLr = TLr.Replace("r_["+(i-2).ToString()+"]", "d");
	#: TLr = TLr.Replace("& 1 & \\cdot", "&&");
	#: TLr = TLr.Replace("0 & \\cdot  & b  & +", " &  &   & ");
	#:		
	#: TL += "\nSi llamamos $";
	#: for (int j=1; j <=i-3; j++)
	#:	TL += "r_[" + j.ToString() + "]" + (j==i-3 ?  "" : ", ");
	#: TL += "$ a los restos, \nlas igualdades anteriores quedan\n\n";
	#:
	#: TL += "$$\n\\begin{array}{ccccccc}\n";
	#: string[] TLLines = TLr.Split('@');
	#: for (int j = 0; j < TLLines.Length - 2; j++)
	#:	TL += (TLLines[j]+"\n");
	#: TL += "\\end{array}\n$$\n";
	#:
	#: // Despejamos:
	#: TL+= "\nDespejamos $d$ de la última igualdad y \neliminamos los restos $";
	#: for (int j=i-3; j>=1; j--)
	#:	TL += "r_[" + j.ToString() + "]" + (j==1 ? "" :", ");
	#: TL += "$ usando las igualdades anteriores \n(de abajo hacia arriba), con lo que \nexpresamos $d$ como combinación lineal de $a$ y $b$:\n";
	#: TL += string.Format("$$d={0:s}\\cdot a+{1:s}\\cdot b$$\n", ToN(eb[0]), ToN(eb[1]));
	#: TL += "y así obtenemos la igualdad de Bézout:\n";
	#: TL += string.Format("$${4:s}={0:s}\\cdot {1:s}+{2:s}\\cdot {3:s}$$\n", ToN(eb[0]) ,ToN(a0), ToN(eb[1]), ToN(b0), eb[2]);
	#: TL = TL.Replace("[", "{");
	#: TL = TL.Replace("]", "}");
	
	#: TT = TT.Replace(procesando, i.ToString() + " iteraciones.\n\n");

	#Trace (LATEX ? TL : TT)
	
	return eb;
}

string ToN(big n)
{
	string res = n.ToString();

	if (n < 0)
		res = "(" + res + ")";
	
	return res;
}
	
#Return EqBezout(a, b);		


bool EsCap(big x)
{
	string s = x.ToString();
	int n = s.Length;

	for (int i = n / 2 - 1; i >= 0; i--)
		if (s[i] != s[n - 1 - i])
			return false;
	return true;
}

big SigCapX(big x)
{
	// No lo intentes con x grande
	while (!EsCap(x))
		x++;
	return x;
}
big ProxCap(big x)
{
	char[] d = x.ToString().ToCharArray();
	int n = d.Length;

	// Busca el capicúa más cercano, copiando el lado izquerdo en el derecho
	for (int i = n / 2 - 1; i >= 0; i--)
		d[n - 1 - i] = d[i];

	return new big(new string(d));
}

big SigCap(big x)
{
	char[] d = x.ToString().ToCharArray();
	int n = d.Length;

	// Primero busca el capicúa más cercano, copiando el lado izquerdo en el derecho
	for (int i = n / 2 - 1; i >= 0; i--)
		d[n - 1 - i] = d[i];
	big c = new big(new string(d));
		
	// Si el más cercano es mayor o igual que x, lo retorna
	if (c >= x) return c;

	// Si el más cercano es menor que x busca el capicúa siguiente, 
	// incrementando adecuadamente los dígitos, desde el centro hacia afuera
	else SigCap(d);

	// Retorna el nuevo número
	return new big(new string(d));
}

/// <summary>
/// Busca el capicúa siguiente, incrementando adecuadamente los dígitos, desde el centro hacia afuera
/// </summary>
/// <param name="d"></param>
void SigCap(char[] d)
{
	int n = d.Length;

	// Si n es impar, c = n/2 = (n-1)/2 es la posición del dígito central 
	//  0   1   2   3   4 
	//          c  
	// si n es par y c = n/2-1, entonces c, c+1 son los dígitos centrales
	//  0   1   2   3   4   5 
	//          c  c+1

	int c = ((n & 0x01) == 0 ? n / 2 - 1 : n / 2);
	
	for (int i = c; i >= 0; i--)
	{
		if (d[i] != '9')
		{
			d[i]++;
			d[n - 1 - i] = d[i];
			break;
		}
		else
		{
			d[i] = '0';
			d[n - 1 - i] = d[i];
		}
	}
}

bool test()
{
	bool ret = true;
	#Trace clear
	for (int i = 1; i < 1000; i++)
	{
		big n = big.random(1000, 100000000); 
		big c = SigCap(n);
		big cx = SigCapX(n);
		if (c != cx) ret = false;
		#Trace "x=", n, "\tc=", c, "\tcx=", cx, "\t", (c == cx), "\n";
	}
	return ret;
}





#R test();
// Congruencias lineales
// Resuelve la ecuación a·x ≡ b (mod n) 
// La sintaxis es Solve(a, b, c)

string Solve(big a, big b, big n)
{
	#Trace clear
	if (a <=0 || b <=0 || n <=1)
		return "Parámetros incorrectos";
			
	bool ok = true;
	big[] bez = bezout(a, n);
	big d = bez[2];


	if (b % d != 0)
		ok = false;
	
	if (ok)
	{
		#Trace "La ecuación es a·x ≡ b (mod n), con ", "a=", a, ", b=", b, ", n=", n, ". Entonces "
		#Trace "d = m", "cd(a, n) = ", d, ".\n"
		if (!ok)
		{
			#Trace "Como d=", d, " no divide a ", b, " no hay soluciones."
		}
		else
		{
			if (d == n)
			{
				#Trace "la equación es equivalente a 0·x ≡ 0 (mod n), hay ", n, " soluciones: 0 ≤ x ≤ ", n-1
			} 	
			else if (d == 1)
			{
				#Trace "La igualdad de Bézout para (a, n) es α·", a, " + β·", n, " = ", d, " con α=", bez[0], ", β=", bez[1], "\n"

				#Trace "\nHay ", 1, " solución en Z", n, ": \n"
				#Trace "x = α·b  mod n + k·n, con k entero => x= ", (bez[0]*b/d), " + k·", n/d,  ", con k entero." 

			}
			else // αβ
			{ 
				#Trace "La igualdad de Bézout para (a, n) es α·", a, " + β·", n, " = ", d, " con α=", bez[0], ", β=", bez[1], "\n"

				#Trace "\nHay ", d, " soluciones en Z", n, ": \n"
				#Trace "x = α·b/d  mod n/d + k·n/d, con k entero => x= ", (bez[0]*b/d), " + k·", n/d,  ", con k entero." 
			}
		}
	}



	if (!ok)
		return "Sin solución";
	else if (d == n)
		return "0 ≤ x ≤ " + (n-1).ToString();
	else
		return (bez[0]*b/d ).ToString() + " + " + (n/d).ToString() + "·k";
}


// Resuelve la ecuación a·x ≡ b (mod n) 
// La sintaxis es Solve(a, b, c)

#return (Solve(12,9,15));
//
// Resuelve la ecuación a·x ≡ b (mod n) 
// Da las soluciones enteras y reduce la ecuación a 
// una de la forma x ≡ s (mod m).
// 
// Introduce aquí los valores de a, b, n:
//

big a = new big("216");
big b = new big("288");
big n = new big("336");

// ---------------------------------------------------------------------------

string Solve(big a, big b, big n)
{
	#Trace clear
	if (a <= 0 || b <= 0 || n <= 1)
		throw new Exception("Parámetros incorrectos: deben ser positivos y n>1.");
			
	bool ok = true;
	big d = mcd(a,n);
	big a1 = 0, b1 = 0, n1 = 0;
	big x0 = 0;

	if (b % d != 0)
		ok = false;
	
	if (ok)
	{
		a1 = a / d;
		b1 = b / d;
		n1 = n / d;

		x0 = (modinv(a1, n1) * b1) % n1;
	}
	
	#: TraceText TT = new TraceText();
	#: TT.Add("La ecuación es a·x ≡ b (mod n), con a=", a, ", b=", b,  ", n=", n, ".\nEntonces ");
	#: TT.Add("d=m", "cd(a, n)=", d, ". \nComo ");
	#: if (!ok)
	#: {
	#: 	TT.Add("d=", d, " no divide a ", b, " no hay soluciones.");
	#: }
	#: else
	#: {
	#: 	if (d == n)
	#: 	{
	#: 		TT.Add("la ecuación es equivalente a 0·x ≡ 0 (mod n) y hay ", n, " soluciones: 0 ≤ x ≤ ", n-1);
	#: 	} 	
	#: 	else if (d == 1)
	#: 	{
	#: 		TT.Add("d=1, hay una solución única en Z", n, ": \n");
	#: 		TT.Add("\nx=a^(-1)·b mod n = ", (modinv(a, n) * b) % n);  
	#: 	}
	#: 	else
	#: 	{ 
	#: 		TT.Add("d=", d, " divide a b=", b, ", ");
	#: 		TT.Add("hay ", d, " soluciones en Z", n, ": \n");
	#: 		TT.Add("dividiendo por ",  d, ", la ecuación queda ");
	#: 		if (a1 == 1) 
	#: 			TT.Add("x ≡ ", b1, " (mod ", n1, ") \ny las soluciones son ", x0, " + k·", n1,  ", con 0 ≤ k ≤ ", d-1); 
	#: 		else
	#: 			TT.Add(a1, "·x ≡ ", b1, " (mod ", n1, ") \ny las soluciones son \n\nx = (", a1, "^{-1}·", b1, ") mod ", n1, " + k·", n1, " = ", x0, " + k·", n1,  ", con 0 ≤ k ≤ ", d-1); 
	#: 	}
	#: }
	
	
	
	#: TraceText TL = new TraceText();
	#: TL.Add("% ---------- LIMEX: solución obtenida con 'Congruencias lineales.cs' ----------\n\n");

	#: TL.Add("La ecuación es $a \\cdot  x\\equiv b\\ (\\Mod n)$, con $a=",  a, ", b=", b, ", n=", n, "$. Entonces $d=\\m");
	#: TL.Add("cd(a, n)=", d, "$.\n\\vspace*{2ex}\n\n");
	#: if (!ok)
	#: {
	#: 	TL.Add("Como $d=", d, "$ no divide a $", b, "$, no hay soluciones.\n\n");
	#: }
	#: else
	#: {
	#: 	if (d == n)
	#: 	{
	#: 		TL.Add("La equación es equivalente a $0\\cdot x \\equiv 0\\ (\\Mod n)$, hay $", n, "$ soluciones: $0 <= x <= ", n-1, "$.\n\n");
	#: 	} 	
	#: 	else if (d == 1)
	#: 	{
	#: 		TL.Add("Como $d=1$, hay una solución única en $\\Z_{", n, "}$: \n");
	#: 		TL.Add("\n$$x = a^{-1}\\cdot b\\ \\Mod n = ", (modinv(a, n) * b) % n, "$$\n\n");
	#: 	}
	#: 	else
	#: 	{
	#: 		TL.Add("Como $d=", d, "$ divide a $b=", b, "$, hay ", d, " soluciones en $\\Z_{", n, "}$: dividiendo por ", d, ", la ecuación queda ");
	#: 		TL.Add("$", a1, "\\cdot x\\equiv ", b1, "\\ (\\Mod ", n1, ")$ \ny las soluciones son ");
	#: 		TL.Add("$$ x=(", a1, "^{-1}\\cdot ", b1, ") \\Mod ", n1, "+ k·", n1, "=", x0, "+ k\\cdot ", n1, "\\text{, con } 0<= k <=", d-1, "$$\n\n");
	#: 	}
	#: }
	#: TL.Add("% ---------- LIMEX: fin de la solución ----------------------------------------\n\n");

	#Trace (LATEX ? TL : TT);

	if (!ok)
		return "Sin solución";
	else if (d == n)
		return "0 ≤ x ≤ " + (n-1).ToString();
	else if (d == 1)
		return x0.ToString();
	else
		return "x = " + x0.ToString() + " + " + n1.ToString() + "·k, con 0 ≤ k ≤ " + (d-1).ToString() + 
			"\nx ≡ " + x0.ToString() + " (mod " + n1.ToString() + ")";
}


// Resuelve la ecuación a·x ≡ b (mod n) 
// La sintaxis es Solve(a, b, n)

#return Solve(a, b, n);
//
// Algoritmo que mediante fuerza bruta enumera todas las RC1 en Zn.
//
// Introduce aquí el valor de n:

int n = 2*23;

// -------------------------------------------------------------------------------

public int CuentaRC1(int n)  
{
	#Trace clear

	int k = 0;	// Para contar las raíces

	if (n == 2)
	{
		k++;
		#Trace string.Format("{0:000}: \t{1:d}\n", k, 1);
	}

	else
	{
		int m = n/2;			// si r<m entonces n-r>m, por lo que no hay que explorar más que la mitad.
		for (int r = 1; r <= m; r++)	// nótese que aunque n fuese par, m no puede ser RC1 a menos que n=2.
		{ 
			if ((r * r) % n == 1)
			{
				k++; 	// r
				#Trace string.Format("{0:000}: \t {1:d}\n", k, r);				
				k++;		// -r
				#Trace string.Format("{0:000}: \t{1:d} \t= {2:d}\n\n", k, -r, n - r);
			}
		}
	}

	return k;
	
}

#Return(CuentaRC1(n));big k = 1; // contador de llamadas a cuentau.

big cuentau(big n)
{
	#T "Llamada nº ", k, " con n=", n
	if (n == 1 || n== 2)
	{
		#T " (como n=", n, " no se incrementará el contador)\n"
		return 0; // no se vuelve a llamar a sí misma
	}
	#T "\n"
	k++; 
	cuentau (n - 1);
	cuentau (n - 2);
	return k;
}

#return cuentau(7)double probDif(int n) 
{
	int i = 0;
	double r = 1.0;

	double dias = 365.0;

	#trace clear

	#trace "La probabilidad de colisión es: \n"
	while (i < n)
	{
		r *= ((dias - i) / dias);
		if (i == n-1)
			#Trace r, "\n"
		else
			#trace r, " *\n" 
		i++;
	}
	return r;
}

double probCol(int n)
{
	return 1 - probDif(n);
}
			
#Return (probCol(23));

///
/// Solución de la ecuación diofántica a·x + b·y = c.
///
/// Introduce aquí los valores de a, b, c:
///

big a = 7*110;
big b = 7*19;
big c = 7*1200;

bool reduce = false;

///
/// ---------------------------------------------------------------------------------


string Solve(big a, big b, big c)
{
	#Trace clear

	#: if (LATEX)
	#: {
		#Trace "\n%------------- Solución generada por LIMEX \n\n"
		#Trace "Solución de la ecuación diofántica $",  ToN(a), "\\cdot x+", ToN(b), "\\cdot y = ", ToN(c), "$, en la que\n" 
		#Trace "llamamos $a=", a, ", b=", b, ", c=", c, "$.\n\\Par\n"
	#: }
	#: else
	#: {
		#Trace "Solución de la ecuación diofántica ",  ToN(a), "·x+", ToN(b), "·y = ", ToN(c), "\n" 
		#Trace "a=", a, ", b=", b, ", c=", c, ".\n\n"
	#: }

	if (a == 0 || b == 0)
		throw(new Exception("Los coeficientes no pueden ser nulos."));
	

	// mcd de a y b:
	big d = mcd(a, b);

	#: if (LATEX)
		#Trace "Obtenemos $d=\\m", "cd(", a, ", ", b, ")=", d, "$.\n" 
	#: else
		#Trace "Obtenemos d=m", "cd(", a, ", ", b, ")=", d, ".\n"
	

	// si d no divide a c, no hay soluciones
	if (c.abs() % d != 0)
	{
		#: if (LATEX)
			#Trace "Como $d=", d, "$  no divide a $c=", c, "$, no hay soluciones.\n\n";
		#: else
			#Trace  "Como d=", d, " no divide a c=", c, ", no hay soluciones.\n\n";

		return null;
	}
	#: else
	#: {
		#: if (LATEX)
			#Trace "Como $d=", d, "$  divide a $c=", c, "$, hay infinitas soluciones enteras.\n\n";
		#: else
			#Trace  "Como d=", d, " divide a c=", c, ", hay soluciones.\n\n";

	#: }


	if (d != 1)
	{
		a /= d;
		b /= d;
		c /= d;
		#: if (LATEX)
		#: {
			#Trace "Dividimos la ecuación entre $d=", d, "$ y obtenemos "  
			#Trace "$", ToN(a), "\\cdot x+", ToN(b), "\\cdot y=", ToN(c), "$.\n" 
			#Trace "Ahora $a=", a, ", b=", b, ", c=", c, "$.\n\\Par\n"
		#: }
		#: else
		#: {
			#Trace "Dividimos la ecuación entre d=", d, " y obtenemos "  
			#Trace ToN(a), "·x+", ToN(b), "·y=", ToN(c), ".\n" 
			#Trace "Ahora a=", a, ", b=", b, ", c=", c, ".\n\n"
		#: }
	}

	big[] eb = bezout(a, b); 


	#: if (LATEX)
		#Trace "Calculamos la igualdad de Bézout para $a=", a, "$ y $b=", b, "$. Para ello,\n"
	#: else
		#Trace "Calculamos la igualdad de Bézout para a=", a, ", b=", b, ". Para ello\n"

	EqBezout(a,b);

	big alfa = eb[0] * c;
	big beta = eb[1] * c;

      SolED solG = new SolED(alfa, beta, b, -a);

	big[] coef_X_G = { alfa, (a >= 0 ? 1 : -1) * b.abs() };
	big[] coef_Y_G = { beta, (b >= 0 ? -1 : 1) * a.abs() };

	// Reduce la solución para x
	SolED solX = solG.ReduceX();
	big qX = solG.Px.Floor(solG.Vx);
      
	// Reduce la solución para y
      SolED solY = solG.ReduceY();
	big qY = solG.Py.Floor(solG.Vy);

	#: if (LATEX)
	#: {

		#Trace "y la multiplicamos por $c=", c, "$, obteniendo $" 
		#Trace ToN(alfa), "\\cdot ", ToN(a), "+" , ToN(beta), "\\cdot ", ToN(b), "=", c, "$\n";

		#Trace "(ya tenemos una solución particular:", "$x_0=", alfa, ", y_0=" , beta, "$)\n\n";

		#Trace "En el primer miembro de la igualdad, sumamos y restamos $|a\\cdot b|\\cdot  k =", a.abs(), "\\cdot ", b.abs(), "\\cdot  k $ y tenemos \n"
		#Trace "$$ \\underblock{",  ToN(alfa), "\\cdot ", ToN(a), "+", a.abs(), "\\cdot ", b.abs(), "\\cdot  k )}_{\\text{factor común }",  a, "} +  \\underblock{" , ToN(beta), "\\cdot ", ToN(b), "-", a.abs(), "\\cdot ", b.abs(), "\\cdot  k }_{\\text{factor común }", b, "}=", c, "$$\n";
		#Trace "Sacando factores comunes tenemos\n"
		#Trace "$$ (",  alfa, (a>=0 ? "+" : "-"), b.abs(), "\\cdot k  )\\cdot ", ToN(a), "+ (" , beta, (b>=0 ? "-" : "+"), a.abs(),  "\\cdot k  ))\\cdot ", ToN(b), "=", c, "$$\n\n";
		
		#Trace "De aquí la solución general es\n"
		#Trace solG.ToLatexString();

		if (reduce) 
		{

			#Trace "\n\n\\vspace*{2ex}\n\n"
			#Trace "\n\nSi $r = ", solG.Px, "\\ \\MMod  ", solG.Vx, " = ", solG.Px.Mod(solG.Vx),  ",\\; "
			#Trace "q = \\f", "loor{\\frac{", solG.Px, "}{", solG.Vx, "}}=", solG.Px.Floor(solG.Vx), "$,\nreducimos la expresión obtenida para $x$ sustituyendo "
			#Trace "$$k \\", "right", "arrow k - q = k ", Utils.ToStringWithSign(-qX), "$$\n"
      	     	#Trace "y obtenemos la solución equivalente:\n"
			#Trace solX.ToLatexString();

			#Trace "\n\n\\vspace*{2ex}\n\n"
			#Trace "\n\nSi $r = ", solG.Py, "\\ \\MMod  ", solG.Vy, " = ", solG.Py.Mod(solG.Vy),  ",\\; "
			#Trace "q = \\f", "loor{\\frac{", solG.Py, "}{", solG.Vy, "}}=", solG.Py.Floor(solG.Vy), "$,\nreducimos la expresión obtenida para $y$ sustituyendo "
			#Trace "$$k \\", "right", "arrow k - q = k ", Utils.ToStringWithSign(-qY), "$$\n"
      	     	#Trace "y obtenemos la solución equivalente:\n"
			#Trace solY.ToLatexString();
		}
		 
		#Trace "\n\n%------------- Fin de la solución generada por LIMEX \n"
	#: }
	#: else
	#: {
		#Trace "y la multiplicamos por c=", c, " : " 
		#Trace ToN(alfa), "·", ToN(a), "+" , ToN(beta), "·", ToN(b), "=", c, "\n";

		#Trace "(ya tenemos una solución particular:", "x=", alfa, ", y=" , beta, ")\n\n";

		#Trace "En el primer miembro de la igualdad, sumamos y restamos |a·b|·k=", a.abs(), "·", b.abs(), "·k: \n"

		#Trace ToN(alfa), "·", ToN(a), "+", a.abs(), "·", b.abs(), "·k+" , ToN(beta), "·", ToN(b), "-", a.abs(), "·", b.abs(), " k=", c, "\n";
		#Trace "En los dos primeros sumandos sacamos factor común a=", a, " y en los dos segundos, b=", b," y tenemos\n"
		#Trace "(",  alfa, (a>=0 ? "+" : "-"), b.abs(), " k)+", ToN(a), "+(" , beta, (b>=0 ? "-" : "+"), a.abs(),  "·k)·", ToN(b), "=", c, "\n\n";

		#Trace "La solución general es: \n"
		#Trace solG.ToString()

		#Trace "\n\nCalculamos r = ", solG.Px, " mod ", solG.Vx, " = ", solG.Px.Mod(solG.Vx),  ", "
		#Trace "q = cociente de dividir ", solG.Px, " entre ", solG.Vx, " = ", solG.Px.Floor(solG.Vx), ".\nHaciendo "
		#Trace "k -> k - q = k ", Utils.ToStringWithSign(-qX), ", "
           	#Trace "obtenemos la solución equivalente: \n"
		#Trace solX.ToString()

		#Trace "\n\nCalculamos r = ", solG.Py, " mod ", solG.Vy, " = ", solG.Py.Mod(solG.Vy),  ", "
		#Trace "q = cociente de dividir ", solG.Py, " entre ", solG.Vy, " = ", solG.Py.Floor(solG.Vy), ".\nHaciendo "
		#Trace "k -> k - q = k ", Utils.ToStringWithSign(-qY), ", "
           	#Trace "obtenemos la solución equivalente: \n"
		#Trace solY.ToString()
	#: }

	return "";

}

big EqBezout(big a, big b)
{
	#: string TT = "";
	#: string TL = "";


	big a0 = a;
	big b0 = b;

	#: big[] eb = bezout(a, b);

	#: int i = 1;

	big r = 1;
	big c = 0;
	big d = 0;

	// Trabajamos con positivos; los signos se ajustarán al final:
	a = a.abs();
	b = b.abs();

	#: TT += "hacemos las sucesivas divisiones del \nAlgoritmo de Euclides para calcular d=m" + "cd(a,b):\n\n";
	#: TL += "hacemos las sucesivas divisiones del \nAlgoritmo de Euclides para calcular $d=\\m" + "cd(a,b)$:\n\n";

	#: string TTn = "", TTr = "";
	#: string TLn = "", TLr = "";


	do
	{
		d = r;
		c = a / b;
		r = a % b;

		#: TTn += string.Format("{0}={1}·{2}+{3}\n", a, c, b,r);
		#: TTr += string.Format("r{0}={1}r{2}+r{3}@",  i-2, c==1 ? "": c.ToString()+"·",  i-1,  i);

		#: TLn += string.Format("{0} 	& = & {1} 	& \\cdot 	& {2} 	& + & {3}			\\\\\n", a, c, b,r);
		#: TLr += string.Format("r_[{0}] 	& = & {1}	& \\cdot 	& r_[{2}] 	& + & r_[{3}]\\\\@",  i-2, c,  i-1,  i);


		a = b;
		b = r;

		#: i++;
	}
	while (r>0);

	#: // Texto: ------------------------------------------------
	#:
	#: // La tabla de divisiones numéricas 
	#: TT += TTn;
	#:
	#: // La tabla de divisiones simbólicas 
	#: TTr = TTr.Replace("r-1", "a");
	#: TTr = TTr.Replace("r0", "b");
	#: TTr = TTr.Replace("r"+(i-2).ToString(), "d");
	#: TTr = TTr.Replace("a=0·b+r1", "a=r1");
	#:		
	#: TT += "\nSi llamamos ";
	#: for (int j=1; j <=i-3; j++)
	#:	TT += "r" + j.ToString() + (j==i-3 ?  "" : ", ");
	#: TT += " a los restos, \nlas igualdades anteriores quedan\n\n";
	#:
	#: string[] TTLines = TTr.Split('@');
	#: for (int j = 0; j < TTLines.Length - 2; j++)
	#:	TT += (TTLines[j]+"\n");
	#:
	#: // Despejamos:
	#: TT+= "\nDespejamos d de la última igualdad y \neliminamos los restos ";
	#: for (int j=i-3; j>=1; j--)
	#:	TT += "r" + j.ToString() + (j==1 ? " " :", ");
	#: TT += "usando las igualdades anteriores \n(de abajo hacia arriba), con lo que \nexpresamos d como combinación lineal de a y b:\n";
	#: TT += string.Format("{0:s}·a+{1:s}·b=d\n", ToN(eb[0]), ToN(eb[1]));
	#: TT += "y así obtenemos la igualdad de Bézout:\n";
	#: TT += string.Format("{0:s}·{1:s}+{2:s}·{3:s}={4:s}\n", ToN(eb[0]) ,ToN(a0), ToN(eb[1]), ToN(b0), eb[2]);

	#: // Latex: ------------------------------------------------
	#:
	#: // La tabla de divisiones numéricas 
	#: TL += "$$\n\\begin{array}{rcrcrcr}\n" + TLn + "\\end{array}\n$$\n";
	#:
	#: // La tabla de divisiones simbólicas 
	#: TLr = TLr.Replace("r_[-1]", "a");
	#: TLr = TLr.Replace("r_[0]", "b");
	#: TLr = TLr.Replace("r_["+(i-2).ToString()+"]", "d");
	#: TLr = TLr.Replace("& 1 & \\cdot", "&&");
	#: TLr = TLr.Replace("0 & \\cdot  & b  & +", " &  &   & ");
	#:		
	#:		
	#: TL += "\nSi llamamos $";
	#: for (int j=1; j <=i-3; j++)
	#:	TL += "r_[" + j.ToString() + "]" + (j==i-3 ?  "" : ", ");
	#: TL += "$ a los restos, \nlas igualdades anteriores quedan\n\n";
	#:
	#: TL += "$$\n\\begin{array}{ccccccc}\n";
	#: string[] TLLines = TLr.Split('@');
	#: for (int j = 0; j < TLLines.Length - 2; j++)
	#:	TL += (TLLines[j]+"\n");
	#: TL += "\\end{array}\n$$\n";
	#:
	#: // Despejamos:
	#: TL+= "\nDespejamos $d$ de la última igualdad y \neliminamos los restos $";
	#: for (int j=i-3; j>=1; j--)
	#:	TL += "r_[" + j.ToString() + "]" + (j==1 ? "" :", ");
	#: TL += "$ usando las igualdades anteriores \n(de abajo hacia arriba), con lo que \nexpresamos $d$ como combinación lineal de $a$ y $b$:\n";
	#: TL += string.Format("$$d={0:s}\\cdot a+{1:s}\\cdot b$$\n", ToN(eb[0]), ToN(eb[1]));
	#: TL += "y así obtenemos la igualdad de Bézout:\n";
	#: TL += string.Format("$${4:s}={0:s}\\cdot {1:s}+{2:s}\\cdot {3:s}$$\n", ToN(eb[0]) ,ToN(a0), ToN(eb[1]), ToN(b0), eb[2]);
	#: TL = TL.Replace("[", "{");
	#: TL = TL.Replace("]", "}");
	

	#Trace (LATEX ? TL : TT)
	
	return d;
}

string ToN(big n)
{
	string res = n.ToString();

	if (n < 0)
		res = "(" + res + ")";
	
	return res;
}

string ToStringWithSign(big x)
{
	string res = "";
	if (x < 0)
		res = " - " + x.abs().ToString();
	else
		res = " + " + x.abs().ToString();

	return res;
}

#Return Solve(a, b,c);
///
/// Solución de la ecuación diofántica a·x + b·y = c.
///
/// Introduce aquí los valores de a, b, c:
///

big a = 4;
big b = -1;
big c = 7;

///
/// ---------------------------------------------------------------------------------

bool reducir = false;

string Solve(big a, big b, big c)
{
	#Trace clear

	#: if (LATEX)
	#: {
		#Trace "\n%--------- Solución generada por LIMEX \n"
		#Trace "Solución de la ecuación diofántica $",  ToN(a), "\\cdot x+", ToN(b), "\\cdot y = ", ToN(c), "$, en la que\n" 
		#Trace "llamamos $a=", a, ", b=", b, ", c=", c, "$.\n\\Par\n"
	#: }
	#: else
	#: {
		#Trace "Solución de la ecuación diofántica ",  ToN(a), "·x+", ToN(b), "·y = ", ToN(c), "\n" 
		#Trace "a=", a, ", b=", b, ", c=", c, ".\n\n"
	#: }

	if (a == 0 || b == 0)
		throw(new Exception("Los coeficientes no pueden ser nulos."));
	

	// mcd de a y b:
	big d = mcd(a, b);

	#: if (LATEX)
		#Trace "Obtenemos $d=\\m", "cd(", a, ", ", b, ")=", d, "$.\n" 
	#: else
		#Trace "Obtenemos d=m", "cd(", a, ", ", b, ")=", d, ".\n"
	

	// si d no divide a c, no hay soluciones
	if (c.abs() % d != 0)
	{
		#: if (LATEX)
			#Trace "Como $d=", d, "$  no divide a $c=", c, "$, no hay soluciones.\n\n";
		#: else
			#Trace  "Como d=", d, " no divide a c=", c, ", no hay soluciones.\n\n";

		return null;
	}
	#: else
	#: {
		#: if (LATEX)
			#Trace "Como $d=", d, "$  divide a $c=", c, "$, hay infinitas soluciones enteras.\n\n";
		#: else
			#Trace  "Como d=", d, " divide a c=", c, ", hay soluciones.\n\n";

	#: }


	if (d != 1)
	{
		a /= d;
		b /= d;
		c /= d;
		#: if (LATEX)
		#: {
			#Trace "Dividimos la ecuación entre $d=", d, "$ y obtenemos "  
			#Trace "$", ToN(a), "\\cdot x+", ToN(b), "\\cdot y=", ToN(c), "$.\n" 
			#Trace "Ahora $a=", a, ", b=", b, ", c=", c, "$.\n\\Par\n"
		#: }
		#: else
		#: {
			#Trace "Dividimos la ecuación entre d=", d, " y obtenemos "  
			#Trace ToN(a), "·x+", ToN(b), "·y=", ToN(c), ".\n" 
			#Trace "Ahora a=", a, ", b=", b, ", c=", c, ".\n\n"
		#: }
	}

	big[] eb = bezout(a, b); 


	#: if (LATEX)
		#Trace "Calculamos la igualdad de Bézout para $a=", a, "$ y $b=", b, "$. Para ello,\n"
	#: else
		#Trace "Calculamos la igualdad de Bézout para a=", a, ", b=", b, ". Para ello\n"

	EqBezout(a,b);

	big alfa = eb[0] * c;
	big beta = eb[1] * c;

       SolED solG = new SolED(alfa, beta, b, -a);

	big[] coef_X_G = { alfa, (a >= 0 ? 1 : -1) * b.abs() };
	big[] coef_Y_G = { beta, (b >= 0 ? -1 : 1) * a.abs() };

	// Reduce la solución para x
	SolED solX = solG.ReduceX();
	big qX = solG.Px.Floor(solG.Vx);
      
	// Reduce la solución para y
      SolED solY = solG.ReduceY();
	big qY = solG.Py.Floor(solG.Vy);

	#: if (LATEX)
	#: {

		#Trace "y la multiplicamos por $c=", c, "$, obteniendo $" 
		#Trace ToN(alfa), "\\cdot ", ToN(a), "+" , ToN(beta), "\\cdot ", ToN(b), "=", c, "$\n";

		#Trace "(ya tenemos una solución particular:", "$x_0=", alfa, ", y_0=" , beta, "$)\n\n";
		
		#Trace "De aquí la solución general es\n"
		#Trace solG.ToLatexString();

		if (reducir)
		{

			#Trace "\n\n\\vspace*{2ex}\n\n"
			#Trace "\n\nSi $r = ", solG.Px, "\\ \\MMod  ", solG.Vx, " = ", solG.Px.Mod(solG.Vx),  ",\\; "
			#Trace "q = \\f", "loor{\\frac{", solG.Px, "}{", solG.Vx, "}}=", solG.Px.Floor(solG.Vx), "$,\nreducimos la expresión obtenida para $x$ sustituyendo "
			#Trace "$$k \\", "right", "arrow k - q = k ", Utils.ToStringWithSign(-qX), "$$\n"
      	     	#Trace "y obtenemos la solución equivalente:\n"
			#Trace solX.ToLatexString();

			#Trace "\n\n\\vspace*{2ex}\n\n"
			#Trace "\n\nSi $r = ", solG.Py, "\\ \\MMod  ", solG.Vy, " = ", solG.Py.Mod(solG.Vy),  ",\\; "
			#Trace "q = \\f", "loor{\\frac{", solG.Py, "}{", solG.Vy, "}}=", solG.Py.Floor(solG.Vy), "$,\nreducimos la expresión obtenida para $y$ sustituyendo "
			#Trace "$$k \\", "right", "arrow k - q = k ", Utils.ToStringWithSign(-qY), "$$\n"
      	     	#Trace "y obtenemos la solución equivalente:\n"
			#Trace solY.ToLatexString();
		}
		 
		#Trace "\n\n%------Fin de la solución generada por LIMEX \n"
	#: }
	#: else
	#: {
		#Trace "y la multiplicamos por c=", c, " : " 
		#Trace ToN(alfa), "·", ToN(a), "+" , ToN(beta), "·", ToN(b), "=", c, "\n";

		#Trace "(ya tenemos una solución particular:", "x=", alfa, ", y=" , beta, ")\n\n";

		#Trace "La solución general es: \n"
		#Trace solG.ToString()
		if (reducir)
		{

			#Trace "\n\nCalculamos r = ", solG.Px, " mod ", solG.Vx, " = ", solG.Px.Mod(solG.Vx),  ", "
			#Trace "q = cociente de dividir ", solG.Px, " entre ", solG.Vx, " = ", solG.Px.Floor(solG.Vx), ".\nHaciendo "
			#Trace "k -> k - q = k ", Utils.ToStringWithSign(-qX), ", "		
   	        	#Trace "obtenemos la solución equivalente: \n"
			#Trace solX.ToString()

			#Trace "\n\nCalculamos r = ", solG.Py, " mod ", solG.Vy, " = ", solG.Py.Mod(solG.Vy),  ", "
			#Trace "q = cociente de dividir ", solG.Py, " entre ", solG.Vy, " = ", solG.Py.Floor(solG.Vy), ".\nHaciendo "
			#Trace "k -> k - q = k ", Utils.ToStringWithSign(-qY), ", "
	           	#Trace "obtenemos la solución equivalente: \n"
			#Trace solY.ToString()
		}

	#: }
	return "";

}

big EqBezout(big a, big b)
{
	#: string TT = "";
	#: string TL = "";


	big a0 = a;
	big b0 = b;

	#: big[] eb = bezout(a, b);

	#: int i = 1;

	big r = 1;
	big c = 0;
	big d = 0;

	// Trabajamos con positivos; los signos se ajustarán al final:
	a = a.abs();
	b = b.abs();

	#: TT += "hacemos las sucesivas divisiones del \nAlgoritmo de Euclides para calcular d=m" + "cd(a,b):\n\n";
	#: TL += "hacemos las sucesivas divisiones del \nAlgoritmo de Euclides para calcular $d=\\m" + "cd(a,b)$:\n\n";

	#: string TTn = "", TTr = "";
	#: string TLn = "", TLr = "";


	do
	{
		d = r;
		c = a / b;
		r = a % b;

		#: TTn += string.Format("{0}={1}·{2}+{3}\n", a, c, b,r);
		#: TTr += string.Format("r{0}={1}r{2}+r{3}@",  i-2, c==1 ? "": c.ToString()+"·",  i-1,  i);

		#: TLn += string.Format("{0} 	& = & {1} 	& \\cdot 	& {2} 	& + & {3}			\\\\\n", a, c, b,r);
		#: TLr += string.Format("r_[{0}] 	& = & {1}	& \\cdot 	& r_[{2}] 	& + & r_[{3}]\\\\@",  i-2, c,  i-1,  i);


		a = b;
		b = r;

		#: i++;
	}
	while (r>0);

	#: // Texto: ------------------------------------------------
	#:
	#: // La tabla de divisiones numéricas 
	#: TT += TTn;
	#:
	#: // La tabla de divisiones simbólicas 
	#: TTr = TTr.Replace("r-1", "a");
	#: TTr = TTr.Replace("r0", "b");
	#: TTr = TTr.Replace("r"+(i-2).ToString(), "d");
	#: TTr = TTr.Replace("a=0·b+r1", "a=r1");
	#:		
	#: TT += "\nSi llamamos ";
	#: for (int j=1; j <=i-3; j++)
	#:	TT += "r" + j.ToString() + (j==i-3 ?  "" : ", ");
	#: TT += " a los restos, \nlas igualdades anteriores quedan\n\n";
	#:
	#: string[] TTLines = TTr.Split('@');
	#: for (int j = 0; j < TTLines.Length - 2; j++)
	#:	TT += (TTLines[j]+"\n");
	#:
	#: // Despejamos:
	#: TT+= "\nDespejamos d de la última igualdad y \neliminamos los restos ";
	#: for (int j=i-3; j>=1; j--)
	#:	TT += "r" + j.ToString() + (j==1 ? " " :", ");
	#: TT += "usando las igualdades anteriores \n(de abajo hacia arriba), con lo que \nexpresamos d como combinación lineal de a y b:\n";
	#: TT += string.Format("{0:s}·a+{1:s}·b=d\n", ToN(eb[0]), ToN(eb[1]));
	#: TT += "y así obtenemos la igualdad de Bézout:\n";
	#: TT += string.Format("{0:s}·{1:s}+{2:s}·{3:s}={4:s}\n", ToN(eb[0]) ,ToN(a0), ToN(eb[1]), ToN(b0), eb[2]);

	#: // Latex: ------------------------------------------------
	#:
	#: // La tabla de divisiones numéricas 
	#: TL += "$$\n\\begin{array}{rcrcrcr}\n" + TLn + "\\end{array}\n$$\n";
	#:
	#: // La tabla de divisiones simbólicas 
	#: TLr = TLr.Replace("r_[-1]", "a");
	#: TLr = TLr.Replace("r_[0]", "b");
	#: TLr = TLr.Replace("r_["+(i-2).ToString()+"]", "d");
	#: TLr = TLr.Replace("& 1 & \\cdot", "&&");
	#: TLr = TLr.Replace("0 & \\cdot  & b  & +", " &  &   & ");
	#:		
	#:		
	#: TL += "\nSi llamamos $";
	#: for (int j=1; j <=i-3; j++)
	#:	TL += "r_[" + j.ToString() + "]" + (j==i-3 ?  "" : ", ");
	#: TL += "$ a los restos, \nlas igualdades anteriores quedan\n\n";
	#:
	#: TL += "$$\n\\begin{array}{ccccccc}\n";
	#: string[] TLLines = TLr.Split('@');
	#: for (int j = 0; j < TLLines.Length - 2; j++)
	#:	TL += (TLLines[j]+"\n");
	#: TL += "\\end{array}\n$$\n";
	#:
	#: // Despejamos:
	#: TL+= "\nDespejamos $d$ de la última igualdad y \neliminamos los restos $";
	#: for (int j=i-3; j>=1; j--)
	#:	TL += "r_[" + j.ToString() + "]" + (j==1 ? "" :", ");
	#: TL += "$ usando las igualdades anteriores \n(de abajo hacia arriba), con lo que \nexpresamos $d$ como combinación lineal de $a$ y $b$:\n";
	#: TL += string.Format("$$d={0:s}\\cdot a+{1:s}\\cdot b$$\n", ToN(eb[0]), ToN(eb[1]));
	#: TL += "y así obtenemos la igualdad de Bézout:\n";
	#: TL += string.Format("$${4:s}={0:s}\\cdot {1:s}+{2:s}\\cdot {3:s}$$\n", ToN(eb[0]) ,ToN(a0), ToN(eb[1]), ToN(b0), eb[2]);
	#: TL = TL.Replace("[", "{");
	#: TL = TL.Replace("]", "}");
	

	#Trace (LATEX ? TL : TT)
	
	return d;
}

string ToN(big n)
{
	string res = n.ToString();

	if (n < 0)
		res = "(" + res + ")";
	
	return res;
}

string ToStringWithSign(big x)
{
	string res = "";
	if (x < 0)
		res = " - " + x.abs().ToString();
	else
		res = " + " + x.abs().ToString();

	return res;
}

#Return Solve(a, b,c);
// Pon aquí el número de operaciones:
big fn = 2000000;

// Pon aquí el número de operaciones que se hacen por segundo.
// Empezamos suponiendo un millón:
big numOpS = ((big) 10)^(6);

#: /**/

// Calculamos el tiempo
public string eficiencia(big numOperaciones)
{
	// segundos
	big s = numOperaciones / numOpS ;

	// minutos
	big m = s / 60;	

	// días
	big d = m / (60*24);
 
	// años (más o menos)
	big y = d / 365;

	#trace clear
	#T "Segundos\t\t: ", s.ToString(), "\n"
	#T "Minutos    \t\t: ", m.ToString(), "\n"
	#T "Días      \t\t: ", d.ToString(), "\n"
	#T "Años     \t\t: ", y.ToString(), "\n"
	#: if (y > 1000000)
		#T "\nE.E.U.A.\t\t: ", ((big) 137)*((big) 10^8), " (Edad Estimada del Universo en Años)\n"; 
 
	return s.ToString() + " segundos";
	
}

#return eficiencia(fn)function modInv(m : big, n : big)
{
	var a = m, b = n;
	var s : big = 1; // signo.

	// La matriz se inicializa a la matriz unidad:
	var m11 : big = 1;
	var m12 : big = 0;
	var m21 : big = 0;
	var m22 : big = 1;

	while (b > 0)
	{
		var c = a / b;
		var r = a % b;
		a = b;
		b = r;
		s = -s;
		var t11 = m11 * c + m12;
		var t12 = m11;
		var t21 = m21 * c + m22;
		var t22 = m21;

		m11 = t11, m12 = t12, m21 = t21, m22 = t22;
	}

	// Se retorna un array: los dos primeros elementos son los coeficientes de la
	// igualdad de Bezout y el tercero es el mcd:
	var ret = new big[3];
	ret[0] = s * m22;
	ret[1] = - s * m12;
	ret[2] = a;	

	if (ret[2] != 1)
	{
		#T("\nNo tiene inverso");
		return 0;
	}

	else
	{
		return ret[0] % n;
	}
}
function PotMod(u : big, m : big, n: big): big
{
	var b : big = 1, t : big = u, q : big = m; 
	while (q > 0)
	{
		if (q.IsOdd())
			b = (b * t) % n;
		q = q / 2;
		if (q > 0)
			t = (t * t) % n;
	}
	return b; 
}

public class cypherText
{
	public var gamma: big = new big(0);
	public var delta: big = new big(0);
}

public class firm
{
	public var r: big = new big(0);
	public var s: big = new big(0);
}


function cifraElGamal(m: big, p: big, g: big, beta: big): cypherText
{

	var k: big = new big("1529");

	var ct = new cypherText();
	ct.gamma= PotMod(g, k, p);
	ct.delta = m * PotMod(beta, k, p) % p;
	return ct
}

function firmaElGamal(h: big, p: big, g: big, beta: big): firm
{
/*var i:big =0;
while (i < 71)
{
	#T(PotMod(g, i, p), "\n");
	i++;
}
*/

	var k: big = new big("31");

	var f = new firm();
	f.r = PotMod(g, k, p);
	var invk : big = modInv(k, p-1);
	#T("k^-1=", invk, "\n");
	#T("r=", f.r, "\n");
	f.s = (invk * (h - a * f.r )) % (p - 1);
	return f
}


function validaFirma(h:big, r:big, s:big, p: big, g: big, beta: big)
{
	var v1 : big = (PotMod(beta, r, p) * PotMod(r, s, p)) % p;
	var v2 : big = PotMod(g, h, p);
	return v1 == v2;
}

var p: big = new big("71");
var g: big = new big("7");
var a: big = new big("7");
var beta: big = PotMod(g, a, p);
var m: big = new big("13");
var h: big = new big("13");


#T("Ciframos m = ", m, ":\n");
var ct: cypherText = cifraElGamal(h, p, g, beta);
#T("cifrado = (", ct.gamma, ", ", ct.delta, ")\n");
#T("\nDesciframos:\n");
#T("(", ct.gamma, ", ", ct.delta, ")->", (PotMod(ct.gamma, p - 1 - a, p) * ct.delta) % p, "\n");

#T("\n\n\nFirmamos h = ", h, ":\n");
var f: firm = firmaElGamal(h, p, g, beta);
#T("firma = (", f.r, ", ", f.s, ")\n");

#T(validaFirma(h, 11, 16,  p, g, beta));const string chars = " abcdefghijklmnñopqrstuvwxyzç0123456789@(){}<>=+-*/%&ºª,;.:¿?¡!'";

double Entropia(string text)
{
	int[] frec = new int[chars.Length];
	double[] prob = new double[chars.Length];


	string t = text.ToLower();

	for (int i = 0; i < chars.Length; i++)
	{
		frec[i] = 0;
		for (int j = 0; j < t.Length; j++)
		{
			if (t[j] == chars[i])
			{
				frec[i] ++;
			}
		}
	}
	
	double e = 0.0;
	
	for (int i = 0; i < chars.Length; i++)
	{
		if (frec[i] > 0)
		{
			prob[i]  = ((double) frec[i]) / t.Length;
			e -= prob[i] * Math.Log(prob[i]) / Math.Log(2);
			#Trace "p(", chars.Substring(i,1), ") = ", frec[i], "/", t.Length, " = ", prob[i], "\n" 
		}
			
	}

	return e;
 
}

#Return(Entropia(chars + "a"));
big m = 1492;
big n = 1066;

big emcd(big a, big b)
{
	#trace clear
	#Trace "k es un contador de iteraciones, solo para la traza.\n\n"
	int k = 0; 
	
	big I = mcd(a, b);

	while (b > 0)
	{
		big t = b;
		b = a % b;
		a = t;
		
		k++; // solo para la traza

		#Trace "k = ", string.Format("{0:000}", k), ": I = m", "cd("
		#Trace string.Format("{0:0000000000}", a), ", "
		#Trace string.Format("{0:0000000000}", b), ") = "
		#Trace string.Format("{0:0000000000}", mcd(a,b)), "\n"
	}


	return a;
}

#Return emcd(m, n);ºbig a = 199;
big b = 98;


string Euclides(big a, big b)
{
	a = a.abs();
	b = b.abs();
	if (a == 0)
		return b.ToString();
	else if (b == 0)
		return a.ToString();
	else
	{
		if (b > a)
		{
			big t = b;
			b = a;
			a = t;
		}
	}

	string s = "";

	if (LATEX)
	{
		s+= "$$\n\\begin{array}{lclc}\n";
		while (b > 0)
		{	
			s += string.Format("\t\\m" + "cd({0}, {1}) & = & \\m" + "cd({1}, {0} mod {1}) & = \n", a, b);

			big t = a % b;
			a = b;
			b = t;
					
			if (b == 0)		
				s += string.Format("\t\\m" + "cd({0}, 0) & = & {0} & \n", a);

		}
		s+= "\\end{array}\n$$\n";
	}
		

	else
	{
		while (b > 0)
		{
			s += string.Format("m" + "cd({0}, {1}) = m" + "cd({1}, {0} mod {1}) = \n", a, b);

			big t = a % b;
			a = b;
			b = t;

			if (b == 0)		
				s += string.Format("m" + "cd({0}, 0) = {0} \n", a);

			}
	}	

	return s;
}


#Return #Trace Euclides(a, b);big euclides(int n)
{
	if (n == 1)
		return 2;
	else
	{
		big e = 1;
		for (int i = 1; i < n; i++)
			e *= euclides(i);

		return e+1;
	}

}

/*
big Fer(int n)
{
	big dos = 2;
	big dos_elevado_a_n = dos^n;

	big nf = (dos^dos_elevado_a_n)+1 ;

	FactorizeLenstra fac = new FactorizeLenstra();
	fac.Factorize(nf);
	#Trace "F_{", n, "} & = & ", nf;	
	if (fac.Factors.Count > 1)
	{
		#Trace " & = & ", fac.Factors
	}
	return nf;
}
	
*/

int test(int n)
{
	#Trace clear
	for (int i = 1; i <= n; i++)
	{
		big e = euclides(i);
		FactorizePollardRho fe = new FactorizePollardRho();
		fe.Factorize(e);
		#Trace "e_{", i, "} & = & ", e;	
	
		if (fe.Factors.Count > 1)
		{
			#Trace " & = & ", fe.Factors
		}
	
		#Trace "\\\\ \n"
	}

//	#Trace replace "·", "\\cdot "
	return 0;
}

#Return test(7);

//
// Factoriza un entero por el método de Fermat.
// Solo funcionará si el entero n es producto de dos primos cercanos.
//
// Cambia aquí el valor de n:
//
big n = 151*(151+2+3*22); //new big("308339"); //151*101; //new big ("2027651281");
//
// ------------------------------------------------------------------------------------

public string FactorizaFermat(big n)  
{
	if (n <= 1)
		throw new Exception("Debe dar un entero mayor que 1");

	#Trace clear
	big x = sqrt(n);
	if (x*x < n) x++;

	if (LATEX)
	{
		#T "%------ Solución generada por LIMEX ----------------\n"
		#T "$\\fl", "oor{\\s", "qrt{", n, "}}=", x, "$, así:\n\\Par\n"
	}

	int k = 1; // contador de intentos

	if (LATEX)
		#T "$$ \\begin{array}{cclc} \n\\text{Intento} & x & \\sq", "rt{x^2-n}	& \\\\ \\hline\n"
	else
	{
		#Trace string.Format("Intento \t\tx \t\t\ty=√(x^2-n)\n");
		#Trace "---------------------------------------------------------------------------------------\n";
	}

	big y = 0;
	while (true)
	{
		big y0 = sqrt(10000*(x*x-n))/100;
		big y1 = sqrt(10000*(x*x-n))%100;

		y = sqrt(x*x - n);
		if (y*y == x*x - n)
		{
			if (LATEX)
				#Trace string.Format("{0} & {1} & {2} & \\acierto \\\\ \n", k, x, y0);
			else
				#Trace string.Format("{0} \t\t\t{1} \t\t\t{2}\n", k, x, y0);
			break;
		}
	
		if (LATEX)
			#Trace string.Format("{0} & {1} & {2},{3}\\ldots & \\error\\\\ \n", k, x, y0, y1);
		else	
			#Trace string.Format("{0} \t\t\t{1} \t\t\t{2},{3}...\n", k, x, y0, y1);
		k++;
		x++;
	}

	if (LATEX)
	{
		#T "\\end{array}\n$$\n"
		#: string isprime1 = "\\" + "text{" + (isprobprime(x-y)?"primo":"compuesto") + "}";
		#: string isprime2 = "\\" + "text{" + (isprobprime(x+y)?"primo":"compuesto") + "}";
		#T "Por tanto la descomposición obtenida es \n"
		#T "$$", n, "\\quad=\\quad"
		#T "\\ub{\\hspace{3ex}", x-y, "\\hspace{3ex}}_{", isprime1, "}";
		#T "\\quad\\cdot\\quad";
		#T "\\ub{\\hspace{3ex}", x+y, "\\hspace{3ex}}_{", isprime2, "}$$\n";

		#T "%------ Fin de la solución generada por LIMEX ----------\n"

	}	
	else
	{
		#Trace string.Format("\n{4} = ({0}-{1}) * ({0}+{1}) = {2} * {3}\n", x, y, x-y, x+y, n);
		#Trace "(", isprobprime(x-y)?" primo ":" compuesto ", " * ", isprobprime(x+y)?" primo ":" compuesto ", ")\n"
	}
	
	return string.Format("{0} * {1}\n{2} * {3}", x-y, x+y, isprobprime(x-y)?"primo ":"compuesto", isprobprime(x+y)?"primo":"compuesto");
}

#Return FactorizaFermat(n);
/// 
/// Calcula Fibonacci(n) con un algoritmo iterativo
/// 
// Cambia aquí el valor de n:

int n = 5;


big ifib(int n) 
{
	#Trace clear
	#: string res = "Iterando:\n";
	#Trace res;
	#: TimeSpan start = new TimeSpan(DateTime.Now.Ticks);
	
	if (n <= 2)
		return 1;
	else
	{
		big a = 1;
		big b = 1;
	
		for (int i = 3; i <= n; i++)
		{
			big f = a + b;
			#trace "F(", i, ") = ", a, " + ", b, " = ", f, "\n";
		 	a = b;
		 	b = f;
		}
	
		#: TimeSpan stop = new TimeSpan(DateTime.Now.Ticks);
 		#Trace replace res, string.Format("{0} iteraciones realizadas en {1} milisegundos:\n", n-1, stop.Subtract(start).TotalMilliseconds);
		
		return b;
	}

}
#return (ifib(n));// 
// Calcula Fibonacci(n) con un algoritmo de orden logarítmico.
// https://es.wikipedia.org/wiki/Sucesión_de_Fibonacci
// 
// Cambia aquí el valor de n:

int n = 200;

// ---------------------------------------------------------------------

big lfib(int n)
{
	if (n <= 0)
		return 0;
	
	#: int k = 0; // para contar el número de iteraciones  
	#: TimeSpan start = new TimeSpan(DateTime.Now.Ticks);

	big b11 = 0, b12 = 1, b21 = 1, b22 = 1; // la matriz B 
	big p11 = 1, p12 = 0, p21 = 0, p22 = 1; // P mpieza siendo la matriz unidad de orden 2  

	n--; // tenemos que llegar hasta B^{n-1}
	
	while (n > 0)
	{	
		#: k++;

		if (n % 2 == 1) // si n es impar 
		{
			// la multiplicación: P = B * P
			big pt11 = b11 * p11 + b12 * p21;
			big pt12 = b11 * p12 + b12 * p22; 
			big pt21 = b21 * p11 + b22 * p21; 
			big pt22 = b21 * p12 + b22 * p22; 
			
			p11 = pt11; p12 = pt12; p21 = pt21; p22 = pt22; // P = Pt
			// fin de la multiplicación: P = B * P 
		}
	
		// el cuadrado: B = B^2
		big bt11 = b11 * b11 + b12 * b21; 
		big bt12 = b11 * b12 + b12 * b22; 
		big bt21 = b21 * b11 + b22 * b21; 
		big bt22 = b21 * b12 + b22 * b22;

		b11 = bt11; b12 = bt12; b21 = bt21; b22 = bt22; // B = Bt 
		// fin del cuadrado: B = B^2 
	
		n = n/2;   
	}
	
	#Trace clear
	#Trace k, " iteraciones"
	#: TimeSpan stop = new TimeSpan(DateTime.Now.Ticks);

	#Trace " en ", string.Format("{0:0.0000}", stop.Subtract(start).TotalMilliseconds), " milisegundos.";	

	return p11 + p12;  // f(n)
	
}

#return (lfib(n));// 
// Calcula Fibonacci(n) con un algoritmo de orden logarítmico.
// https://es.wikipedia.org/wiki/Sucesión_de_Fibonacci
// 
// Cambia aquí el valor de n:

int n = 20;

// ---------------------------------------------------------------------

big lfib(int n)
{
	if (n <= 0)
		return 0;
	
	n--;

	#: int k = 0; // para contar el número de iteraciones  
	#: TimeSpan start = new TimeSpan(DateTime.Now.Ticks);

	// Valores auxiliares
	big aux1 = 0, aux2 = 1;

	// Empezamos con la matriz unidad
	big a = 1, b = 0;
	big c = 0, d = 1;
	
	while (n > 0)
	{
		#: k ++;

		if (n % 2 == 1) // n es impar: Multiplica por la matriz 0111
		{
			aux1 = d * b + c * a;
			aux2 = d * (b + a) + c * b;
			a = aux1;
			b = aux2; 
		}
		
		n = n / 2;

		if (n > 0)
		{
			// Eleva al cuadrado
			aux1 = c * c + d * d;
			aux2 = d * (2 * c + d);
			c = aux1;
			d = aux2; 
		}	
	}
	
	#Trace clear
	#Trace k, " iteraciones"
	#: TimeSpan stop = new TimeSpan(DateTime.Now.Ticks);

	#Trace " en " + string.Format("{0:0.0000}", stop.Subtract(start).TotalMilliseconds) +" milisegundos.";	
	return a + b;
}

#return (lfib(n));// 
// Calcula Fibonacci(n) con un algoritmo de orden logarítmico.
// 
// Cambia aquí el valor de n:

int n = 20;

// ---------------------------------------------------------------------

big lfib(int n)
{
	if (n <= 0)
		return 0;

	int i = n-1;
	
	#: int k = 0; // para contar el número de iteraciones
	#: TimeSpan start = new TimeSpan(DateTime.Now.Ticks);

	// La matriz unidad
	big a = 1, b = 0, c = 0, d = 1;  
	
	while (i > 0)
	{
		#: k ++;

		if ((i & 1) == 1) // i es impar
		{
			big atemp = d * b + c * a; // temporal
			b = d * (b + a) + c * b; 
			a = atemp;
		}
		
		big ctemp = c * c + d * d; 	// temporal
		d = d * (2 * c + d); 
		c = ctemp;
		i /= 2;
	}
	
	#Trace clear
	#Trace k, " iteraciones"
	#: TimeSpan stop = new TimeSpan(DateTime.Now.Ticks);

	#Trace " en " + string.Format("{0:0.0000}", stop.Subtract(start).TotalMilliseconds) +" milisegundos.";
	return a + b;
}

#return (lfib(n));/// 
/// Calcula Fibonacci(n) con un algoritmo recursivo.
/// 
// Cambia aquí el valor de n:

int n = 5;
int limit = 100;

#: int k = 0;
#: big ns = 0;
#: int[] call = null;

int rfib(int n)
{
	#: if (ns == 0) 
	#:	ns =  fib(n) - 1;
	
	#: call[n]++;

	int ret;

	if (n <= 2)
	{
		ret = 1;
	}
	else
	{
		#: k++;
		ret = rfib(n-1) + rfib(n-2);
	}

	#trace set "Realizadas ", k, " iteraciones de ", ns, ".",  (k < ns ? "\nEvaluando ahora rf"+ "ib(" + n.ToString() + ")" : "")

	return (ret);
}

int TestRFib(int n)
{

	#: if (n > limit)
	#: 	throw new Exception("Debes dar un valor menor que "+ limit.ToString() + " (es un consejo de amigo)");

	#:  call = new int[n+1];

            #: TimeSpan start = new TimeSpan(DateTime.Now.Ticks);

	int r = rfib(n);
	
	#: TimeSpan stop = new TimeSpan(DateTime.Now.Ticks);

	#: string TT = "", TL = "";
	#: TL += "En el cálculo de $f_{" + n. ToString() + "}$  se han realizado $";
	#: TL += ns.ToString();
	#: TL += "$ iteraciones en " + stop.Subtract(start).TotalMilliseconds.ToString() +" milisegundos: \nsi llamamos $c(n)$ al número de llamadas a $f_n$, entonces\n\n$$\n";

	#: TT += "En el cálculo de rfib(" + n. ToString() + ") se han realizado ";
	#: TT += ns.ToString();
	#: TT += " iteraciones \nen " + stop.Subtract(start).TotalMilliseconds.ToString() +" milisegundos: ";


	#: TT += "\n\nn\tNº de llamadas a rf" + "ib(n)\n-------------------------------------------------------------------------------------\n"; 

	#: TL += "\\begin{array}{|c|";
	#: for (int i = 1; i < n; i++)
			#: TL += "r|";		
	#: TL += "}\n\\hline\nn & ";
	#: for (int i = 1; i < n; i++)
	#: 	TL += i.ToString() + (i<n-1 ? " & " : "\\\\ \\hline\n");
	#: TL += "c(n) & ";
	#: for (int i = 1; i < n; i++)
	#: 	TL += call[i]. ToString() + (i<n-1 ? " & " : "\\\\ \\hline\n");
	#: TL += "\\end{array}\n$$";

	#: for (int i = 1; i < n; i++)
	#:	TT += i.ToString() + "\t" + call[i].ToString() + "\n";
	
	#Trace clear
	#Trace (LATEX ? TL : TT);
	
            
	return r;
}

#return(TestRFib(n));
public class ListBig: List<big>
{
	// representa la lista entre paréntesis, con los números separados por comas.
	public string ToString()
	{
		if (this.Count == 0) return "()";

		StringBuilder sb = new StringBuilder();
		if (this[0] > 0)
			sb.AppendFormat("{0} + ", this[0]);

		for (int i = 1; i < this.Count; i++)
		{
			sb.AppendFormat("1/{0}", this[i]);
			if ( i < this.Count - 1)
				sb.Append(" + ");
		}
		
		return sb.ToString();
	}
}
		

ListBig FracEgipto(big a, big b)
{
	#Trace clear

	// Valida sus argumentos
	if (a < 1 || b < 1)
		throw new Exception("Los argumentos deben ser enteros positivos");

	ListBig ld = new ListBig();
	
	// Simplifica la fracción
	big d = mcd(a, b);

	if (d > 1)
	{ 
		#Trace  a, "/", b, " = "
		a /= d;
		b /= d;
		#Trace  a, "/", b, "\n\n"
	}

	string frac = string.Format("{0}/{1} = ", a, b);
	
	// Extrae la parte entera
	big e = suelo(a, b);
	ld.Add(e);
	a = a - b*e;

	// Descompone en suma de fracciones de numerador igual a 1
	while(a > 0)
	{
		big q = techo(b, a);
		ld.Add(q);
				
		// Hace a/b = a/b - 1/q
		big m = mcm(b, q);
		//#Trace "q=", q, " b=", b, " m=", m, "\n"
		a = a * m/b - m/q;
		b = m;

		if (a > 0)
		{
			#Trace frac, ld.ToString()
			#Trace " + "
			if (a > 1)
				#Trace "(", a, "/", b, ")\n"
			else if (a == 1)
				#Trace a, "/", b, "\n"
		}
	}

	return ld;
}

#return FracEgipto(19,20)public class ListFrac: List<BigFrac>
{
	// representa la lista entre paréntesis, con los números separados por comas.
	public override string ToString()
	{
		if (this.Count == 0) return "()";

		StringBuilder sb = new StringBuilder();
		if (this[0] > 0)
			sb.AppendFormat("{0} + ", this[0]);

		for (int i = 1; i < this.Count; i++)
		{
			sb.AppendFormat("{0}", this[i]);
			if ( i < this.Count - 1)
				sb.Append(" + ");
		}
		
		return sb.ToString();
	}
}
		
ListFrac FracEgipto(big a, big b)
{
	#Trace clear

	// Valida sus argumentos
	if (a < 1 || b < 1)
		throw new Exception("Los argumentos deben ser enteros positivos");

	ListFrac list = new ListFrac();
	
	BigFrac frac = new BigFrac(a, b);

	if (mcd(a,b) > 1)
		#Trace  a, "/", b, " = ", frac, "\n\n"
	
	string sfrac = frac.ToString() + " = ";
	
	// Extrae la parte entera
	big e = suelo(frac.Numerator, frac.Denominator);
	list.Add(e);
	frac -= e;

	// Descompone en suma de fracciones de numerador igual a 1
	while(frac.Numerator > 1)
	{
		BigFrac f1 = new BigFrac(1, techo(frac.Denominator, frac.Numerator));
		list.Add(f1);
				
		// Hace frac = frac - 1/b1
		frac -= f1;
		
		#Trace sfrac, list.ToString(), " + "
		if (frac.Numerator > 1)
			#Trace "(", frac, ")\n"
		else
			#Trace frac, "\n"
	}

	// El numerador es 1:
	list.Add(frac);

	return list;

}

#return FracEgipto(19,20)// y=a*x+b mod m
int a=3;
int b=2;
int m=1931; // prueba con m=1931
int n=0;

string Genera(int k)
{
	#trace clear

	#Trace n, "-"

	int nl = 0;

	int[] ns = new int[k];
	for (int i=0; i < k; i++)
	{
		n = (a*n + b) % m;
		
		nl++;
		if (nl == 15)
		{
			#trace "\n"
			nl = 0;
		}

		#Trace n, "-"

		for (int j = 0; j < i; j++)
		{
		    if (n == ns[i])
		    {
		        #Trace "\nPeriodo ", i - j, "\n"
		        return "";
		    }
		}
		ns[i] = n;
	}
	#Trace "\nEl periodo es mayor que ", k, "\n" 
	
	return "";
}

#return Genera(1000);
// La factorización de p:
big[] primo = new big[] {2, 3, 5, 2953, 8885820721};
int[] exp = new int[]   {1, 1, 1,    1,          1};

big FindGenerator()
{
	big p = 1;
	int k = primo.Length;	
	if (exp.Length != k)
		throw new Exception("Error en el número de exponentes");
		
	for (int i = 0; i < k; i++)
	{
		if (!probprimo(primo[i]))
			throw new Exception(string.Format("Error: {0} no es primo", primo[i]));
		for (int j = 0; j < exp[i]; j++)
			p *= primo[i]; 
	}
	p++;

	if (!probprimo(p))
		throw new Exception(string.Format("Error: {0} no es primo", p));
 	
	big g = 0;
	bool hallado = false;
	while (!hallado)
	{
		hallado = true;
    		g = random(2, p - 1);
  		for (int i = 0; i < k; i++)
		{
			big b = modpot(g, (p - 1) / primo[i], p);
			if (b == 1)
			{
				hallado = true;
				break;
			}
		}
		
		if (hallado)
			return g;
	}

	return g; 

 
 }

#return FindGenerator();// Da aquí el alfabeto
string alfabeto = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ"; // usa □ para un espacio visible

// La clave debe ser una matriz invertible módulo la longitud del alfabeto
int[,] key = {
{2,3,4}, 
{5,5,6}, 
{7,8,9}
}; 

// El texto en claro (los caracteres que no estén en el alfabeto se ignorarán):
string plaintext = "EN UN LUGAR DE LA MANCHA DE CUYO NOMBRE NO QUIERO ACORDARME";

/*
// Para descifrar (atención: la inversa es modulo 27)
int[,] key = {
{21,1,5}, 
{21,25,7}, 
{1,1,26}
}; 

string plaintext = "THRNWMAUIXQPDOGAMNOTRHJQOIÑYFOLSQDMYKBULUQNLIMFO";
*/

// --------------------------------------------------------------------------------------------------

public string Hill()  
{

	// Prepara el sistema
	// ------------------------------------------------------------------------

	// Dimensiones
	int modulo = alfabeto.Length;
	int nrows = key.GetLength(0); 

	// Hace visibles los espacios
	string pt_espacios_visibles = "";
	for (int i=0; i < plaintext.Length; i++)
		pt_espacios_visibles += (plaintext[i] == ' ' ? '□' : plaintext[i]);	

	// Descarta los caracteres que no están en el alfabeto
	string pt = "";
	for (int i=0; i < pt_espacios_visibles.Length; i++)
		if (alfabeto.IndexOf(pt_espacios_visibles[i]) >= 0)
			pt += pt_espacios_visibles[i];
	
	// Completa para que la longitud del texto en claro sea múltiplo de ncols
	while (pt.Length % 3  > 0)
		pt  += "X";

	// Número de filas de la matriz que contendrá el texto en claro
	int ncols = pt.Length / 3;
	
	// Cifra
	// ------------------------------------------------------------------------

	// Distribuye el texto en claro en una matriz de caracteres escribiéndolo por filas
	char[ , ] char_pt = new char[nrows, ncols];
	int row = 0; int col = 0;
	for (int i = 0; i < pt.Length; i ++)
	{
		char_pt[row, col] = pt[i];
		col++;
		if (col == ncols)
		{
			col = 0; row++;
		}
	}

	// Convierte la matriz de caracteres en una matriz de enteros
	int[ , ] int_pt = new int[nrows, ncols];
	for (int r = 0; r < nrows; r++)
		for (int c = 0; c < ncols; c++)
			int_pt[r, c] = alfabeto.IndexOf(char_pt[r, c]);
	
	// Cifra multiplicando cada columna por la matriz de la clave
	int[ , ] int_ct = new int[nrows, ncols];
	for (int c = 0; c < ncols; c++)
		for (int i = 0; i < nrows; i++)
			for (int j = 0; j < nrows; j++)
			{
				int_ct[i, c] += key[i, j] * int_pt[j, c];
				int_ct[i, c] %= modulo;
			}
	
	// El texto cifrado
	string cyphertext = "";
	for (int r = 0; r < nrows; r++)
		for (int c = 0; c < ncols; c++)
			cyphertext += alfabeto[int_ct[r, c]];

	// Ahora cuenta todo lo que ha hecho
	// ------------------------------------------------------------------------

	#Trace clear

	if (LATEX)
	{
		#T "Con el alfabeto \n\\begin{center}\n\\fbox{\\color{blue}" +  alfabeto + "}\n\\end{center}\n";
		#T "ciframos el texto \n\\begin{center}\n\\fbox{" + plaintext + "}\n\\end{center}\n";
		#T "usando un criptosistema de Hill de clave";
		#T "\n$$\n";
		#T "K=";
		#T "\\left(\\begin{array}{" + new string('r', nrows) + "}\n";
		for (int r = 0; r < nrows; r++)
			for (int c = 0; c < nrows; c++)
				#T "\\color{red}", key[r,c], (c == nrows-1 ? "\\\\ \n" : " & "); 
		#T "\\end{array}\\" + "right)\n"
		#T "$$\n";

		
		#T "La matriz del texto en claro es:"
		#T "\n$$\n\\left(\\begin{array}{" + new string('c', ncols) + "}\n";
		for (int r = 0; r < nrows; r++)
		{
			for (int c = 0; c < ncols; c++)
				#T  "\\color{blue}\\" + "text{" + char_pt[r, c] + "}" + (c < ncols -1 ? " & " :  " \\\\");
			#T "\n"
		}

		#T "\\end{array}\\" + "right)\n$$\n";
		
		#T "Sustituimos cada caracter por su valor numérico y ciframos multiplicando por la matriz de la clave:"
		#T "\n$$\n";
		#T "\\left(\\begin{array}{" + new string('r', nrows) + "}\n";
		for (int r = 0; r < nrows; r++)
			for (int c = 0; c < nrows; c++)
				#T "\\color{red}", key[r,c], (c == nrows-1 ? "\\\\ \n" : " & "); 
		#T "\\end{array}\\" + "right)\n"
		#T "\\cdot\n"
			
		#T "\\left(\\begin{array}{" + new string('r', ncols) + "}\n";
		for (int r = 0; r < nrows; r++)
			for (int c = 0;  c < ncols;c++)
				#T  "\\color{blue}" + int_pt[r, c].ToString() + (c < ncols - 1 ? " & " : " \\\\ \n");
		#T "\\end{array}\\" + "right)";
		#T "\\quad=\\quad";
		#T "\n$$\n";

		#T "\n$$\n";
		#T "\\left(\\begin{array}{" + new string('r', ncols) + "}\n";
		for (int r = 0; r < nrows; r++)
			for (int c = 0;  c < ncols;c++)
				#T  "\\color{blue}" + int_ct[r, c].ToString() + (c < ncols - 1 ? " & " : " \\\\ \n");
		#T "\\end{array}\\" + "right)";
		#T "\n$$\n";

		#T "En caracteres, es la matriz\n";
		#T "\n$$\n";
		#T "\\left(\\begin{array}{" + new string('r', ncols) + "}\n";
		for (int r = 0; r < nrows; r++)
			for (int c = 0;  c < ncols;c++)
				#T  "\\color{blue}\\text{" + alfabeto[int_ct[r, c]] + "}" + (c < ncols - 1 ? " & " : " \\\\ \n");
		#T "\\end{array}\\" + "right)";
		#T "\n$$\n";

		#T "que se corresponde con el texto cifrado\n\n";
		#T "\\begin{center}\\fbox{" + cyphertext + "}\\end{center}\n";
	}

	else
	{
		#T "Con el alfabeto " +  alfabeto + "\n";
		#T "ciframos el texto " + plaintext + "\n";
		#T "usando un criptosistema de Hill de clave\n";
		#T "\nK=\n";
		for (int r = 0; r < nrows; r++)
		{
			#T "|";
			for (int c = 0; c < nrows; c++)
				#T key[r,c], (c == nrows-1 ? "|\n" : "\t"); 
		}
		#T "\n";

		
		#T "La matriz del texto en claro es:\n"
		for (int r = 0; r < nrows; r++)
		{
			#T "|";
			for (int c = 0; c < ncols; c++)
				#T  char_pt[r, c], (c == ncols -1 ? "|\n" : "\t");
		}
		
		#T "\nSustituimos cada caracter por su valor numérico y ciframos multiplicando por la matriz de la clave:\n"
		for (int r = 0; r < nrows; r++)
		{
			#T "|";
			for (int c = 0; c < nrows; c++)
				#T key[r,c].ToString("00"), (c == nrows-1 ? "|\n" : "\t"); 
		}
		#T "*\n"
			
		for (int r = 0; r < nrows; r++)
		{
			#T "|";
			for (int c = 0;  c < ncols;c++)
				#T  int_pt[r, c].ToString("00"), (c== ncols - 1 ? "|\n" : "\t");
		}
		#T "=\n";
	

		for (int r = 0; r < nrows; r++)
		{
			#T "|";
			for (int c = 0;  c < ncols;c++)
				#T  int_ct[r, c].ToString("00"), (c == ncols - 1 ? "|\n" : "\t");
		}

		#T "\nEn caracteres, es la matriz\n";
		for (int r = 0; r < nrows; r++)
		{
			#T "|";
			for (int c = 0;  c < ncols;c++)
				#T  alfabeto[int_ct[r, c]] + (c == ncols - 1 ? "|\n" : "\t");
		}

		#T "\nque se corresponde con el texto cifrado ";
		#T cyphertext, "\n";
	}




	return (cyphertext);
}
		
		
#Return (Hill());// El número debe ser de 4 cifras y tener al menos dos dígitos diferentes
//	Se ordenan la cifras y se obtiene un número a
//	Se invierte a y se obtene b
// 	Se restan y se toma valor absoluto.
// Se repite: siempre se llega a la llamada Constante de Kaprekar: K = 6174

//--------- Cambia el valor de n aquí:

int n = 9650;

// -----------------------------------------------------

int i = 1; // contador de iteraciones
const int K = 6174; 


public int Kaprekar1(int n)
{	
	#T "Iteración ", i, ": "

	char[] k = n.ToString("0000").ToCharArray();
	#T k[0], k[1], k[2], k[3], "\n"


	// Ordena las cifras de forma ascendente
	Array.Sort(k);
	#T "\tOrdenamos: ", k[0], k[1], k[2], k[3], "\n"

	
	// Forma el número con esas cifras
	int n0 = 1000 * (k[0]-'0') + 100 * (k[1]-'0') + 10 * (k[2]-'0') + k[3]-'0'; 

	// Ordena las cifras de forma descendente
	Array.Reverse(k);

	// Forma el número con esas cifras:
	int n1 = 1000 * (k[0]-'0') + 100 * (k[1]-'0') + 10 * (k[2]-'0') + k[3]-'0'; 

	#T "\tRestamos: ", "|", n0, " - ", n1, "| = ", Math.Abs(n0 - n1), "\n\n"

	i ++;

	// Los resta
	return Math.Abs(n0 - n1);

}

public int Kaprekar(int n)
{
	#Trace clear

	// Comprueba que el número sea válido
	string error = "";
	if (n  > 9999)
	{
		error = "Debes dar un númeno menor que 10000";
	}
	else
	{
		string d = "";
		char[] k = n.ToString("0000").ToCharArray();
		for (int j=0; j<=3; j++)
			if (!d.Contains(k[j].ToString()))
				d += k[j].ToString();
		if (d.Length < 3)
			error = "El número debe tener al menos dos dígitos diferentes";
	}
	if (error != "")
	{
		throw new Exception(error);
		return n;
	}

	i = 1;

	while (n != K) // Parece que puede llegar a ser un bucle infinito, pero no lo es
	{
		n = Kaprekar1(n);
	}

	#T "-----------------------------------------------------------------------\n"
	#T "\tEncontrada la constante de Kaprekar en la iteración ", i - 1, "\n"
	#T "-----------------------------------------------------------------------\n\n"

	return n;
}

#Return(Kaprekar(n));
        string Kasiski(string s)
        {
            ArrayList result = new ArrayList();
            Hashtable trigrams = new Hashtable();

            // todos los trigramas y sus posiciones
            for (int i = 0; i < s.Length - 2; i++)
            {
                string k = s.Substring(i, 3);
                trigram tr = (trigram) trigrams[k];

                if (tr == null)
                {
                    tr = new trigram();
                    tr.key = k;
                    trigrams.Add(k, tr);
                }
                tr.positions.Add(i);
            }

			// Las coincidencias
			foreach (DictionaryEntry item in trigrams)
			{
				trigram tr = (trigram)item.Value; 
				if (tr.positions.Count > 1)
				{
					#Trace "\n[", tr.key, "] -> "
					for (int i = 0; i < tr.positions.Count; i++)
					{
						#Trace tr.positions[i], " "
						for (int j = i + 1; j < tr.positions.Count; j++)
						{
							result.Add((int) tr.positions[j] - (int) tr.positions[i]);
						}
					}
				}
			}

			result.Sort();
			StringBuilder sb = new StringBuilder();
			foreach (int d in result)
				sb.AppendFormat("{0:d} ", d);
        
			return (sb.ToString());
        }

        public class trigram
        {
         public string key = String.Empty;
         public ArrayList positions = new ArrayList(); 
        }

            #return (Kasiski("lpyjm0bam0lnwrsvm00;h20a0tldi24bjplñ22ñbhspa3t3aio1bk4bam0lim73fz4bal4xjupyewopmhwtfz70am2lmioqsiv6b·owfhtxcm0ptiqmñhp2vm0wp0omñ1tnsi<0thv3vm80thr0nwo5sw2ñp0oofhp3cw0pt'oñvjxps144altlvuo7fs00am81f04lzh70kp<0;ht3j7pop0oofh16tk@wp0o@altlñm77jw8da0trvzpxfu9paxpñp'opmhwpszt3p'owf3pyui7tbh0mak4xplplemo4vhwmcp9mdp4yak4ya220a04wphspa0@4ap11putyum8lczp(p0o@a0xyazt4fu9ts0tda6oofh86a143b5fl?y@p¡hr0ñhu3fk@pñkxmam0lim73fz4luzpnbqpnbhtyakpxj0t5bh}lt2o1fkw0aot3d20pph86cpplzhqmkiqm;hpwazt4qp7ms'oñpt4ltpoqvm7mam0lemo6ñhtwfnpyumosfzxop·opt14lfzplvuosptq3f·o@au4lsi10ñ'opmhwtkwoofsonp1xñbzx0;htxqm7pkp0mewo@a1xptwo@axpwjl4ldw10a22mat@ñiirsbh10sjxobh}lqzt4vtxob·o4jht4uwopsio1swv3f04bam0baltñjlxobttyumflñwo2vm7tbh53pñ7pti7dax43a0@lqi75f'o4fhr0ñn43niqmak4ya1tyfzo6ñio1bztubhspa3pñb0flvuplqm66fvplr2t4fzxma6opmhxytpvyjnxñbu9pao@ps14lemowbh93b0t3bhspa0@ldi8m:h20axtojioxb0hlmw8lepp4aspnpzpnmm8lgiq3jkp3jio2vm80t'oñpt4lt2o1bl7p;h}lmw8lew1tññ44a0tlfu93f1tyezxmak4yasplf0r0qm9m;h4ltm7ljzxmai0lsp4lbh5ptkp3a176dop4awomamrsbzo6ñio1bz9teiommhr0sz4lemonps44:"));
      big M(int n)
{
	big dos = (big)2;
	return (dos^n) - 1;
}


int test(int n)
{
	#Trace clear
	for (int i = 1; i <= n; i++)
	{
		big m = M(i);
		FactorizePollardRho fm = new FactorizePollardRho();
		fm.Factorize(m);
		#Trace "M_{", i, "} & = & ", m;	
	
		if (fm.Factors.Count > 1)
		{
			#Trace " & = & ", fm.Factors
		}
	
		#Trace "\\\\ \n"
	}

//	#Trace replace "·", "\\cdot "
	return 0;
}

#Return test(7);

big I(big m, big n)
{
	big ret = 0;
	
	if (m == 0)
		ret = 0;
	else if (n == 0)  
		ret = 1;
	else if (m > n)
		ret = I(m % n, n);
	else
	
	ret = I(m, n % m) - suelo(n, m) * I(n % m, m);

	return ret;
}

big Inv(big m, big n)
{
	#trace clear

	big k = I(m,n);

	#trace "El inverso de ", m, " mod ", n, " es ", k

	//#trace "\n", (k*m) % n
	return  k;
}

#Return Inv(83, 2450);

big xmodInv(big m, big n)
{
	#trace clear

	big a = m;
	big b = n;
	big s = 1; // signo.

	// La matriz se inicializa a la matriz unidad:
	big m11 = 1;
	big m12 = 0;
	big m21 = 0;
	big m22 = 1;

	while (b > 0)
	{
		big c = a / b;
		big r = a % b;
		a = b;
		b = r;
		s = -s;
		big t11 = m11 * c + m12;
		big t12 = m11;
		big t21 = m21 * c + m22;
		big t22 = m21;

		m11 = t11;
		m12 = t12;
		m21 = t21;
		m22 = t22;
	}

	// Se retorna un array: los dos primeros elementos son los coeficientes de la
	// igualdad de Bezout y el tercero es el mcd:
	big[] ret = new big[3];
	ret[0] = s * m22;
	ret[1] = - s * m12;
	ret[2] = a;	

	if (ret[2] != 1)
	{
		#Trace "\nNo tiene inverso"
		return 0;
	}

	else
	{
		ret[0] %= n;
		if (ret[0] < 0)
			ret[0] += n;

		#trace "El inverso de ", m, " mod ", n, " es ", ret[0]
		return ret[0];
	}
}

#Return xmodInv(3, 112);
big Pot(big u, big m)
{
	int k = 0;
	big b = 1;
	big t = u;
	big q = m;
	string op = ""; 

	#trace clear
	#Trace "op\t", "q\t", "b\t\t", "t", "\tb*t^q" 
	#Trace "\n------------------------------------------------------------------------------" 
	#Trace "\n", op, "\t", q, "\t", b, "\t\t", t, "\t", b * PotS(t, q) 
	while (q > 0)
	{
		op = "";
		if (q % 2 == 1)
		{
			b *= t;
			op += "M";
		}

		q = q / 2;
		if (q > 0)
		{
			t = t * t;
			op += "C";
		} 
		#Trace "\n", op, "\t", q, "\t", b, "\t\t", t, "\t", b * PotS(t, q)
	}
	return b; 
}

big PotS(big u, big m)
{
	int k = 0;
	big b = new big(1);
	big t = u;
	big q = m;

	while (q > 0)
	{
		if (q % 2 == 1)
			b *= t;

		q = q / 2;

		if (q > 0)
			t = t * t;
	}
	return b; 
}

#Return Pot(2, 3139);

//
// Calcula una potencia módulo un número.
// Usa el algoritmo ingenuo para las potencias.
//
// Para calcular b^e mod n, cambia los valores aquí:
//

big b = new big("12345678");
big e = new big("23456789");
big n = new big("34567890");

//
// ----------------------------------------------------------------------------------------------------


big Pot(big u, big m, big mod)
{
	big ret = 1;
	#: TimeSpan start = new TimeSpan(DateTime.Now.Ticks);

	for (int i = 0; i < m; i++)
	{
		#Trace set "Realizados ", i, " productos en "
		#: TimeSpan stop = new TimeSpan(DateTime.Now.Ticks);
		#Trace string.Format("{0:0.0000}", stop.Subtract(start).TotalMilliseconds) +" milisegundos.";
		ret = (ret * u) % n;
	}

	return ret;
}

#Return Pot(b, e, n);

//
// Calcula una potencia módulo un número.
// Usa el algoritmo de las potencias rápidas.
//
// Para calcular b^e mod n, cambia los valores aquí:
//

big b = new big("21");
big e = new big("99"); // 99=phi(250)-1). Prueba también con 24
big n = new big("250");

//
// ----------------------------------------------------------------------------------------------------


big Pot(big u, big m, big mod)
{
	int k = 0;
	big b = 1; 
	
	#Trace clear

	string bim = tobin(m);
	
	#: string msg ="Calculando:\n";
	#Trace msg

	if (LATEX)
		#Trace m, " en binario es ", bim, " $\\longrightarrow$ {\\tt " 
	else
		#Trace m, " en binario es ", bim, " ──► " 

	for (int i = 0; i < bim.Length; i++)
	{
		if (bim[i] == '1')
			#Trace "M"
		if (i < bim.Length - 1) // entre dos bits
			#Trace "C"
	} 

	if (LATEX)
		#Trace "}\n\\vspace*{1ex}\n\nOperando módulo ", mod, ":\n\\vspace*{1ex}\n\n$$"
	else
		#Trace "\n\n"


	#Trace b

	#: TimeSpan start = new TimeSpan(DateTime.Now.Ticks);

	for (int i = 0; i < bim.Length; i++)
	{

		if (bim[i] == '1')
		{
			b *= u;
			if (mod > 1)
				b = b % mod;
			k++; 
			if (LATEX)
				#Trace "\\suparrow[.5cm]{M} ", b
			else
				#Trace " ──M──► ", b
		}
		if (i < bim.Length - 1) // entre dos bits
		{
			b *= b;
			if (mod > 1)
				b = b % mod;
			k++; 
			if (LATEX)
				#Trace "\\suparrow[.5cm]{C} ", b
			else
				#Trace " ──C──► ", b
		}
	} 
	if (LATEX)
		#Trace "$$\n\\vspace*{2ex}\nSe han realizado ", k, " multiplicaciones."

	#: TimeSpan stop = new TimeSpan(DateTime.Now.Ticks);

	#trace replace msg, string.Format("Realizadas {0} multiplicaciones en {1:0.0000} milisegundos:\n\n", k, stop.Subtract(start).TotalMilliseconds);

	return b;
}

#Return Pot(b, e, n);

big Pot(big u, big m)
{
	int k = 0;
	big b = 1; 
	
	#Trace clear
	string bim = tobin(m);
	#Trace m, " en binario es ", bim, " ──► " 

	for (int i = 0; i < bim.Length; i++)
	{
		if (bim[i] == '1')
			#Trace "M"
		if (i < bim.Length - 1) // entre dos bits
			#Trace "C"
	} 

	#Trace "\n\n"

	#Trace b

	for (int i = 0; i < bim.Length; i++)
	{

		if (bim[i] == '1')
		{
			b *= u;
			k++; 
			#Trace " ──M──► ", b
		}
		if (i < bim.Length - 1) // entre dos bits
		{
			b *= b;
			k++; 
			#Trace " ──C──► ", b
		}
	} 
	#Trace "\n", k, " multiplicaciones."
	return b;
}

#Return Pot(2, 3139);


//
// Calcula una potencia módulo un número.
// Usa el algoritmo de las potencias rápidas.
//
// Para calcular b^e, cambia los valores aquí:
//

big b = new big("2");
big e = new big("10"); 

//
// ----------------------------------------------------------------------------------------------------


big Pot(big b, big e)
{	
	big p = 1;

	#Trace tobin(e), "\n"
	while (e > 0)
	{
		if (e % 2 == 1)
		{
			p = p * b;
		}

		e = e / 2;
		if (e > 0)
		{
			b = b * b;
		}

	}

	return p;	
}

#Return Pot(b, e);
bool IsPrime(big n)
{
	return IsPrime(n, 100);
}


bool IsPrime(big n, int k)
{
	big m = (n-1) / 2 + 1;

	for (int i = 0; i < k; i++)
	{
		big a = Random(2, m);
		big b = Random(2, m);

		big pot1 = modpot(a + b, n, n);
		big pot2 = (modpot(a, n, n) + modpot(b, n, n)) % n;
		 
		if (pot1 != pot2)
			return false;
	}

	return true;
}

bool test()
{
	#Trace clear
	#Trace "Iniciando el test"
	for (int i = 0; i < 1000; i++)
	{
		big n = genimpar(100);

		bool mr = mrtest(n);
		bool pb = IsPrime(n);

		if (mr != pb)
		{
			#Trace "Error en \n", n
			#Trace "\nmr -> ", mr
			#Trace "\npb -> ", pb
			return false;
		}
	}
	#Trace "\nTest finalizado"
	return true;
}
	
#Return test();
//IsPrime(50, 100);		
///
/// Calcula el producto de dos enteros.
/// Entrada: dos enteros 0 < a < b
///
/// Cambia aquí los valores de entrada:

int a = 45;
int b = 123;

// -----------------------------------------------------------------------------------------------------------

public int Producto(int a, int b) {

	if (a <= 0 || b<= 0 || a > b)
		throw new Exception ("Los argumentos deben ser positivos y a <  b");
	
	int p = 0;

	#Trace clear
	#: string op = "";

	#: int s = 0;
	#: int d = 0;
	#Trace "a=", a, "\n"
	#Trace "b=", b, "\n"

	#Trace "I=a·b+p=", a*b+p, "\n"
	while (a > 0)
	{
		if (a % 2 == 1)
		{
			#: op += "+"; s++;
			p = p + b;
		}

		a = a  /  2; op += "»"; 
		#: d++;
		b = 2 *  b; op += "«"; 
		#: s++;
		#Trace "a=", padleft(a, 10), "\tb=", padleft(b, 10), "\tp=", padleft(p, 10), "\tI=a·b+p=", a*b+p, "\n" 
	}
	
	#Trace "\nOperaciones realizadas:", op, " (", s, " sumas y ", d, " desplazamientos de bits)."
	return p;
	
}

#Return Producto(a, b);//
// Este programa cuenta cuántas RC1 hay en $\Z_n$ (muy rápidamente) y las 
// calcula (algo más lento, aunque no utiliza la fuerza bruta para hallarlas.
//
// Cambia aquí el valor de n:

big n = new big("180435524543453222222888888888888");

// -------------------------------------------------------------------------------

BigList RC1(big n)
{
	#Trace clear
	big dos = 2;
	SysEq seq = null;

	BigList roots = new BigList();

	FactorizeLenstra lenstra = new FactorizeLenstra();
	lenstra.Factorize(n);

	int r = lenstra.Factors.Count;
	big k = 0; // Exponente de 2
	if (lenstra.Factors.Values[0].NumberBase == 2)
		k = lenstra.Factors.Values[0].Exponent;

	List<string> signs;

	#Trace "n = ", lenstra.Factors.ToString(), "\n\n"
	CuentaRC1(n, r);
	#Trace "\n"

	if (k == 1)
	{
	   	#Trace "n = 2·m, m impar\n"
		if (r == 1) // n = 2
		{
			roots.Add(1);
			#Trace "ra", "íz: ", 1, "\n"
		}
		else
		{
			signs = AllSign(r - 1);
			foreach (string s in signs)
			{
				seq = new SysEq(1, 1, 2);
				for (int i = 0; i < r - 1; i++)
				{
					FactorBlock factor = lenstra.Factors.Values[i + 1];
					seq.Add(new Equation(1, (s[i] == '+' ? 1 : -1), factor.NumberBase, factor.Exponent));
				}
				Equation res = seq.Solve();
				#Trace "\n", seq, "ra", "íz: ", res.b, "\n"
				roots.Add(res.b);
			}
		}
	}
		
	else if (k == 2)
	{
		#Trace "n = 4·m, m impar\n"
		if (r == 1) // n = 4
		{
			roots.Add(1);
			roots.Add(-1);
			#Trace "ra", "íz: ", 1, "\n"
			#Trace "ra", "íz: ", n-1, "\n"
		}
		else
		{
			signs = AllSign(r - 1);
			foreach (string s in signs)
			{
				foreach (int j in new int[] {1, -1})
				{
					seq = new SysEq(1, j, 4);
					for (int i = 0; i < r - 1; i++)
					{
						FactorBlock factor = lenstra.Factors.Values[i + 1];
						seq.Add(new Equation(1, (s[i] == '+' ? 1 : -1), factor.NumberBase, factor.Exponent));
					}
					Equation res = seq.Solve();
					#Trace "\n", seq, "ra", "íz: ", res.b, "\n"
					roots.Add(res.b);
				}
			}
		}
	}
		
	else if (k >= 3)
	{
		#Trace "n = 8·m\n"
		if (r == 1) // n = 8
		{
			roots.Add(1);
			roots.Add(7);
			roots.Add(3);
			roots.Add(5);
			#Trace "ra", "íz: ", 1, "\n"
			#Trace "ra", "íz: ", 3, "\n"
			#Trace "ra", "íz: ", 5, "\n"
			#Trace "ra", "íz: ", 7, "\n"
		}
		else
		{
			signs = AllSign(r - 1);

			foreach (string s in signs)
			{
				foreach (big j in new big[] { 1, -1, (dos ^ (k - 1)) + 1, (dos ^ (k - 1)) - 1 })
				{
					seq = new SysEq();
					seq.Add(new Equation(1, j, dos, k));
					for (int i = 0; i < r - 1; i++)
					{
						FactorBlock factor = lenstra.Factors.Values[i + 1];
						seq.Add(new Equation(1, (s[i] == '+' ? 1 : -1), factor.NumberBase, factor.Exponent));
					}
					Equation res = seq.Solve();
					#Trace "\n", seq, "ra", "íz: ", res.b, "\n"
					roots.Add(res.b);
				}
			}
		}
	}
		
	else
	{
		#Trace "n impar\n"
		signs = AllSign(r);
		foreach (string s in signs)
		{
			seq = new SysEq();
			for (int i = 0; i < r; i++)
			{
				FactorBlock factor = lenstra.Factors.Values[i];
				seq.Add(new Equation(1, (s[i] == '+' ? 1 : -1), factor.NumberBase, factor.Exponent));
			}
			Equation res = seq.Solve();
			#Trace "\n", seq, "ra", "íz: ", res.b, "\n"
			roots.Add(res.b);
		}
	}

	roots.Sort(delegate(big x, big y)
	{
		if (x < y) return -1;
		else if (x > y) return 1;
		else return 0;
	});

	return roots;
}

static List<string> AllSign(int n)
{
	if (n == 0)
		return new List<string>();

	if (n == 1)
	{
		List<string> ls = new List<string>();
		ls.Add("+");
		ls.Add("-");
		return ls;
	}
	else
	{
		List<string> ls2 = new List<string>();
		foreach (string s in AllSign(n - 1))
		{
			ls2.Add("+" + s);
			ls2.Add("-" + s);
		}
		return ls2;
	}
}


void  CuentaRC1(big n, int r)
{
	int m8 = (n % 8 == 0 ? 1 : 0);
	int m4 = (n % 4 == 0 ? 1 : 0);
	int m2 = (n % 2 == 0 ? 1 : 0);
	
	big dos = 2;

	#Trace "Nº de raíces: 2^(", r, "+", m8, "+", m4, "-", m2, ") = ", dos^(r+m8+m4-m2), "\n" 
}

#Return RC1(n);// 
// Permite cifrar y descifrar con RSA usando un alfabeto elegido
// y realizando una codificación secuencial de los caracteres.
//
// ------------------------ Modifica  a partir de aquí: -------------------------------

string alfabeto = "□ABCDEFGHIJKLMNÑOPQRSTUVWXYZ";

big p = new big("31");
big q = new big("911");
big e = new big("19");
int r = 2;

string plaintext = "HOLA□A□TODOS";

bool TCR = true;

// ------------------------ fin de las modificaciones --------------------------------

// Variables globales
big n;
big fi; 
big d; 
big d_p; 
big d_q; 
big q_inv;
big p_inv;

List<string> pbt;
List<big> pnum;
List<big> cnum;
List<big> y;

// Precalcula
void Precalcula()
{
	n = p * q;
	fi = (p-1)*(q-1);
	d = modinv(e, fi);
	d_p = d % (p-1);
	d_q = d % (q-1);
	q_inv = modinv(q, p);
	p_inv = modinv(p, q);

	pbt = new List<string>();
	pnum = new List<big>();
	cnum = new List<big>();
}

// Representa la cadena de texto en claro como la lista de enteros 'pnum':
void text2num()
{
	// Completa, si es necesario
	string ptext = plaintext.ToUpper();
	if (plaintext.Length % r > 0)
		ptext += new String(alfabeto[0], r - (plaintext.Length % r));

	// Divide en bloques
	pbt = new List<string>();
	while (!string.IsNullOrEmpty(ptext))
	{
		big num = 0;
		string bloque = ptext.Substring(0, r);
		foreach (char c in bloque)
		{
			int n = alfabeto.IndexOf(c);
			num = 100 * num + n;
		}
		pbt.Add(bloque);
		pnum.Add(num);
		ptext = ptext.Substring(r);
	}
}

string num2text(big x)
{
	// Interpreta el número 'x' como un bloque de texto

	string b = x.ToString();
	while (b.Length < 2 * r)
		b = "0" + b;

	//	string bx = d2(b);

	string pb = "";
	while (!string.IsNullOrEmpty(b))
	{
      	try
      	{
                	pb += alfabeto[int.Parse(b.Substring(0, 2))].ToString();
            }
            catch
            {
             	pb += "?";
            }
            finally
            {
            	b = b.Substring(2);
            }
	}

	return pb;
}

string d2(string b)
{
	string res = "";

	while (!string.IsNullOrEmpty(b))
	{
		res += (b.Substring(0, 2) + " ");
		b = b.Substring(2);
	}

	return res.Trim();
}


bool ValidaClave()
{
	List<string> msg = new List<string>();

	// Valida el alfabeto
	if (alfabeto.Length > 99)
		msg.Add ("El alfabeto no puede ser de más de 99 caracteres");

	// Valida la clave
	if (!mrtest(p))
		msg.Add ((LATEX ? "$p$" : "p") + " no es primo.");

	if (!mrtest(q))
		msg.Add ((LATEX ? "$q$" : "q") + " no es primo.");
	
	n = p * q;
	fi = (p - 1) * (q - 1);

	if (mcd(e, fi) != 1)
		msg.Add (LATEX ? "$e$ no es primo con $\\phi(n)$." : "e no es primo con fi.");
	
	big mm = 0;
	int mc = alfabeto.Length - 1;
	for (int i = 0; i < r; i++)
		mm = 100 * mm + mc;

	if (p*q < mm)
		msg.Add ("El módulo debe ser mayor que " + mm.ToString() + ".");

	// Si hay errores, informa:
	if (msg.Count > 0)
	{
		#Trace "La clave RSA no es correcta:\n"
		if (LATEX)
			#Trace "\n\\begin{itemize}\n"
		foreach (string s in msg)
			#Trace (LATEX ? "\\item " : ""), s, "\n"
		if (LATEX)
			#Trace "\n\\end{itemize}\n\n"
		
		return false;
	}

	// Clave válida
	else
	{
		#Trace "La clave RSA es correcta.\n\n"
	}

	Precalcula();

	// Informa con un resumen de los parámetros de la clave
	#: if (LATEX)
	#: {
		#Trace "$$\n\\begin{array}{|llr|}\n"
		#Trace "\\hline\n"
		#Trace "\\", "rowcolor{gray!15}\\multicolumn{3}{|l|}{\\text{Parámetros de RSA:}}\\\\\n"
		#Trace "\\hline\n"
		#Trace "p 	&=& ", p, "\\\\\n"
		#Trace "q 	&=& ", q, "\\\\\n"
		#Trace "n = p\\cdot q 	&=& ", n, "\\\\\n"
		#Trace "e 	&=& ", e, "\\\\\n"
		#Trace "\\phi(n) = (p-1)\\cdot (q-1) 	&=& ", fi, "\\\\\n"
		#Trace "d=e^{-1}\\ \\Mmod\\phi(n) 	&=& ", d, "\\\\\n"
		#Trace "r 	&=& ", r, "\\\\\n"
		#: if (TCR)
		#: {
			#Trace "d_p=d\\ \\Mmod (p-1) 	&=& ", d_p, "\\\\\n"
			#Trace "d_q=d\\ \\Mmod (q-1) 	&=& ", d_q, "\\\\\n"
			#Trace "p_{inv}=p^{-1}\\ \\Mmod q 	&=& ", p_inv, "\\\\\n"
			#Trace "q_{inv}=q^{-1}\\ \\Mmod p 	&=& ", q_inv, "\\\\\n"
		#: }
		#Trace "\\hline\n"
		#Trace "\\end{array}\n$$\n"
	#: }
	#: else
	#: {
		#Trace "\nParámetros de RSA:\n---------------------------------------------------------------------------------------------------\n"
		#Trace "p = ", p, "\n"
		#Trace "q = ", q, "\n"
		#Trace "n = p · q = ", n, "\n"
		#Trace "e = ", e, "\n"
		#Trace "fi = (p-1)(q-1) = ", fi, "\n"
		#Trace "d = e^-1 (mod fi) = ", d, "\n"
		#Trace "r = ", r, "\n"

		#: if (TCR)
		#: {
			#Trace "d_p = d mod (p-1) = ", d_p, "\n"
			#Trace "d_q = d mod (q-1) = ", d_q, "\n"
			#Trace "p_inv = p^-1 mod q = ", p_inv, "\n"
			#Trace "q_inv = q^-1 mod p = ", q_inv, "\n"
		#: }

	#: }
		
	return true;

}

void Cifra()
{
	// El cifrado siempre se hace elevando al exponente.
	foreach (big x in pnum)
	{
		cnum.Add(modpot(x, e, n));
	}

	// Muestra una tabla con el cifrado:
	#: if (LATEX)
	#: {
		#Trace "\\Par\nEl cifrado de $x$ es $y=x^e\\ \\Mmod n= x^{", e, "}\\ \\Mmod ", n, "$.\n\\Par\n"
		
		#Trace "\\begin{center}\n"
		#Trace "\\def\\arraystretch{1.5}\n"
		#Trace "\\begin{tabular}{|c|c|r|}\n"
		#Trace "\\hline\n"
		#Trace "\\", "rowcolor{gray!15}\n"
		#Trace "{\\color{blue}\\tt M} & $x$	& $y$\\\\\n"
		#Trace "\\hline\n"
	#: }
	#: else
	#: {
		#Trace "\n\n"
		#Trace "El cifrado de x es y = x^e mod n = x^", e, " mod ", n, ".\n\n"
		#Trace "M", " \t-> \t", "x", " \t-> \t", "y"
		#Trace "\n-----------------------------------------------------------------------------------------------------", "\n"
	#: }
	#:
	#: for (int i = 0; i < pnum.Count; i++)
	#: {
	#:	big x = pnum[i];
	#: 	string b = x.ToString();
	#: 	while (b.Length < 2 * r)
	#: 		b = "0" + b;

	#: 	string bx = d2(b);

	#: 	if (LATEX)
			#Trace "{\\color{blue}\\tt ", pbt[i].Replace("□", "\\ev "), "}	& ", bx, " & \\color{red}", cnum[i], " \\\\\n";
	#: 	else
			#Trace pbt[i], " \t-> \t", bx, " \t-> \t", cnum[i], "\n"
	#: }

	#: if (LATEX)
	#: {
		#Trace "\\hline\n"
		#Trace "\\end{tabular}\n\\end{center}\n"
	#: }

}


void Descifra()
{
	#: if (LATEX)
	#: {
		#Trace "\n\n\\Par\nEl descifrado de $y$ es $x = y^d \\ \\Mmod n = y^{", d, "}\\ \\Mmod ", n, "$\n"
		#Trace "Usando el TCR, el descifrado es"
		#Trace "$$x = ( (y^{d_p}\\ \\Mmod p) \\cdot q \\cdot q_{inv} + (y^{d_q}\\ \\Mmod q) \\cot p \\cdot q_{inv} )\\ \\Mmod n =$$\n"
		#Trace "$$"
		#Trace "(y^{", d_p, "}\\ \\Mmod ", p, ")\\cdot ", q, "\\cdot ", q_inv, " + "  
		#Trace "(y^{", d_q, "}\\ \\Mmod ", q, ")\\cdot ", p, "\\cdot ", p_inv , ")\\ \\Mmod ", n, "$$" 
		#Trace "que hace potencias menores y es más eficiente.\n\n"		
		#: if (TCR)
			#Trace "Desciframos usando el TCR\n"
		#: else
			#Trace "Desciframos elevendo a la clave privada\n"	
		#Trace "\n\\Par\n"
		#Trace "\\begin{center}\n"
		#Trace "\\def\\arraystretch{1.5}\n"
		#Trace "\\begin{tabular}{|c|c|c|}\n"
		#Trace "\\hline\n"
		#Trace "\\", "rowcolor{gray!15}\n"
		#Trace "{\\color{red}$y$} & $x$ & "
		#Trace "{\\color{blue}\\tt M} \\\\\n"
		#Trace "\\hline\n"
	#: }
	#: else
	#: {
		#Trace "\n\nEl descifrado de y es x = y^d mod n = y^", d, " mod ", n, ".\n"
		#Trace "Usando el TCR, el descifrado es x = ( (y^d_p mod p) · q · q_inv + (y^d_q mod q) · p · q_inv ) mod n =\n"
		#Trace "(y^", d_p, " mod ", p, ")·", q, "·", q_inv, " + "  
		#Trace "(y^", d_q, " mod ", q, ")·", p, "·", p_inv , ") mod ", n, ", " 
		#Trace "que hace potencias menores y es más eficiente.\n\n"	
		#: if (TCR)
			#Trace "Desciframos usando el TCR\n"
		#: else
			#Trace "Desciframos elevendo a la clave privada\n"	
		#Trace "y", " \t-> \t", "x"
		#Trace "\t-> \t", "M", "\n-----------------------------------------------------------------------------------------------------", "\n"
	#: }

	foreach (big y in cnum)
	{
		big x = 0;
		if (TCR)
			x = (modpot(y, d_p, p) * q * q_inv + modpot(y, d_q, q) * p * p_inv ) % n; 
		else
			x = modpot(y, d, n);
	
		string pb = num2text(x);

		if (LATEX)
			#Trace "$", y, "$ & $", x, "$ & ", pb.Replace("□", "\\ev "), "\\\\\n"

		else
			#Trace y, " \t-> \t", x, " \t-> \t", pb, "\n"

	}

	if (LATEX)
	{
		#Trace "\\hline\n"
		#Trace "\\end{tabular}\n\\end{center}\n"
	}

}




string RSA()
{
	#Trace clear

	if (!ValidaClave())
		return "Error";

	text2num();

	Cifra();

	Descifra();

	return "";
}


#Return RSA()// 
// Permite cifrar y descifrar con RSA usando un alfabeto elegido
// y realizando una codificación secuencial de los caracteres.
//
// ------------------------ Modifica  a partir de aquí: -------------------------------

string alfabeto = "ABEKR";

big p = new big("13");
big q = new big("37");
big e = new big("19");
int r = 2;

string plaintext = "BAKA";

// ------------------------ fin de las modificaciones --------------------------------



List<big> Cifra(string plaintext)
{
	// Completa, si es necesario
	string ptext = plaintext.ToUpper();
	if (plaintext.Length % r > 0)
		ptext += new String(alfabeto[0], r - (plaintext.Length % r));

	// Divide en bloques
	List<big> pnum = new List<big>();
	List<string> pbt = new List<string>();
	while (!string.IsNullOrEmpty(ptext))
	{
		big num = 0;
		string bloque = ptext.Substring(0, r);
		foreach (char c in bloque)
		{
			int n = alfabeto.IndexOf(c);
			num = 100 * num + n;
		}
		pbt.Add(bloque);
		pnum.Add(num);
		ptext = ptext.Substring(r);
	}

	// Cifra cada bloque
	List<big> cnum = new List<big>();
	int i = 0;

	if (LATEX)
	{
		#Trace "\n\\vspace*{2ex}\n\nCifrado de {\\tt ", plaintext.Replace(alfabeto[0].ToString(), "\\ev "), "}:\n\n" 
		#Trace "\\begin{center}\n"
		#Trace "\\def\\arraystretch{1.5}\n"
		#Trace "\\begin{tabular}{|c|r|rcr|}\n"
		#Trace "\\hline\n"
		#Trace "\\", "rowcolor{gray!15}\n"
		#Trace "\\vphantom{$M^M$}{\\color{blue}\\tt M}			& $x$		& \\multicolumn{3}{r|} {\\color{red}$y$} \\\\\n"
		#Trace "\\hline\n"
	}
	else
	{
		#Trace "\n\nCifrado:\n"
		#Trace "M", " \t-> \t", "x", " \t-> \t", "y", "\n-----------------------------------------------------------------------------------------------------", "\n"
	}

	foreach (big x in pnum)
	{
		string b = x.ToString();
		while (b.Length < 2 * r)
			b = "0" + b;

		string bx = d2(b);

		cnum.Add(modpot(x, e, n));

		if (LATEX)
			#Trace "{\\color{blue}\\tt ", pbt[i].Replace(alfabeto[0].ToString(), "\\ev "), "}	& ", bx, " & $", x, "^{", e, "}\\ (\\Mod\\; ", n, ")$    & = &  \\color{red}", cnum[i], " \\\\\n";
		else
			#Trace pbt[i], " \t-> \t", bx, " \t-> \t", cnum[i], "\n"
		
		i++;
	}

	if (LATEX)
	{
		#Trace "\\hline\n"
		#Trace "\\end{tabular}\n\\end{center}\n"
	}
	
	return cnum;
}

string Descifra(List<big> cnum)
{
	string plaintext = "";
	
	if (LATEX)
	{
		#Trace "\n\\vspace*{2ex}\n\nDescifrado:\n\n"
		#Trace "\\begin{center}\n"
		#Trace "\\def\\arraystretch{1.5}\n"
		#Trace "\\begin{tabular}{|r|rcrcc|c|}\n"
		#Trace "\\hline\n"
		#Trace "\\", "rowcolor{gray!15}\n"
		#Trace "\\vphantom{$M^M$}{\\color{red}$y$}			& \\multicolumn{3}{r}{$x$}		& & &  {\\color{blue}\\tt M} \\\\\n"
		#Trace "\\hline\n"
	}
	else
	{
		#Trace "\n\nDescifrado:\n"
		#Trace "y", " \t-> \t", "x", " \t-> \t", "M", "\n-----------------------------------------------------------------------------------------------------", "\n"
	}

	foreach (big y in cnum)
	{
		big x = modpot(y, d, n);

		// Interpreta el número como un bloque de texto
		string b = x.ToString();
		while (b.Length < 2 * r)
			b = "0" + b;

		string bx = d2(b);

		string pb = "";
		while (!string.IsNullOrEmpty(b))
		{
           	try
                {
                		pb += alfabeto[int.Parse(b.Substring(0, 2))].ToString();
                }
                catch
                {
                		pb += "?";
                }
                finally
                {
                      b = b.Substring(2);
                }
		}

		plaintext += pb;
		
		if (LATEX)
			#Trace "\\color{red}", y, " & $", y, "^{", d, "}\\ (\\Mod\\; ", n, ")$	& = & ", x, " & $\\", "rightarrow $ &  ", bx, "	& {\\color{blue}\\tt ", pb.Replace(alfabeto[0].ToString(), "\\ev "), "} \\\\\n"			
		else
			#Trace y, " \t-> \t", bx, " \t-> \t", pb, "\n"

	}

	if (LATEX)
	{
		#Trace "\\hline\n"
		#Trace "\\end{tabular}\n\\end{center}\n"
	}

	return plaintext.Replace(alfabeto[0], ' ');
}

string d2(string b)
{
	string res = "";

	while (!string.IsNullOrEmpty(b))
	{
		res += (b.Substring(0, 2) + " ");
		b = b.Substring(2);
	}

	return res.Trim();
}

string MC(big m)
{
	string bim = tobin(m);

	string mc = "";

	foreach (char c in bim)
	{
		if (c == '1')
			mc += "M";
		mc += "C";
	}

	return mc.Substring(0, mc.Length - 1);
}

string RSA()
{
	#Trace clear

	if (alfabeto.Length > 99)
		throw new Exception("El alfabeto no puede ser de más de 99 caracteres");

	// Valida la clave
	List<string> msg = new List<string>();
	bool critical = false;

	if (!mrtest(p))
	{
		critical = true;
		msg.Add ((LATEX ? "$p$" : "p") + " no es primo.");
	}	
	if (!mrtest(q))
	{
		critical = true;
		msg.Add ((LATEX ? "$q$" : "q") + " no es primo.");
	}	
	
	n = p * q;
	big fi = (p - 1) * (q - 1);

	if (mcd(e, fi) != 1)
	{
		critical = true;
		msg.Add (LATEX ? "$e$ no es primo con $\\phi(n)$." : "e no es primo con fi.");
	}	
	
	big mm = 0;
	int mc = alfabeto.Length - 1;
	for (int i = 0; i < r; i++)
		mm = 100 * mm + mc;

	if (p*q < mm)
	{
		critical = false;
		msg.Add ("El módulo debe ser mayor que " + mm.ToString() + ".");
	}

	if (msg.Count > 0)
	{
		#Trace "La clave RSA no es correcta:\n"
		if (LATEX)
			#Trace "\n\\begin{itemize}\n"
		foreach (string s in msg)
			#Trace (LATEX ? "\\item " : ""), s, "\n"
		if (LATEX)
			#Trace "\n\\end{itemize}\n\n"
	}
	else
	{
		#Trace "La clave RSA es correcta.\n\n"
	}

	if (critical) return "Error";

	d = e.modInverse(fi);

	if (LATEX)
	{
		#Trace "$$\n\\begin{array}{|llr|}\n"
		#Trace "\\hline\n"
		#Trace "\\", "rowcolor{gray!15}\\multicolumn{3}{|l|}{\\text{Parámetros de RSA:}}\\\\\n"
		#Trace "\\hline\n"
		#Trace "p 	&=& ", p, "\\\\\n"
		#Trace "q 	&=& ", q, "\\\\\n"
		#Trace "n = p\\cdot q 	&=& ", n, "\\\\\n"
		#Trace "\\phi(n) = (p-1)\\cdot (q-1) 	&=& ", fi, "\\\\\n"
		#Trace "e 	&=& ", e, "\\\\\n"
		#Trace "d=e^{-1}\\ \\Mod\\phi(n) 	&=& ", d, "\\\\\n"
		#Trace "r 	&=& ", r, "\\\\\n"
		#Trace "\\hline\n"
		#Trace "\\end{array}\n$$\n"
	}
	else
	{
		#Trace "\nParámetros de RSA:\n---------------------------------------------------------------------------------------------------\n"
		#Trace "p = ", p, "\n"
		#Trace "q = ", q, "\n"
		#Trace "n = p\\cdot q = ", n, "\n"
		#Trace "fi = (p-1)\\cdot (q-1) = ", fi, "\n"
		#Trace "e = ", e, "\n"
		#Trace "d = e^-1 mod fi = ", d, "\n"
		#Trace "r = ", r, "\n"

	}

	string mc_e = MC(e);
	string mc_d = MC(d);
	
	if (LATEX) 
	{
		#Trace "\\vspace*{2ex}\n\nEl cifrado requiere ", mc_e.Length - 1, " multiplicaciones en cada potencia ({\\tt ", mc_e, "}).\n\n"
		#Trace "El descifrado requiere ", mc_d.Length - 1, " multiplicaciones en cada potencia ({\\tt ", mc_d, "}).\n\n"
	}
	else
	{
		#Trace "\nEl cifrado requiere ", mc_e.Length - 1, " multiplicaciones en cada potencia (", mc_e, ").\n"
		#Trace "El descifrado requiere ", mc_d.Length - 1, " multiplicaciones en cada potencia (", mc_d, ").\n\n"
	}

		
	plaintext = plaintext.Replace(' ', alfabeto[0]);

	List<big> y = Cifra(plaintext);

	string t = Descifra(y);

	return "Proceso completado";
}


big n = 0;
big d = 0;

#Return RSA()big p = new big("71");
big q = new big("43");
big e = new big("157");

big ClavesDescifrado()  
{
	#Trace clear

	big b = 0;		

	if (!probprimo(p) || !probprimo(q))
	{
		throw new Exception("p y q deben ser primos");
	}

	else
	{
		big d = modinv(e, (p-1)*(q-1));

		big m = mcm (p-1, q-1);
		big dm = modinv(e, m);
		b = (p * q - dm) / m;
 
		for (big k = 0;  k <= b; k++)
		{
			big dk = dm + k * m;
			#Trace string.Format("{0:0000}: \t {1:d} {2:s}\n", k + 1, dk, (dk == d ? "<--" : ""));
						
 		}
	}

	return b + 1;
	
}

#Return(ClavesDescifrado());// Este algoritmo busca la factorización de un módulo RSA a partir de 
// la clave pública e y la privada d.

// Introduce aquí los valores:
big n= new big("972910339982382725561968353978026889447662042645858394972661");
big e = new big("537001");
big d = new big("832813323664316820620394079611718414003096834116836440413009");



 

big modpots(big b, big e, big n)
{
	big r = modpot(b, e, n);
	if ((r+1) % n == 0) r = -1;
	return r;
}

big factoriza_n(big n, big e, big d)   
{  
	#trace clear 
	
	big t = e * d - 1; 
	big s = 0;
	while (t % 2 == 0)
	{
		s++;
		t /= 2;
	}
	#trace "n = ", n, "\n";
	#trace "e·d-1 = ", "2^", s, "·", t, "\n";
	#trace "s = ", s, "\n";
	#trace "t = ", t, "\n";

	big k = 0;

	while (true)
	{
		k++;
		
		big a = 1;
		do
		{
			a=random(2, n-1);
		}
		while  (mcd(a, n) != 1);	// Busca una unidad en Zn

		#trace "\nIntento nº", k, ": a = ", a, "\n";

		// Los casos en que a no es válido son
		// 1. a^t=1
		// 2. a^(t·2^i) = -1 para algún i=0,...,s-1
		// Nótese que para i=s, a^(t·2^i) = 1.  


		big r=0, r2=0; 
		for (int i = 1; i <= s; i++)
		{	
			if (i == 1)
			{
				r = modpots(a, modpot(2, i-1, n)*t, n);
				#trace "r0 = a^(t·2^", i-1, ") mod n = ", r;
			}
			else	
			{
				r = r2;
			}
			if (r == 1 || r == -1)
			{
				#trace " -> a no válido.\n";
				break;
			}
			else
			{
				#trace "\n";
			}
			
			r2 = modpots(r, 2, n); 
			#trace "r", i, " = a^(t·2^", i, ") mod n = ", r2;
			
			if ((r +1) % n != 0 && (r - 1) % n != 0 && (r2 - 1) % n  == 0)
			{			
				big p = mcd(r - 1, n);
				big q = n / p;
				#trace "\n\nEncontrada una \x0072aiz cuadrada no trivial módulo n de 1:  r = r", i-1, " = ", r, ".\n"
				if (n == p * q && mrtest(p) && mrtest(q))
					#trace "\nFactorización conseguida en ", k, " intento", (k == 1 ? "" : "s"), ":\n"
				#trace "p = \x006Dcd(r-1, n) = ", p, "\n";
				#trace "q = n/p         = ", q, "\n"
				return k;
				break;
			}
		}
	}

	return k; 
}  

#return factoriza_n(n, e, d); 

//#return factoriza_n(642835439, 1234567, 525018007); 


// También funciona con otras claves privadas que descifran:
// La clave verdadera es d=1753, pero con d=103 también funciona 
//#return factoriza_n(3053, 157,  103)


// Módulo n de 512 bits, producto de dos primos fuertes, de 256 bits cada uno
// p = 264419558013858955951264656593594240266297608938535751598447196665248615711057
// q = 124833390895189436749747926588317387612180897263296110964069415600747279402967
//#return factoriza_n( 
//	/* n= */	new big("33008390045877275657300736201615727804936788818361149873482348523436074819459719991661300434269855080617283655947983173313366102585982267194381891140506119"), 
//	/* e= */	new big("1756209788605960384170290023876238723630676961"),	
//	/* d= */	new big ("4942545111458383117841315209993781943346902157056207791304055760501397556906038537804787872852023989172303777208441133277702724811251027183451644148840033"));	


// Este algoritmo busca la factorización de un módulo RSA a partir de 
// el valor de la función de Euler del módulo.
 
bool factoriza_n(big n, big fi)   
{  
	#trace clear 
	
	if (n % 2 == 0)
		throw new Exception("Error: n debe ser producto de dos primos impares");
		
	big b = (n + 1 - fi) / 2; 
	big s = sqrt(b*b - n);
	
	big p = b + s;
	big q = b - s;
	
	if (p * q != n)
		throw new Exception("Error: n debe ser producto de dos primos impares");

	#Trace string.Format("{0:s} = {1:s} * {2:s}\n", n, p, q); 		
	return true;
}
		
big n = new big("26219887903992952987646921");
big fi = new big("26219887902858418765358100");	

#return factoriza_n(n, fi);

// Este algoritmo busca la factorización de un módulo RSA a partir del mcm(p-1,q-1) 
 
big modpots(big b, big e, big n)
{
	big r = modpot(b, e, n);
	if ((r+1) % n == 0) r = -1;
	return r;
}

big factoriza_n(big n, big m)   
{  
	#trace clear 
	
	big e = 0;

	do
	{
		e = random(2, m-1);
	}
	while  (mcd(e, m) != 1);	// Busca una unidad en Zm

	big d = modinv(e, m);
	
	big t = e * d - 1; 
	big s = 0;
	while (t % 2 == 0)
	{
		s++;
		t /= 2;
	}
	#trace "n = ", n, "\n";
	#trace "m = ", m, "\n";
	#trace "e = ", e, "\n";
	#trace "d = ", d, "\n";
	#trace "e·d-1 = ", "2^", s, "·", t, "\n";
	#trace "s = ", s, "\n";
	#trace "t = ", t, "\n";

	big k = 0;

	while (true)
	{
		k++;
		
		big a = 1;
		do
		{
			a=random(2, n-1);
		}
		while  (mcd(a, n) != 1);	// Busca una unidad en Zn

		#trace "\nIntento nº", k, ": a = ", a, "\n";

		// Los casos en que a no es válido son
		// 1. a^t=1
		// 2. a^(t·2^i) = -1 para algún i=0,...,s-1
		// Nótese que para i=s, a^(t·2^i) = 1.  

		big r=0, r2=0; 
		for (int i = 1; i <= s; i++)
		{	
			if (i == 1)
			{
				r = modpots(a, modpot(2, i-1, n)*t, n);
				#trace "r0 = a^(t·2^", i-1, ") mod n = ", r;
			}
			else	
			{
				r = r2;
			}
			if (r == 1 || r == -1)
			{
				#trace " -> a no válido.\n";
				break;
			}
			else
			{
				#trace "\n";
			}
			
			r2 = modpots(r, 2, n); 
			#trace "r", i, " = a^(t·2^", i, ") mod n = ", r2;
			
			if ((r +1) % n != 0 && (r - 1) % n != 0 && (r2 - 1) % n  == 0)
			{			
				big p = mcd(r - 1, n);
				big q = n / p;
				#trace "\n\nEncontrada una \x0072aiz cuadrada no trivial módulo n de 1:  r = r", i-1, " = ", r, ".\n"
				if (n == p * q && mrtest(p) && mrtest(q))
					#trace "\nFactorización conseguida en ", k, " intento", (k == 1 ? "" : "s"), ":\n"
				#trace "p = \x006Dcd(r-1, n) = ", p, "\n";
				#trace "q = n/p         = ", q, "\n"
				return k;
				break;
			}
		}
	}

	return k; 
}  

//#return factoriza_n(13*19, (12*18)/mcd(12,18)); 


#return factoriza_n(642835439, 321392328); 


// Módulo n de 512 bits, producto de dos primos fuertes, de 256 bits cada uno
// p = 264419558013858955951264656593594240266297608938535751598447196665248615711057
// q = 124833390895189436749747926588317387612180897263296110964069415600747279402967
//#return factoriza_n( 
//	/* n= */	new big("33008390045877275657300736201615727804936788818361149873482348523436074819459719991661300434269855080617283655947983173313366102585982267194381891140506119"), 
//	/* m= */	new big("16504195022938637828650368100807863902468394409180574936741174261718037409729665369356195692938577034017050872160052347403582135361709875291057947622696048"))	


big SmallKey(big n)
{
	List<big[]> l = new List<big[]>();
	
	big pq = n * n;
	big p = n.sqrt();
	if (!n.isProbablePrime())
		p = p.NextPrime();
	big rn = p;

	while (p < n)
	{
		big q = 3;
		while (q < rn)
		{
			if (p * q > n)
			{
				//if (p * q < pq)
				{
					pq = p * q;
					l.Add(new big[] {p, q, pq});
					#Trace p, "·", q, "=", p*q, "\n";
				}
			}

			q = q.NextPrime();
		}
		p = p.NextPrime();
	}

	l.Sort((x, y) => (x[2] == y[2] ? 0 : (x[2] > y[2] ? 1 : -1)));
	
	#Trace clear
	foreach(big[] x in l)
	{
		#Trace x[0], "·", x[1], "=", x[2], "\n";
	}	
	return l[0][2];
} 


/*

para n=27 obtiene 
11·3=33
7·5=35
13·3=39
17·3=51
11·5=55
19·3=57
13·5=65
23·3=69
17·5=85
19·5=95
23·5=115

para n = 2727 obtiene
911·3=2733
547·5=2735
211·13=2743
67·41=2747
919·3=2757
89·31=2759
251·11=2761
163·17=2771
59·47=2773
397·7=2779
557·5=2785
929·3=2787
401·7=2807
937·3=2811
97·29=2813
563·5=2815
941·3=2823
257·11=2827
149·19=2831
167·17=2839
947·3=2841
569·5=2845
571·5=2855
953·3=2859
409·7=2863
61·47=2867
151·19=2869
67·43=2881
577·5=2885
263·11=2893
223·13=2899
967·3=2901
71·41=2911
971·3=2913
127·23=2921
79·37=2923
101·29=2929
977·3=2931
419·7=2933
587·5=2935
173·17=2941
421·7=2947
983·3=2949
227·13=2951
269·11=2959
593·5=2965
991·3=2973
229·13=2977
271·11=2981
157·19=2983
103·29=2987
997·3=2991
73·41=2993
599·5=2995
601·5=3005
97·31=3007
131·23=3013
431·7=3017
1009·3=3027
233·13=3029
433·7=3031
607·5=3035
1013·3=3039
179·17=3043
277·11=3047
71·43=3053
1019·3=3057
1021·3=3063
613·5=3065
83·37=3071
439·7=3073
181·17=3077
617·5=3085
281·11=3091
1031·3=3093
619·5=3095
163·19=3097
1033·3=3099
443·7=3101
107·29=3103
239·13=3107
283·11=3113
1039·3=3117
101·31=3131
241·13=3133
73·43=3139
449·7=3143
1049·3=3147
67·47=3149
137·23=3151
1051·3=3153
631·5=3155
109·29=3161
167·19=3173
1061·3=3183
1063·3=3189
103·31=3193
139·23=3197
457·7=3199
641·5=3205
1069·3=3207
643·5=3215
293·11=3223
461·7=3227
647·5=3235
79·41=3239
463·7=3241
191·17=3247
1087·3=3261
251·13=3263
653·5=3265
467·7=3269
1091·3=3273
113·29=3277
1093·3=3279
193·17=3281
173·19=3287
1097·3=3291
89·37=3293
659·5=3295
661·5=3305
1103·3=3309
107·31=3317
1109·3=3327
71·47=3337
257·13=3341
197·17=3349
1117·3=3351
479·7=3353
673·5=3365
1123·3=3369
307·11=3377
109·31=3379
199·17=3383
677·5=3385
1129·3=3387
79·43=3397
179·19=3401
83·41=3403
487·7=3409
683·5=3415
263·13=3419
311·11=3421
149·23=3427
73·47=3431
491·7=3437
181·19=3439
313·11=3443
1151·3=3453
691·5=3455
1153·3=3459
151·23=3473
317·11=3487
1163·3=3489
499·7=3493
269·13=3497
113·31=3503
701·5=3505
1171·3=3513
503·7=3521
271·13=3523
1181·3=3543
709·5=3545
1187·3=3561
509·7=3563
83·43=3569
1193·3=3579
211·17=3587
97·37=3589
719·5=3595
277·13=3601
1201·3=3603
157·23=3611
191·19=3629
727·5=3635
1213·3=3639
331·11=3641
521·7=3647
89·41=3649
1217·3=3651
281·13=3653
523·7=3661
733·5=3665
193·19=3667
1223·3=3669
283·13=3679
127·29=3683
1229·3=3687
1231·3=3693
739·5=3695
337·11=3707
1237·3=3711
79·47=3713
743·5=3715
101·37=3737
197·19=3743
1249·3=3747
163·23=3749
751·5=3755
1259·3=3777
199·19=3781
757·5=3785
541·7=3787
223·17=3791
131·29=3799
761·5=3805
293·13=3809
103·37=3811
347·11=3817
89·43=3827
547·7=3829
1277·3=3831
1279·3=3837
349·11=3839
167·23=3841
769·5=3845
1283·3=3849
227·17=3859
773·5=3865
1289·3=3867
1291·3=3873
353·11=3883
1297·3=3891
229·17=3893
557·7=3899
83·47=3901
1301·3=3903
1303·3=3909
1307·3=3921
787·5=3935
127·31=3937
563·7=3941
359·11=3949
1319·3=3957
107·37=3959
233·17=3961
1321·3=3963
137·29=3973
97·41=3977
173·23=3979
1327·3=3981
569·7=3983
797·5=3985
307·13=3991
571·7=3997
211·19=4009
139·29=4031
109·37=4033
367·11=4037
577·7=4039
311·13=4043
809·5=4045
811·5=4055
131·31=4061
239·17=4063
313·13=4069
1361·3=4083
241·17=4097
1367·3=4101
373·11=4103
821·5=4105
587·7=4109
823·5=4115
179·23=4117
1373·3=4119
317·13=4121
827·5=4135
101·41=4141
1381·3=4143
829·5=4145
593·7=4151
181·23=4163
379·11=4169
97·43=4171
113·37=4181
89·47=4183
599·7=4193
839·5=4195
1399·3=4197
601·7=4207
383·11=4213
103·41=4223
1409·3=4227
223·19=4237
137·31=4247
607·7=4249
853·5=4265
251·17=4267
1423·3=4269
389·11=4279
1427·3=4281
857·5=4285
1429·3=4287
613·7=4291
859·5=4295
1433·3=4299
331·13=4303
139·31=4309
227·19=4313
863·5=4315
1439·3=4317
617·7=4319
149·29=4321
619·7=4333
1447·3=4341
101·43=4343
229·19=4351
1451·3=4353
1453·3=4359
397·11=4367
257·17=4369
1459·3=4377
151·29=4379
337·13=4381
877·5=4385
107·41=4387
191·23=4393
881·5=4405
401·11=4411
1471·3=4413
883·5=4415
631·7=4417
233·19=4427
103·43=4429
887·5=4435
193·23=4439
1481·3=4443
1483·3=4449
1487·3=4461
1489·3=4467
109·41=4469
263·17=4471
1493·3=4479
641·7=4487
1499·3=4497
409·11=4499
643·7=4501
347·13=4511
647·7=4529
197·23=4531
1511·3=4533
907·5=4535
349·13=4537
239·19=4541
157·29=4553
911·5=4555
97·47=4559
1523·3=4569
653·7=4571
269·17=4573
199·23=4577
241·19=4579
353·13=4589
1531·3=4593
919·5=4595
107·43=4601
271·17=4607
419·11=4609
659·7=4613
149·31=4619
661·7=4627
1543·3=4629
421·11=4631
113·41=4633
929·5=4645
1549·3=4647
1553·3=4659
359·13=4667
1559·3=4677
151·31=4681
937·5=4685
109·43=4687
127·37=4699
1567·3=4701
941·5=4705
277·17=4709
673·7=4711
1571·3=4713
163·29=4727
947·5=4735
1579·3=4737
677·7=4739
431·11=4741
101·47=4747
1583·3=4749
433·11=4763
953·5=4765
251·19=4769
367·13=4771
281·17=4777
683·7=4781
1597·3=4791
1601·3=4803
283·17=4811
1607·3=4821
1609·3=4827
439·11=4829
967·5=4835
691·7=4837
1613·3=4839
103·47=4841
167·29=4843
131·37=4847
373·13=4849
211·23=4853
971·5=4855
1619·3=4857
113·43=4859
1621·3=4863
157·31=4867
443·11=4873
1627·3=4881
257·19=4883
977·5=4885
701·7=4907
1637·3=4911
983·5=4915
379·13=4927
449·11=4939
991·5=4955
709·7=4963
1657·3=4971
383·13=4979
293·17=4981
997·5=4985
1663·3=4989
263·19=4997
1667·3=5001
1669·3=5007
173·29=5017
457·11=5027
107·47=5029
719·7=5033
1009·5=5045
163·31=5053
389·13=5057
1013·5=5065
137·37=5069
461·11=5071
1693·3=5079
727·7=5089
1697·3=5091
463·11=5093
1019·5=5095
1699·3=5097
1021·5=5105
269·19=5111
109·47=5123
1709·3=5127
223·23=5129
733·7=5131
467·11=5137
139·37=5143
271·19=5149
1031·5=5155
397·13=5161
1721·3=5163
1033·5=5165
1723·3=5169
739·7=5173
167·31=5177
179·29=5191
1039·5=5195
1733·3=5199
743·7=5201
127·41=5207
401·13=5213
307·17=5219
227·23=5221
1741·3=5223
1747·3=5241
1049·5=5245
181·29=5249
1051·5=5255
751·7=5257
1753·3=5259
277·19=5263
229·23=5267
479·11=5269
1759·3=5277
311·17=5287
757·7=5299
1061·5=5305
113·47=5311
1063·5=5315
409·13=5317
313·17=5321
761·7=5327
1777·3=5331
281·19=5339
1069·5=5345
1783·3=5349
487·11=5357
233·23=5359
1787·3=5361
173·31=5363
1789·3=5367
131·41=5371
283·19=5377
769·7=5383
317·17=5389
491·11=5401
1801·3=5403
773·7=5411
1811·3=5433
1087·5=5435
419·13=5447
1091·5=5455
127·43=5461
1093·5=5465
1823·3=5469
421·13=5473
1097·5=5485
499·11=5489
1831·3=5493
239·23=5497
787·7=5509
149·37=5513
1103·5=5515
503·11=5533
191·29=5539
1847·3=5541
241·23=5543
1109·5=5545
179·31=5549
293·19=5567
797·7=5579
1861·3=5583
1117·5=5585
151·37=5587
193·29=5597
509·11=5599
1867·3=5601
431·13=5603
181·31=5611
1871·3=5613
1123·5=5615
137·41=5617
1873·3=5619
331·17=5627
433·13=5629
1877·3=5631
131·43=5633
1879·3=5637
1129·5=5645
809·7=5663
1889·3=5667
811·7=5677
139·41=5699
1901·3=5703
439·13=5707
197·29=5713
1907·3=5721
337·17=5729
521·11=5731
1913·3=5739
821·7=5747
523·11=5753
1151·5=5755
443·13=5759
823·7=5761
1153·5=5765
199·29=5771
251·23=5773
827·7=5789
1931·3=5793
1933·3=5799
829·7=5803
157·37=5809
1163·5=5815
307·19=5833
449·13=5837
1949·3=5847
1951·3=5853
1171·5=5855
839·7=5873
137·43=5891
347·17=5899
1181·5=5905
311·19=5909
257·23=5911
1973·3=5919
191·31=5921
349·17=5933
1187·5=5935
1979·3=5937
457·13=5941
313·19=5947
541·11=5951
1987·3=5961
1193·5=5965
127·47=5969
853·7=5971
139·43=5977
1993·3=5979
193·31=5983
1997·3=5991
461·13=5993
1999·3=5997
857·7=5999
353·17=6001
1201·5=6005
2003·3=6009
859·7=6013
547·11=6017
463·13=6019
317·19=6023
163·37=6031
2011·3=6033
863·7=6041
263·23=6049
2017·3=6051
1213·5=6065
467·13=6071
2027·3=6081
1217·5=6085
2029·3=6087
359·17=6103
197·31=6107
149·41=6109
1223·5=6115
2039·3=6117
211·29=6119
557·11=6127
877·7=6139
1229·5=6145
1231·5=6155
131·47=6157
2053·3=6159
881·7=6167
199·31=6169
167·37=6179
883·7=6181
1237·5=6185
269·23=6187
2063·3=6189
151·41=6191
563·11=6193
2069·3=6207
887·7=6209
479·13=6227
271·23=6233
367·17=6239
2081·3=6243
1249·5=6245
2083·3=6249
569·11=6259
2087·3=6261
2089·3=6267
571·11=6281
331·19=6289
1259·5=6295
2099·3=6297
487·13=6331
2111·3=6333
2113·3=6339
373·17=6341
577·11=6347
907·7=6349
277·23=6371
911·7=6377
491·13=6383
1277·5=6385
2129·3=6387
2131·3=6393
1279·5=6395
173·37=6401
337·19=6403
149·43=6407
2137·3=6411
1283·5=6415
2141·3=6423
2143·3=6429
919·7=6433
157·41=6437
137·47=6439
379·17=6443
1289·5=6445
1291·5=6455
587·11=6457
2153·3=6459
281·23=6463
223·29=6467
2161·3=6483
1297·5=6485
499·13=6487
151·43=6493
929·7=6503
1301·5=6505
283·23=6509
383·17=6511
1303·5=6515
593·11=6523
139·47=6533
1307·5=6535
2179·3=6537
503·13=6539
211·31=6541
937·7=6559
227·29=6583
941·7=6587
599·11=6589
347·19=6593
1319·5=6595
1321·5=6605
2203·3=6609
601·11=6611
389·17=6613
509·13=6617
2207·3=6621
179·37=6623
947·7=6629
349·19=6631
1327·5=6635
2213·3=6639
229·29=6641
2221·3=6663
953·7=6671
607·11=6677
163·41=6683
181·37=6697
353·19=6707
2237·3=6711
2239·3=6717
2243·3=6729
293·23=6739
613·11=6743
397·17=6749
157·43=6751
2251·3=6753
233·29=6757
967·7=6769
521·13=6773
617·11=6787
971·7=6797
523·13=6799
2267·3=6801
1361·5=6805
2269·3=6807
619·11=6809
401·17=6817
2273·3=6819
359·19=6821
1367·5=6835
977·7=6839
2281·3=6843
167·41=6847
2287·3=6861
1373·5=6865
2293·3=6879
983·7=6881
2297·3=6891
1381·5=6905
223·31=6913
2309·3=6927
239·29=6931
2311·3=6933
991·7=6937
631·11=6941
409·17=6953
367·19=6973
997·7=6979
241·29=6989
1399·5=6995
2333·3=6999
149·47=7003
163·43=7009
2339·3=7017
2341·3=7023
541·13=7033
227·31=7037
2347·3=7041
1409·5=7045
641·11=7051
2351·3=7053
307·23=7061
1009·7=7063
191·37=7067
2357·3=7071
643·11=7073
373·19=7087
1013·7=7091
173·41=7093
151·47=7097
229·31=7099
547·13=7111
2371·3=7113
1423·5=7115
647·11=7117
419·17=7123
2377·3=7131
1019·7=7133
1427·5=7135
193·37=7141
2381·3=7143
1429·5=7145
1021·7=7147
2383·3=7149
311·23=7153
421·17=7157
1433·5=7165
2389·3=7167
2393·3=7179
167·43=7181
653·11=7183
1439·5=7195
2399·3=7197
313·23=7199
379·19=7201
1031·7=7217
233·31=7223
1033·7=7231
2411·3=7233
1447·5=7235
557·13=7241
659·11=7249
2417·3=7251
1451·5=7255
1453·5=7265
2423·3=7269
661·11=7271
1039·7=7273
383·19=7277
251·29=7279
197·37=7289
317·23=7291
1459·5=7295
2437·3=7311
563·13=7319
2441·3=7323
431·17=7327
179·41=7339
2447·3=7341
1049·7=7343
1471·5=7355
1051·7=7357
433·17=7361
199·37=7363
2459·3=7377
157·47=7379
389·19=7391
569·13=7397
2467·3=7401
673·11=7403
1481·5=7405
239·31=7409
1483·5=7415
2473·3=7419
181·41=7421
571·13=7423
1061·7=7427
2477·3=7431
1487·5=7435
173·43=7439
1063·7=7441
1489·5=7445
677·11=7447
257·29=7453
439·17=7463
1493·5=7465
241·31=7471
1069·7=7483
1499·5=7495
577·13=7501
2503·3=7509
683·11=7513
443·17=7531
397·19=7543
1511·5=7555
2521·3=7563
2531·3=7593
691·11=7601
1087·7=7609
331·23=7613
1523·5=7615
2539·3=7617
401·19=7619
263·29=7627
2543·3=7629
587·13=7631
449·17=7633
1091·7=7637
2549·3=7647
1093·7=7651
2551·3=7653
1531·5=7655
163·47=7661
2557·3=7671
1097·7=7679
179·43=7697
593·13=7709
701·11=7711
1543·5=7715
1103·7=7721
2579·3=7737
1549·5=7745
337·23=7751
1109·7=7763
1553·5=7765
457·17=7769
409·19=7771
2591·3=7773
2593·3=7779
251·31=7781
181·43=7783
599·13=7787
1559·5=7795
709·11=7799
269·29=7801
211·37=7807
601·13=7813
1117·7=7819
2609·3=7827
191·41=7831
1567·5=7835
461·17=7837
167·47=7849
2617·3=7851
1571·5=7855
271·29=7859
1123·7=7861
2621·3=7863
463·17=7871
607·13=7891
1579·5=7895
2633·3=7899
1129·7=7903
719·11=7909
193·41=7913
1583·5=7915
467·17=7939
2647·3=7941
419·19=7961
257·31=7967
613·13=7969
2657·3=7971
2659·3=7977
347·23=7981
1597·5=7985
2663·3=7989
727·11=7997
421·19=7999
1601·5=8005
2671·3=8013
617·13=8021
349·23=8027
2677·3=8031
277·29=8033
1607·5=8035
1609·5=8045
619·13=8047
2683·3=8049
1151·7=8057
2687·3=8061
733·11=8063
1613·5=8065
2689·3=8067
1153·7=8071
197·41=8077
2693·3=8079
1619·5=8095
2699·3=8097
1621·5=8105
353·23=8119
2707·3=8121
739·11=8129
173·47=8131
2711·3=8133
1627·5=8135
2713·3=8139
1163·7=8141
479·17=8143
281·29=8149
263·31=8153
2719·3=8157
199·41=8159
743·11=8173
1637·5=8185
431·19=8189
1171·7=8197
631·13=8203
283·29=8207
191·43=8213
433·19=8227
223·37=8251
359·23=8257
751·11=8261
1181·7=8267
487·17=8279
1657·5=8285
193·43=8299
1187·7=8309
1663·5=8315
757·11=8327
641·13=8333
1667·5=8335
269·31=8339
439·19=8341
1669·5=8345
491·17=8347
1193·7=8351
643·13=8359
761·11=8371
227·37=8399
271·31=8401
1201·7=8407
647·13=8411
179·47=8413
443·19=8417
367·23=8441
769·11=8459
1693·5=8465
197·43=8471
229·37=8473
499·17=8483
1697·5=8485
653·13=8489
1213·7=8491
1699·5=8495
293·29=8497
773·11=8503
181·47=8507
1217·7=8519
449·19=8531
1709·5=8545
503·17=8551
199·43=8557
1223·7=8561
659·13=8567
373·23=8579
277·31=8587
661·13=8593
1229·7=8603
1721·5=8605
1723·5=8615
1231·7=8617
233·37=8621
211·41=8651
509·17=8653
787·11=8657
1237·7=8659
1733·5=8665
457·19=8683
1741·5=8705
281·31=8711
379·23=8717
1747·5=8735
1249·7=8743
673·13=8749
461·19=8759
1753·5=8765
797·11=8767
283·31=8773
1759·5=8795
463·19=8797
677·13=8801
383·23=8809
1259·7=8813
239·37=8843
521·17=8857
467·19=8873
683·13=8879
1777·5=8885
523·17=8891
809·11=8899
307·29=8903
1783·5=8915
241·37=8917
811·11=8921
1787·5=8935
1277·7=8939
1789·5=8945
389·23=8947
1279·7=8953
191·47=8977
1283·7=8981
691·13=8983
1801·5=9005
311·29=9019
1289·7=9023
821·11=9031
1291·7=9037
823·11=9053
1811·5=9055
193·47=9071
211·43=9073
313·29=9077
1297·7=9079
293·31=9083
827·11=9097
479·19=9101
1301·7=9107
701·13=9113
1823·5=9115
829·11=9119
1303·7=9121
397·23=9131
223·41=9143
1307·7=9149
1831·5=9155
317·29=9193
541·17=9197
709·13=9217
401·23=9223
839·11=9229
1319·7=9233
1847·5=9235
1321·7=9247
487·19=9253
197·47=9259
251·37=9287
1327·7=9289
547·17=9299
1861·5=9305
227·41=9307
491·19=9329
1867·5=9335
719·13=9347
199·47=9353
1871·5=9355
1873·5=9365
853·11=9383
1877·5=9385
229·41=9389
1879·5=9395
409·23=9407
857·11=9427
1889·5=9445
859·11=9449
727·13=9451
557·17=9469
499·19=9481
863·11=9493
1901·5=9505
257·37=9509
307·31=9517
1361·7=9527
733·13=9529
1907·5=9535
233·41=9553
503·19=9557
1913·5=9565
1367·7=9569
563·17=9571
223·43=9589
331·29=9599
739·13=9607
1373·7=9611
419·23=9637
311·31=9641
877·11=9647
1931·5=9655
743·13=9659
1933·5=9665
1381·7=9667
509·19=9671
569·17=9673
421·23=9683
881·11=9691
313·31=9703
571·17=9707
883·11=9713
263·37=9731
1949·5=9745
1951·5=9755
887·11=9757
227·43=9761
751·13=9763
337·29=9773
1399·7=9793
239·41=9799
577·17=9809
317·31=9827
757·13=9841
229·43=9847
1409·7=9863
1973·5=9865
241·41=9881
761·13=9893
1979·5=9895
521·19=9899
431·23=9913
211·47=9917
1987·5=9935
523·19=9937
269·37=9953
433·23=9959
1423·7=9961
1993·5=9965
907·11=9977
587·17=9979
1997·5=9985
1427·7=9989
1999·5=9995
769·13=9997
1429·7=10003
2003·5=10015
233·43=10019
911·11=10021
271·37=10027
1433·7=10031
773·13=10049
2011·5=10055
347·29=10063
1439·7=10073
593·17=10081
2017·5=10085
439·23=10097
919·11=10109
349·29=10121
1447·7=10129
2027·5=10135
2029·5=10145
1451·7=10157
1453·7=10171
599·17=10183
443·23=10189
2039·5=10195
1459·7=10213
601·17=10217
929·11=10219
787·13=10231
353·29=10237
277·37=10249
331·31=10261
2053·5=10265
239·43=10277
541·19=10279
251·41=10291
1471·7=10297
937·11=10307
2063·5=10315
607·17=10319
449·23=10327
2069·5=10345
941·11=10351
797·13=10361
241·43=10363
1481·7=10367
1483·7=10381
547·19=10393
281·37=10397
2081·5=10405
1487·7=10409
359·29=10411
2083·5=10415
947·11=10417
613·17=10421
1489·7=10423
2087·5=10435
2089·5=10445
337·31=10447
1493·7=10451
283·37=10471
223·47=10481
953·11=10483
617·17=10489
1499·7=10493
2099·5=10495
457·23=10511
809·13=10517
619·17=10523
257·41=10537
811·13=10543
2111·5=10555
2113·5=10565
1511·7=10577
557·19=10583
461·23=10603
967·11=10637
367·29=10643
2129·5=10645
463·23=10649
2131·5=10655
1523·7=10661
227·47=10669
821·13=10673
971·11=10681
2137·5=10685
563·19=10697
823·13=10699
2141·5=10705
2143·5=10715
1531·7=10717
631·17=10727
467·23=10741
977·11=10747
827·13=10751
347·31=10757
229·47=10763
2153·5=10765
829·13=10777
263·41=10783
251·43=10793
1543·7=10801
2161·5=10805
569·19=10811
983·11=10813
373·29=10817
349·31=10819
293·37=10841
1549·7=10843
571·19=10849
1553·7=10871
2179·5=10895
641·17=10897
991·11=10901
839·13=10907
1559·7=10913
643·17=10931
353·31=10943
233·47=10951
577·19=10963
997·11=10967
1567·7=10969
379·29=10991
1571·7=10997
647·17=10999
2203·5=11015
479·23=11017
269·41=11029
2207·5=11035
257·43=11051
1579·7=11053
2213·5=11065
1583·7=11081
853·13=11089
1009·11=11099
653·17=11101
2221·5=11105
383·29=11107
271·41=11111
359·31=11129
857·13=11141
1013·11=11143
587·19=11153
859·13=11167
1597·7=11179
2237·5=11185
2239·5=11195
487·23=11201
659·17=11203
1601·7=11207
1019·11=11209
2243·5=11215
863·13=11219
1021·11=11231
239·47=11233
661·17=11237
1607·7=11249
2251·5=11255
1609·7=11263
593·19=11267
389·29=11281
1613·7=11291
491·23=11293
263·43=11309
241·47=11327
1619·7=11333
2267·5=11335
1031·11=11341
2269·5=11345
1621·7=11347
277·41=11357
307·37=11359
1033·11=11363
2273·5=11365
367·31=11377
599·19=11381
1627·7=11389
877·13=11401
2281·5=11405
601·19=11419
1039·11=11429
2287·5=11435
673·17=11441
881·13=11453
1637·7=11459
2293·5=11465
499·23=11477
883·13=11479
2297·5=11485
311·37=11507
677·17=11509
397·29=11513
281·41=11521
887·13=11531
607·19=11533
1049·11=11539
2309·5=11545
2311·5=11555
1051·11=11561
373·31=11563
269·43=11567
503·23=11569
313·37=11581
1657·7=11599
283·41=11603
683·17=11611
401·29=11629
1663·7=11641
613·19=11647
271·43=11653
2333·5=11665
1667·7=11669
1061·11=11671
1669·7=11683
1063·11=11693
2339·5=11695
2341·5=11705
509·23=11707
617·19=11723
317·37=11729
2347·5=11735
691·17=11747
379·31=11749
2351·5=11755
1069·11=11759
619·19=11761
2357·5=11785
907·13=11791
251·47=11797
911·13=11843
1693·7=11851
2371·5=11855
409·29=11861
383·31=11873
1697·7=11879
2377·5=11885
1699·7=11893
2381·5=11905
277·43=11911
2383·5=11915
701·17=11917
2389·5=11945
919·13=11947
1087·11=11957
1709·7=11963
2393·5=11965
521·23=11983
631·19=11989
2399·5=11995
1091·11=12001
293·41=12013
1093·11=12023
523·23=12029
1721·7=12047
709·17=12053
2411·5=12055
389·31=12059
1723·7=12061
1097·11=12067
929·13=12077
257·47=12079
281·43=12083
2417·5=12085
2423·5=12115
1733·7=12131
1103·11=12133
419·29=12151
283·43=12169
641·19=12179
937·13=12181
2437·5=12185
1741·7=12187
1109·11=12199
2441·5=12205
421·29=12209
643·19=12217
719·17=12223
1747·7=12229
941·13=12233
2447·5=12235
331·37=12247
1753·7=12271
1117·11=12287
647·19=12293
2459·5=12295
397·31=12307
947·13=12311
1759·7=12313
2467·5=12335
1123·11=12353
727·17=12359
263·47=12361
2473·5=12365
2477·5=12385
953·13=12389
653·19=12407
1129·11=12419
401·31=12431
1777·7=12439
541·23=12443
733·17=12461
337·37=12469
1783·7=12481
431·29=12499
1787·7=12509
2503·5=12515
659·19=12521
1789·7=12523
433·29=12557
661·19=12559
739·17=12563
967·13=12571
547·23=12581
307·41=12587
293·43=12599
2521·5=12605
1801·7=12607
971·13=12623
743·17=12631
269·47=12643
2531·5=12655
1151·11=12661
1811·7=12677
409·31=12679
1153·11=12683
2539·5=12695
977·13=12701
2543·5=12715
439·29=12731
271·47=12737
2549·5=12745
311·41=12751
2551·5=12755
1823·7=12761
751·17=12767
983·13=12779
2557·5=12785
673·19=12787
1163·11=12793
557·23=12811
1831·7=12817
313·41=12833
347·37=12839
443·29=12847
677·19=12863
757·17=12869
1171·11=12881
991·13=12883
2579·5=12895
349·37=12913
1847·7=12929
761·17=12937
563·23=12949
2591·5=12955
997·13=12961
2593·5=12965
683·19=12977
419·31=12989
1181·11=12991
317·41=12997
277·47=13019
449·29=13021
1861·7=13027
2609·5=13045
421·31=13051
1187·11=13057
353·37=13061
1867·7=13069
769·17=13073
2617·5=13085
569·23=13087
1871·7=13097
2621·5=13105
1873·7=13111
1009·13=13117
1193·11=13123
691·19=13129
571·23=13133
1877·7=13139
773·17=13141
1879·7=13153
2633·5=13165
1013·13=13169
307·43=13201
281·47=13207
1201·11=13211
1889·7=13223
2647·5=13235
1019·13=13247
457·29=13253
577·23=13271
1021·13=13273
359·37=13283
2657·5=13285
2659·5=13295
283·47=13301
1901·7=13307
2663·5=13315
701·19=13319
1213·11=13343
1907·7=13349
2671·5=13355
431·31=13361
461·29=13369
311·43=13373
787·17=13379
2677·5=13385
1217·11=13387
1913·7=13391
1031·13=13403
2683·5=13415
433·31=13423
463·29=13427
1033·13=13429
2687·5=13435
2689·5=13445
1223·11=13453
313·43=13459
2693·5=13465
709·19=13471
2699·5=13495
587·23=13501
1039·13=13507
1931·7=13517
1229·11=13519
1933·7=13531
2707·5=13535
1231·11=13541
467·29=13543
797·17=13549
2711·5=13555
2713·5=13565
331·41=13571
367·37=13579
2719·5=13595
1237·11=13607
439·31=13609
317·43=13631
1049·13=13637
593·23=13639
1949·7=13643
1951·7=13657
719·19=13661
1051·13=13663
443·31=13733
1249·11=13739
809·17=13753
293·47=13771
599·23=13777
811·17=13787
1061·13=13793
373·37=13801
1973·7=13811
727·19=13813
337·41=13817
1063·13=13819
601·23=13823
1259·11=13849
1979·7=13853
479·29=13891
1069·13=13897
1987·7=13909
449·31=13919
733·19=13927
1993·7=13951
821·17=13957
607·23=13961
1997·7=13979
823·17=13991
1999·7=13993
2003·7=14021
379·37=14023
739·19=14041
1277·11=14047
827·17=14059
1279·11=14069
2011·7=14077
829·17=14093
613·23=14099
1283·11=14113
743·19=14117
2017·7=14119
487·29=14123
1087·13=14131
457·31=14167
383·37=14171
1289·11=14179
1091·13=14183
2027·7=14189
617·23=14191
1291·11=14201
2029·7=14203
1093·13=14209
347·41=14227
331·43=14233
619·23=14237
491·29=14239
1097·13=14261
839·17=14263
1297·11=14267
751·19=14269
2039·7=14273
461·31=14291
349·41=14309
1301·11=14311
1303·11=14333
1103·13=14339
463·31=14353
2053·7=14371
1307·11=14377
757·19=14383
389·37=14393
1109·13=14417
307·47=14429
2063·7=14441
761·19=14459
499·29=14471
353·41=14473
467·31=14477
2069·7=14483
337·43=14491
853·17=14501
1319·11=14509
631·23=14513
1117·13=14521
1321·11=14531
2081·7=14567
857·17=14569
2083·7=14581
503·29=14587
1327·11=14597
1123·13=14599
859·17=14603
2087·7=14609
769·19=14611
311·47=14617
2089·7=14623
863·17=14671
1129·13=14677
773·19=14687
397·37=14689
2099·7=14693
313·47=14711
359·41=14719
641·23=14743
509·29=14761
2111·7=14777
643·23=14789
2113·7=14791
401·37=14837
479·31=14849
647·23=14881
317·47=14899
2129·7=14903
877·17=14909
2131·7=14917
347·43=14921
787·19=14953
2137·7=14959
1151·13=14963
1361·11=14971
881·17=14977
2141·7=14987
1153·13=14989
2143·7=15001
349·43=15007
883·17=15011
653·23=15019
1367·11=15037
367·41=15047
2153·7=15071
887·17=15079
487·31=15097
1373·11=15103
521·29=15109
1163·13=15119
2161·7=15127
409·37=15133
797·19=15143
659·23=15157
523·29=15167
353·43=15179
1381·11=15191
661·23=15203
491·31=15221
1171·13=15223
2179·7=15253
373·41=15293
1181·13=15353
809·19=15371
1399·11=15389
811·19=15409
907·17=15419
2203·7=15421
1187·13=15431
359·43=15437
2207·7=15449
499·31=15469
673·23=15479
911·17=15487
2213·7=15491
1409·11=15499
419·37=15503
1193·13=15509
379·41=15539
2221·7=15547
331·47=15557
677·23=15571
421·37=15577
503·31=15593
821·19=15599
1201·13=15613
919·17=15623
823·19=15637
1423·11=15653
2237·7=15659
2239·7=15673
541·29=15689
1427·11=15697
2243·7=15701
383·41=15703
683·23=15709
827·19=15713
1429·11=15719
829·19=15751
2251·7=15757
1433·11=15763
1213·13=15769
509·31=15779
367·43=15781
929·17=15793
1217·13=15821
1439·11=15829
337·47=15839
547·29=15863
2267·7=15869
2269·7=15883
691·23=15893
1223·13=15899
2273·7=15911
1447·11=15917
937·17=15929
839·19=15941
431·37=15947
389·41=15949
1451·11=15961
2281·7=15967
1229·13=15977
1453·11=15983
941·17=15997
1231·13=16003
2287·7=16009
433·37=16021
373·43=16039
1459·11=16049
2293·7=16051
2297·7=16079
1237·13=16081
947·17=16099
701·23=16123
521·31=16151
557·29=16153
2309·7=16163
2311·7=16177
1471·11=16181
953·17=16201
853·19=16207
523·31=16213
1249·13=16237
439·37=16243
397·41=16277
857·19=16283
1481·11=16291
379·43=16297
709·23=16307
347·47=16309
1483·11=16313
859·19=16321
563·29=16327
2333·7=16331
1487·11=16357
1259·13=16367
2339·7=16373
1489·11=16379
2341·7=16387
443·37=16391
863·19=16397
349·47=16403
1493·11=16423
2347·7=16429
967·17=16439
401·41=16441
2351·7=16457
383·43=16469
1499·11=16489
2357·7=16499
569·29=16501
971·17=16507
719·23=16537
571·29=16559
353·47=16591
2371·7=16597
1277·13=16601
977·17=16609
449·37=16613
1511·11=16621
1279·13=16627
2377·7=16639
877·19=16663
2381·7=16667
1283·13=16679
2383·7=16681
983·17=16711
727·23=16721
2389·7=16723
389·43=16727
577·29=16733
881·19=16739
2393·7=16751
1523·11=16753
1289·13=16757
409·41=16769
541·31=16771
883·19=16777
1291·13=16783
2399·7=16793
1531·11=16841
991·17=16847
887·19=16853
733·23=16859
1297·13=16861
359·47=16873
2411·7=16877
457·37=16909
1301·13=16913
2417·7=16919
1303·13=16939
997·17=16949
547·31=16957
2423·7=16961
1543·11=16973
1307·13=16991
739·23=16997
587·29=17023
1549·11=17039
461·37=17057
2437·7=17059
397·43=17071
1553·11=17083
2441·7=17087
743·23=17089
2447·7=17129
463·37=17131
1319·13=17147
1559·11=17149
1009·17=17153
1321·13=17173
419·41=17179
593·29=17197
2459·7=17213
1013·17=17221
907·19=17233
1567·11=17237
401·43=17243
367·47=17249
1327·13=17251
421·41=17261
557·31=17267
2467·7=17269
751·23=17273
467·37=17279
1571·11=17281
911·19=17309
2473·7=17311
1019·17=17323
2477·7=17339
1021·17=17357
1579·11=17369
599·29=17371
757·23=17411
1583·11=17413
601·29=17429
563·31=17453
919·19=17461
761·23=17503
2503·7=17521
1031·17=17527
373·47=17531
1033·17=17561
1597·11=17567
409·43=17587
607·29=17603
1601·11=17611
569·31=17639
2521·7=17647
929·19=17651
1039·17=17663
431·41=17671
1607·11=17677
769·23=17687
1361·13=17693
1609·11=17699
571·31=17701
2531·7=17717
479·37=17723
1613·11=17743
433·41=17753
1367·13=17771
2539·7=17773
613·29=17777
773·23=17779
2543·7=17801
937·19=17803
1619·11=17809
379·47=17813
1621·11=17831
1049·17=17833
2549·7=17843
1373·13=17849
2551·7=17857
1051·17=17867
941·19=17879
577·31=17887
617·29=17893
1627·11=17897
2557·7=17899
619·29=17951
1381·13=17953
947·19=17993
439·41=17999
383·47=18001
1637·11=18007
419·43=18017
487·37=18019
1061·17=18037
2579·7=18053
1063·17=18071
787·23=18101
421·43=18103
953·19=18107
2591·7=18137
2593·7=18151
443·41=18163
491·37=18167
1069·17=18173
1399·13=18187
587·31=18197
1657·11=18227
2609·7=18263
389·47=18283
1663·11=18293
631·29=18299
1409·13=18317
2617·7=18319
797·23=18331
1667·11=18337
2621·7=18347
1669·11=18359
967·19=18373
593·31=18383
449·41=18409
2633·7=18431
971·19=18449
499·37=18463
1087·17=18479
1423·13=18499
2647·7=18529
431·43=18533
1091·17=18547
1427·13=18551
977·19=18563
599·31=18569
1429·13=18577
1093·17=18581
641·29=18589
2657·7=18599
809·23=18607
503·37=18611
2659·7=18613
433·43=18619
1693·11=18623
1433·13=18629
601·31=18631
2663·7=18641
643·29=18647
1097·17=18649
811·23=18653
397·47=18659
1697·11=18667
983·19=18677
1699·11=18689
2671·7=18697
1439·13=18707
457·41=18737
2677·7=18739
1103·17=18751
647·29=18763
2683·7=18781
1709·11=18799
2687·7=18809
1447·13=18811
607·31=18817
2689·7=18823
991·19=18829
509·37=18833
401·47=18847
2693·7=18851
1109·17=18853
1451·13=18863
439·43=18877
821·23=18883
1453·13=18889
2699·7=18893
461·41=18901
823·23=18929
1721·11=18931
653·29=18937
997·19=18943
2707·7=18949
1723·11=18953
1459·13=18967
2711·7=18977
463·41=18983
1117·17=18989
2713·7=18991
613·31=19003
827·23=19021
2719·7=19033
443·43=19049
1733·11=19063
829·23=19067
1123·17=19091
659·29=19111
1471·13=19123
617·31=19127
467·41=19147
1741·11=19151
661·29=19169
1009·19=19171
619·31=19189
1129·17=19193
1747·11=19217
409·47=19223
1013·19=19247
1481·13=19253
521·37=19277
1483·13=19279
1753·11=19283
839·23=19297
449·43=19307
1487·13=19331
1759·11=19349
523·37=19351
1489·13=19357
1019·19=19361
1021·19=19399
1493·13=19409
1499·13=19487
673·29=19517
1777·11=19547
631·31=19561
1151·17=19567
1031·19=19589
1153·17=19601
1783·11=19613
853·23=19619
1033·19=19627
677·29=19633
479·41=19639
1511·13=19643
457·43=19651
1787·11=19657
1789·11=19679
419·47=19693
857·23=19711
1039·19=19741
859·23=19757
1163·17=19771
421·47=19787
1523·13=19799
683·29=19807
1801·11=19811
461·43=19823
863·23=19849
641·31=19871
1531·13=19903
1171·17=19907
463·43=19909
1811·11=19921
1049·19=19931
643·31=19933
487·41=19967
1051·19=19969
541·37=20017
691·29=20039
1823·11=20053
647·31=20057
1543·13=20059
1181·17=20077
467·43=20081
491·41=20131
1549·13=20137
1831·11=20141
1061·19=20159
877·23=20171
1187·17=20179
1553·13=20189
1063·19=20197
547·37=20239
653·31=20243
431·47=20257
881·23=20263
1559·13=20267
1193·17=20281
883·23=20309
1069·19=20311
1847·11=20317
701·29=20329
433·47=20351
1567·13=20371
887·23=20401
1201·17=20417
1571·13=20423
659·31=20429
499·41=20459
1861·11=20471
661·31=20491
1579·13=20527
1867·11=20537
709·29=20561
1583·13=20579
1871·11=20581
479·43=20597
1873·11=20603
557·37=20609
1213·17=20621
503·41=20623
439·47=20633
1877·11=20647
1087·19=20653
1879·11=20669
1217·17=20689
1091·19=20729
1597·13=20761
1093·19=20767
1889·11=20779
1223·17=20791
1601·13=20813
443·47=20821
563·37=20831
1097·19=20843
719·29=20851
907·23=20861
673·31=20863
509·41=20869
1607·13=20891
1229·17=20893
1901·11=20911
1609·13=20917
1231·17=20927
487·43=20941
911·23=20953
1103·19=20957
1613·13=20969
1907·11=20977
677·31=20987
1237·17=21029
1913·11=21043
1619·13=21047
569·37=21053
1109·19=21071
1621·13=21073
727·29=21083
449·47=21103
491·43=21113
571·37=21127
919·23=21137
1627·13=21151
683·31=21173
1117·19=21223
1249·17=21233
1931·11=21241
733·29=21257
1933·11=21263
1637·13=21281
1123·19=21337
577·37=21349
521·41=21361
929·23=21367
1259·17=21403
691·31=21421
739·29=21431
1949·11=21439
523·41=21443
1129·19=21451
499·43=21457
1951·11=21461
457·47=21479
1657·13=21541
743·29=21547
937·23=21551
1663·13=21619
503·43=21629
941·23=21643
461·47=21667
1667·13=21671
1669·13=21697
1973·11=21703
1277·17=21709
587·37=21719
701·31=21731
1279·17=21743
463·47=21761
1979·11=21769
751·29=21779
947·23=21781
1283·17=21811
1987·11=21857
1151·19=21869
509·43=21887
1153·19=21907
1289·17=21913
953·23=21919
1993·11=21923
593·37=21941
1291·17=21947
467·47=21949
757·29=21953
1997·11=21967
709·31=21979
1999·11=21989
1693·13=22009
2003·11=22033
1297·17=22049
1697·13=22061
761·29=22069
1699·13=22087
1163·19=22097
1301·17=22117
2011·11=22121
1303·17=22151
599·37=22163
541·41=22181
2017·11=22187
1709·13=22217
1307·17=22219
601·37=22237
967·23=22241
1171·19=22249
719·31=22289
2027·11=22297
769·29=22301
2029·11=22319
971·23=22333
1721·13=22373
1723·13=22399
521·43=22403
773·29=22417
1319·17=22423
547·41=22427
2039·11=22429
1181·19=22439
1321·17=22457
607·37=22459
977·23=22471
523·43=22489
479·47=22513
1733·13=22529
727·31=22537
1187·19=22553
1327·17=22559
2053·11=22583
983·23=22609
1741·13=22633
1193·19=22667
613·37=22681
2063·11=22693
1747·13=22711
733·31=22723
2069·11=22759
1753·13=22789
991·23=22793
1201·19=22819
787·29=22823
617·37=22829
557·41=22837
1759·13=22867
487·47=22889
2081·11=22891
619·37=22903
739·31=22909
2083·11=22913
997·23=22931
2087·11=22957
2089·11=22979
743·31=23033
1213·19=23047
491·47=23077
563·41=23083
2099·11=23089
1777·13=23101
797·29=23113
1217·19=23123
1361·17=23137
1783·13=23179
1009·23=23207
2111·11=23221
1787·13=23231
1223·19=23237
1367·17=23239
2113·11=23243
1789·13=23257
541·43=23263
751·31=23281
1013·23=23299
569·41=23329
1373·17=23341
631·37=23347
1229·19=23351
1231·19=23389
571·41=23411
1801·13=23413
2129·11=23419
1019·23=23437
2131·11=23441
499·47=23453
809·29=23461
757·31=23467
1381·17=23477
1021·23=23483
1237·19=23503
2137·11=23507
811·29=23519
547·43=23521
1811·13=23543
2141·11=23551
2143·11=23573
761·31=23591
503·47=23641
577·41=23657
2153·11=23683
1823·13=23699
1031·23=23713
641·37=23717
1249·19=23731
1033·23=23759
2161·11=23771
1399·17=23783
643·37=23791
1831·13=23803
821·29=23809
769·31=23839
823·29=23867
1039·23=23897
1259·19=23921
509·47=23923
647·37=23939
557·43=23951
1409·17=23953
773·31=23963
2179·11=23969
827·29=23983
1847·13=24011
829·29=24041
587·41=24067
1049·23=24127
653·37=24161
1051·23=24173
1423·17=24191
1861·13=24193
563·43=24209
2203·11=24233
1427·17=24259
1277·19=24263
1867·13=24271
2207·11=24277
1429·17=24293
1279·19=24301
593·41=24313
1871·13=24323
839·29=24331
2213·11=24343
1873·13=24349
1433·17=24361
1283·19=24377
659·37=24383
787·31=24397
1877·13=24401
1061·23=24403
1879·13=24427
2221·11=24431
1063·23=24449
661·37=24457
1439·17=24463
569·43=24467
521·47=24487
1289·19=24491
1291·19=24529
571·43=24553
1889·13=24557
599·41=24559
523·47=24581
1069·23=24587
1447·17=24599
2237·11=24607
2239·11=24629
601·41=24641
1297·19=24643
1451·17=24667
2243·11=24673
1453·17=24701
797·31=24707
1901·13=24713
1301·19=24719
853·29=24737
1303·19=24757
2251·11=24761
1907·13=24791
1459·17=24803
577·43=24811
1307·19=24833
857·29=24853
1913·13=24869
607·41=24887
673·37=24901
859·29=24911
2267·11=24937
2269·11=24959
1087·23=25001
2273·11=25003
1471·17=25007
863·29=25027
677·37=25049
1319·19=25061
809·31=25079
2281·11=25091
1091·23=25093
1321·19=25099
1931·13=25103
1933·13=25129
613·41=25133
1093·23=25139
811·31=25141
2287·11=25157
1481·17=25177
1483·17=25211
1327·19=25213
2293·11=25223
1097·23=25231
587·43=25241
2297·11=25267
683·37=25271
1487·17=25279
617·41=25297
1489·17=25313
1949·13=25337
1951·13=25363
1103·23=25369
619·41=25379
1493·17=25381
2309·11=25399
2311·11=25421
541·47=25427
877·29=25433
821·31=25451
1499·17=25483
593·43=25499
1109·23=25507
823·31=25513
881·29=25549
691·37=25567
883·29=25607
827·31=25637
1973·13=25649
2333·11=25663
1511·17=25687
1117·23=25691
829·31=25699
547·47=25709
887·29=25723
1979·13=25727
2339·11=25729
2341·11=25751
599·43=25757
2347·11=25817
1123·23=25829
1987·13=25831
601·43=25843
1361·19=25859
2351·11=25861
631·41=25871
1523·17=25891
1993·13=25909
2357·11=25927
701·37=25937
1997·13=25961
1129·23=25967
1367·19=25973
1999·13=25987
839·31=26009
1531·17=26027
2003·13=26039
2371·11=26081
1373·19=26087
607·43=26101
2011·13=26143
2377·11=26147
557·47=26179
2381·11=26191
2383·11=26213
2017·13=26221
1543·17=26231
709·37=26233
1381·19=26239
2389·11=26279
641·41=26281
907·29=26303
2393·11=26323
1549·17=26333
2027·13=26351
613·43=26359
643·41=26363
2029·13=26377
2399·11=26389
1553·17=26401
911·29=26419
853·31=26443
563·47=26461
1151·23=26473
1559·17=26503
2039·13=26507
1153·23=26519
2411·11=26521
647·41=26527
617·43=26531
857·31=26567
1399·19=26581
2417·11=26587
719·37=26603
619·43=26617
859·31=26629
1567·17=26639
919·29=26651
2423·11=26653
2053·13=26689
1571·17=26707
569·47=26743
1163·23=26749
863·31=26753
1409·19=26771
653·41=26773
2437·11=26807
2063·13=26819
571·47=26837
1579·17=26843
2441·11=26851
2069·13=26897
727·37=26899
1583·17=26911
2447·11=26917
1171·23=26933
929·29=26941
659·41=27019
1423·19=27037
2459·11=27049
2081·13=27053
2083·13=27079
661·41=27101
1427·19=27113
577·47=27119
733·37=27121
2087·13=27131
631·43=27133
2467·11=27137
1597·17=27149
1429·19=27151
2089·13=27157
1181·23=27163
937·29=27173
877·31=27187
2473·11=27203
1601·17=27217
1433·19=27227
2477·11=27247
2099·13=27287
941·29=27289
1187·23=27301
881·31=27311
1607·17=27319
1439·19=27341
739·37=27343
1609·17=27353
883·31=27373
1613·17=27421
1193·23=27439
2111·13=27443
947·29=27463
2113·13=27469
743·37=27491
1447·19=27493
887·31=27497
1619·17=27523
2503·11=27533
1621·17=27557
641·43=27563
1451·19=27569
587·47=27589
673·41=27593
1453·19=27607
1201·23=27623
953·29=27637
643·43=27649
1627·17=27659
2129·13=27677
2131·13=27703
1459·19=27721
2521·11=27731
677·41=27757
2137·13=27781
751·37=27787
647·43=27821
1637·17=27829
2141·13=27833
2531·11=27841
2143·13=27859
593·47=27871
1213·23=27899
2539·11=27929
1471·19=27949
2543·11=27973
2153·13=27989
1217·23=27991
683·41=28003
757·37=28009
2549·11=28039
967·29=28043
2551·11=28061
653·43=28079
2161·13=28093
907·31=28117
2557·11=28127
1223·23=28129
1481·19=28139
599·47=28153
761·37=28157
971·29=28159
1657·17=28169
1483·19=28177
911·31=28241
601·47=28247
1487·19=28253
1229·23=28267
1663·17=28271
1489·19=28291
1231·23=28313
2179·13=28327
691·41=28331
977·29=28333
659·43=28337
1667·17=28339
1493·19=28367
2579·11=28369
1669·17=28373
661·43=28423
1237·23=28451
769·37=28453
1499·19=28481
919·31=28489
2591·11=28501
983·29=28507
2593·11=28523
607·47=28529
773·37=28601
2203·13=28639
2207·13=28691
2609·11=28699
1511·19=28709
1249·23=28727
991·29=28739
701·41=28741
2213·13=28769
1693·17=28781
2617·11=28787
929·31=28799
613·47=28811
2621·11=28831
1697·17=28849
2221·13=28873
1699·17=28883
997·29=28913
1523·19=28937
673·43=28939
1259·23=28957
2633·11=28963
617·47=28999
937·31=29047
1709·17=29053
709·41=29069
2237·13=29081
1531·19=29089
619·47=29093
2239·13=29107
677·43=29111
2647·11=29117
787·37=29119
2243·13=29159
941·31=29171
2657·11=29227
2659·11=29249
1721·17=29257
1009·29=29261
2251·13=29263
1723·17=29291
2663·11=29293
1543·19=29317
947·31=29357
683·43=29369
1277·23=29371
1013·29=29377
2671·11=29381
1279·23=29417
1549·19=29431
2677·11=29447
1733·17=29461
2267·13=29471
719·41=29479
797·37=29489
2269·13=29497
1553·19=29507
1283·23=29509
2683·11=29513
953·31=29543
2273·13=29549
1019·29=29551
2687·11=29557
2689·11=29579
1741·17=29597
1021·29=29609
1559·19=29621
2693·11=29623
1289·23=29647
2281·13=29653
631·47=29657
2699·11=29689
1291·23=29693
1747·17=29699
691·43=29713
2287·13=29731
1567·19=29773
2707·11=29777
1753·17=29801
727·41=29807
2293·13=29809
2711·11=29821
1297·23=29831
2713·11=29843
1571·19=29849
2297·13=29861
1031·29=29899
1759·17=29903
2719·11=29909
1301·23=29923
809·37=29933
1033·29=29957
1303·23=29969
967·31=29977
1579·19=30001
811·37=30007
2309·13=30017
2311·13=30043
733·41=30053
1307·23=30061
1583·19=30077
971·31=30101
641·47=30127
1039·29=30131
701·43=30143
1777·17=30209
643·47=30221
977·31=30287
739·41=30299
1783·17=30311
2333·13=30329
1319·23=30337
1597·19=30343
821·37=30377
1787·17=30379
1321·23=30383
2339·13=30407
647·47=30409
1789·17=30413
1601·19=30419
1049·29=30421
2341·13=30433
823·37=30451
743·41=30463
983·31=30473
1051·29=30479
709·43=30487
2347·13=30511
1327·23=30521
1607·19=30533
2351·13=30563
1609·19=30571
827·37=30599
1801·17=30617
2357·13=30641
1613·19=30647
829·37=30673
653·47=30691
991·31=30721
1619·19=30761
1061·29=30769
1811·17=30787
751·41=30791
1621·19=30799
2371·13=30823
1063·29=30827
2377·13=30901
997·31=30907
1627·19=30913
719·43=30917
2381·13=30953
659·47=30973
2383·13=30979
1823·17=30991
1069·29=31001
757·41=31037
839·37=31043
2389·13=31057
661·47=31067
1637·19=31103
2393·13=31109
1831·17=31127
2399·13=31187
761·41=31201
727·43=31261
1009·31=31279
1361·23=31303
2411·13=31343
1847·17=31399
1013·31=31403
2417·13=31421
1367·23=31441
1657·19=31483
2423·13=31499
733·43=31519
1087·29=31523
769·41=31529
853·37=31561
1373·23=31579
1019·31=31589
1663·19=31597
673·47=31631
1861·17=31637
1091·29=31639
1021·31=31651
1667·19=31673
2437·13=31681
773·41=31693
1093·29=31697
857·37=31709
1669·19=31711
2441·13=31733
1867·17=31739
1381·23=31763
739·43=31777
859·37=31783
1871·17=31807
2447·13=31811
1097·29=31813
677·47=31819
1873·17=31841
1877·17=31909
863·37=31931
1879·17=31943
743·43=31949
1031·31=31961
2459·13=31967
1103·29=31987
1033·31=32023
2467·13=32071
683·47=32101
1889·17=32113
2473·13=32149
1109·29=32161
1693·19=32167
1399·23=32177
2477·13=32201
1039·31=32209
1697·19=32243
787·41=32267
1699·19=32281
751·43=32293
1901·17=32317
1117·29=32393
1409·23=32407
1907·17=32419
877·37=32449
1709·19=32471
691·47=32477
1049·31=32519
1913·17=32521
2503·13=32539
757·43=32551
1123·29=32567
1051·31=32581
881·37=32597
883·37=32671
797·41=32677
1721·19=32699
761·43=32723
1423·23=32729
1723·19=32737
1129·29=32741
2521·13=32773
887·37=32819
1427·23=32821
1931·17=32827
1933·17=32861
1429·23=32867
1061·31=32891
2531·13=32903
1733·19=32927
701·47=32947
1063·31=32953
1433·23=32959
2539·13=33007
2543·13=33059
769·43=33067
1741·19=33079
1439·23=33097
1949·17=33133
2549·13=33137
1069·31=33139
2551·13=33163
1951·17=33167
809·41=33169
1747·19=33193
773·43=33239
2557·13=33241
811·41=33251
1447·23=33281
1753·19=33307
709·47=33323
1451·23=33373
1151·29=33379
1453·23=33419
1759·19=33421
1153·29=33437
2579·13=33527
1973·17=33541
1459·23=33557
907·37=33559
1979·17=33643
821·41=33661
2591·13=33683
1087·31=33697
911·37=33707
2593·13=33709
1163·29=33727
823·41=33743
1777·19=33763
1987·17=33779
719·47=33793
1091·31=33821
1471·23=33833
787·43=33841
1783·19=33877
1993·17=33881
1093·31=33883
827·41=33907
2609·13=33917
1997·17=33949
1787·19=33953
1171·29=33959
1999·17=33983
829·41=33989
1789·19=33991
919·37=34003
1097·31=34007
2617·13=34021
2003·17=34051
1481·23=34063
2621·13=34073
1483·23=34109
727·47=34169
2011·17=34187
1103·31=34193
1487·23=34201
1801·19=34219
2633·13=34229
1489·23=34247
1181·29=34249
797·43=34271
2017·17=34289
1493·23=34339
929·37=34373
1109·31=34379
839·41=34399
1811·19=34409
2647·13=34411
1187·29=34423
733·47=34451
2027·17=34459
1499·23=34477
2029·17=34493
2657·13=34541
2659·13=34567
1193·29=34597
2663·13=34619
1117·31=34627
1823·19=34637
2039·17=34663
937·37=34669
2671·13=34723
739·47=34733
1511·23=34753
809·43=34787
1831·19=34789
2677·13=34801
1123·31=34813
941·37=34817
1201·29=34829
811·43=34873
2683·13=34879
2053·17=34901
743·47=34921
2687·13=34931
2689·13=34957
853·41=34973
1129·31=34999
2693·13=35009
1523·23=35029
947·37=35039
2063·17=35071
2699·13=35087
1847·19=35093
857·41=35137
2069·17=35173
1213·29=35177
2707·13=35191
1531·23=35213
859·41=35219
2711·13=35243
953·37=35261
2713·13=35269
1217·29=35293
751·47=35297
821·43=35303
2719·13=35347
1861·19=35359
2081·17=35377
863·41=35383
823·43=35389
2083·17=35411
1223·29=35467
1867·19=35473
2087·17=35479
1543·23=35489
2089·17=35513
1871·19=35549
827·43=35561
757·47=35579
1873·19=35587
1549·23=35627
1229·29=35641
829·43=35647
1877·19=35663
1151·31=35681
2099·17=35683
1231·29=35699
1879·19=35701
1553·23=35719
1153·31=35743
761·47=35767
967·37=35779
1559·23=35857
1237·29=35873
2111·17=35887
1889·19=35891
2113·17=35921
971·37=35927
877·41=35957
1567·23=36041
1163·31=36053
839·43=36077
1901·19=36119
881·41=36121
1571·23=36133
769·47=36143
977·37=36149
2129·17=36193
883·41=36203
1249·29=36221
2131·17=36227
1907·19=36233
1171·31=36301
1579·23=36317
2137·17=36329
773·47=36331
1913·19=36347
887·41=36367
983·37=36371
2141·17=36397
1583·23=36409
2143·17=36431
1259·29=36511
2153·17=36601
1181·31=36611
991·37=36667
853·43=36679
1931·19=36689
1933·19=36727
1597·23=36731
2161·17=36737
1187·31=36797
1601·23=36823
857·43=36851
997·37=36889
859·43=36937
1607·23=36961
1193·31=36983
787·47=36989
1609·23=37007
1949·19=37031
1277·29=37033
2179·17=37043
1951·19=37069
1279·29=37091
1613·23=37099
863·43=37109
907·41=37187
1283·29=37207
1201·31=37231
1619·23=37237
1621·23=37283
1009·37=37333
911·41=37351
1289·29=37381
1627·23=37421
1291·29=37439
2203·17=37451
797·47=37459
1013·37=37481
1973·19=37487
2207·17=37519
1979·19=37601
1213·31=37603
1297·29=37613
2213·17=37621
1637·23=37651
919·41=37679
1019·37=37703
877·43=37711
1217·31=37727
1301·29=37729
1987·19=37753
2221·17=37757
1021·37=37777
1303·29=37787
1993·19=37867
881·43=37883
1307·29=37903
1223·31=37913
1997·19=37943
883·43=37969
1999·19=37981
809·47=38023
2237·17=38029
2003·19=38057
2239·17=38063
929·41=38089
1229·31=38099
1657·23=38111
811·47=38117
2243·17=38131
887·43=38141
1031·37=38147
1231·31=38161
2011·19=38209
1033·37=38221
1663·23=38249
1319·29=38251
2251·17=38267
1321·29=38309
2017·19=38323
1667·23=38341
1237·31=38347
1669·23=38387
937·41=38417
1039·37=38443
1327·29=38483
2027·19=38513
2267·17=38539
2029·19=38551
2269·17=38573
941·41=38581
821·47=38587
2273·17=38641
823·47=38681
1249·31=38719
2039·19=38741
2281·17=38777
1049·37=38813
947·41=38827
827·47=38869
2287·17=38879
1051·37=38887
1693·23=38939
829·47=38963
2293·17=38981
907·43=39001
2053·19=39007
1259·31=39029
1697·23=39031
2297·17=39049
953·41=39073
1699·23=39077
911·43=39173
2063·19=39197
2309·17=39253
1061·37=39257
2311·17=39287
1709·23=39307
2069·19=39311
1063·37=39331
839·47=39433
1361·29=39469
919·43=39517
2081·19=39539
1069·37=39553
2083·19=39577
1721·23=39583
1277·31=39587
1723·23=39629
1367·29=39643
967·41=39647
1279·31=39649
2087·19=39653
2333·17=39661
2089·19=39691
2339·17=39763
1283·31=39773
2341·17=39797
971·41=39811
1373·29=39817
1733·23=39859
2099·19=39881
2347·17=39899
929·43=39947
1289·31=39959
2351·17=39967
1291·31=40021
1741·23=40043
1381·29=40049
977·41=40057
2357·17=40069
853·47=40091
2111·19=40109
2113·19=40147
1747·23=40181
1297·31=40207
1087·37=40219
857·47=40279
937·43=40291
983·41=40303
2371·17=40307
1753·23=40319
1301·31=40331
1091·37=40367
859·47=40373
1303·31=40393
2377·17=40409
1093·37=40441
2129·19=40451
1759·23=40457
941·43=40463
2381·17=40477
2131·19=40489
2383·17=40511
1307·31=40517
863·47=40561
1399·29=40571
1097·37=40589
2137·19=40603
2389·17=40613
991·41=40631
2141·19=40679
2393·17=40681
2143·19=40717
947·43=40721
2399·17=40783
1103·37=40811
1409·29=40861
1777·23=40871
997·41=40877
1319·31=40889
2153·19=40907
1321·31=40951
953·43=40979
2411·17=40987
1783·23=41009
1109·37=41033
2161·19=41059
2417·17=41089
1787·23=41101
1327·31=41137
1789·23=41147
2423·17=41191
877·47=41219
1423·29=41267
1117·37=41329
1009·41=41369
1427·29=41383
2179·19=41401
881·47=41407
1801·23=41423
2437·17=41429
1429·29=41441
2441·17=41497
883·47=41501
1013·41=41533
1123·37=41551
1433·29=41557
967·43=41581
2447·17=41599
1811·23=41653
887·47=41689
1439·29=41731
971·43=41753
1129·37=41773
1019·41=41779
2459·17=41803
2203·19=41857
1021·41=41861
1823·23=41929
2207·19=41933
2467·17=41939
1447·29=41963
977·43=42011
2473·17=42041
2213·19=42047
1451·29=42079
2477·17=42109
1831·23=42113
1453·29=42137
1361·31=42191
2221·19=42199
983·43=42269
1031·41=42271
1459·29=42311
1033·41=42353
1367·31=42377
1847·23=42481
2237·19=42503
2239·19=42541
2503·17=42551
1373·31=42563
1151·37=42587
1039·41=42599
991·43=42613
2243·19=42617
907·47=42629
1471·29=42659
1153·37=42661
2251·19=42769
1861·23=42803
1381·31=42811
911·47=42817
2521·17=42857
997·43=42871
1867·23=42941
1481·29=42949
1483·29=43007
1049·41=43009
2531·17=43027
1163·37=43031
1871·23=43033
2267·19=43073
1873·23=43079
1051·41=43091
2269·19=43111
1487·29=43123
2539·17=43163
1877·23=43171
1489·29=43181
2273·19=43187
919·47=43193
1879·23=43217
2543·17=43231
1493·29=43297
1171·37=43327
2549·17=43333
2281·19=43339
2551·17=43367
1399·31=43369
1009·43=43387
1889·23=43447
2287·19=43453
2557·17=43469
1499·29=43471
1061·41=43501
1013·43=43559
2293·19=43567
1063·41=43583
2297·19=43643
929·47=43663
1409·31=43679
1181·37=43697
1901·23=43723
1019·43=43817
1511·29=43819
1069·41=43829
2579·17=43843
1907·23=43861
2309·19=43871
1021·43=43903
2311·19=43909
1187·37=43919
1913·23=43999
937·47=44039
2591·17=44047
2593·17=44081
1423·31=44113
1193·37=44141
1523·29=44167
941·47=44227
1427·31=44237
1429·31=44299
2333·19=44327
1031·43=44333
2609·17=44353
1531·29=44399
1931·23=44413
1033·43=44419
1433·31=44423
1201·37=44437
2339·19=44441
1933·23=44459
2341·19=44479
2617·17=44489
947·47=44509
2621·17=44557
1087·41=44567
2347·19=44593
1439·31=44609
2351·19=44669
1039·43=44677
1091·41=44731
1543·29=44747
2633·17=44761
2357·19=44783
953·47=44791
1093·41=44813
1949·23=44827
1447·31=44857
1951·23=44873
1213·37=44881
1549·29=44921
1097·41=44977
1451·31=44981
2647·17=44999
1217·37=45029
1553·29=45037
1453·31=45043
2371·19=45049
1049·43=45107
2377·19=45163
2657·17=45169
1051·43=45193
2659·17=45203
1559·29=45211
1103·41=45223
1459·31=45229
2381·19=45239
1223·37=45251
2663·17=45271
2383·19=45277
1973·23=45379
2389·19=45391
2671·17=45407
1567·29=45443
967·47=45449
2393·19=45467
1109·41=45469
1229·37=45473
2677·17=45509
1979·23=45517
1231·37=45547
1571·29=45559
2399·19=45581
1471·31=45601
2683·17=45611
1061·43=45623
971·47=45637
2687·17=45679
1987·23=45701
1063·43=45709
2689·17=45713
1237·37=45769
2693·17=45781
1579·29=45791
1117·41=45797
2411·19=45809
1993·23=45839
2699·17=45883
1583·29=45907
1481·31=45911
977·47=45919
2417·19=45923
1997·23=45931
1069·43=45967
1483·31=45973
1999·23=45977
2707·17=46019
2423·19=46037
1123·41=46043
2003·23=46069
2711·17=46087
1487·31=46097
2713·17=46121
1489·31=46159
983·47=46201
1249·37=46213
2719·17=46223
2011·23=46253
1493·31=46283
1129·41=46289
2437·19=46303
1597·29=46313
2441·19=46379
2017·23=46391
1601·29=46429
1499·31=46469
2447·19=46493
991·47=46577
1259·37=46583
1607·29=46603
2027·23=46621
1609·29=46661
2029·23=46667
2459·19=46721
1087·43=46741
1613·29=46777
1511·31=46841
997·47=46859
2467·19=46873
2039·23=46897
1091·43=46913
1619·29=46951
2473·19=46987
1093·43=46999
1621·29=47009
2477·19=47063
1097·43=47171
1627·29=47183
1151·41=47191
1523·31=47213
2053·23=47219
1277·37=47249
1153·41=47273
1279·37=47323
1009·47=47423
1103·43=47429
2063·23=47449
1531·31=47461
1283·37=47471
1637·29=47473
2503·19=47557
2069·23=47587
1013·47=47611
1163·41=47683
1109·43=47687
1289·37=47693
1291·37=47767
1543·31=47833
2081·23=47863
1019·47=47893
2521·19=47899
2083·23=47909
1021·47=47987
1297·37=47989
2087·23=48001
1171·41=48011
1549·31=48019
1117·43=48031
2089·23=48047
1657·29=48053
2531·19=48089
1301·37=48137
1553·31=48143
1303·37=48211
1663·29=48227
2539·19=48241
2099·23=48277
1123·43=48289
2543·19=48317
1559·31=48329
1667·29=48343
1307·37=48359
1669·29=48401
1181·41=48421
2549·19=48431
1031·47=48457
2551·19=48469
1129·43=48547
1033·47=48551
2111·23=48553
1567·31=48577
2557·19=48583
2113·23=48599
1187·41=48667
1571·31=48701
1319·37=48803
1039·47=48833
1321·37=48877
1193·41=48913
1579·31=48949
2129·23=48967
2579·19=49001
2131·23=49013
1583·31=49073
1693·29=49097
1327·37=49099
2137·23=49151
1697·29=49213
2591·19=49229
1201·41=49241
2141·23=49243
2593·19=49267
1699·29=49271
2143·23=49289
1049·47=49303
1051·47=49397
1151·43=49493
1597·31=49507
2153·23=49519
1709·29=49561
2609·19=49571
1153·43=49579
1601·31=49631
2161·23=49703
2617·19=49723
1213·41=49733
2621·19=49799
1607·31=49817
1061·47=49867
1609·31=49879
1217·41=49897
1721·29=49909
1063·47=49961
1723·29=49967
1613·31=50003
1163·43=50009
2633·19=50027
2179·23=50117
1223·41=50143
1619·31=50189
1069·47=50243
1621·31=50251
1733·29=50257
2647·19=50293
1171·43=50353
1361·37=50357
1229·41=50389
1627·31=50437
1231·41=50471
2657·19=50483
1741·29=50489
2659·19=50521
1367·37=50579
2663·19=50597
1747·29=50663
2203·23=50669
1237·41=50717
1637·31=50747
2671·19=50749
2207·23=50761
1181·43=50783
1373·37=50801
1753·29=50837
2677·19=50863
2213·23=50899
2683·19=50977
1759·29=51011
1187·43=51041
2687·19=51053
2221·23=51083
1087·47=51089
2689·19=51091
1381·37=51097
2693·19=51167
1249·41=51209
1091·47=51277
2699·19=51281
1193·43=51299
1657·31=51367
1093·47=51371
2707·19=51433
2237·23=51451
2239·23=51497
2711·19=51509
1777·29=51533
2713·19=51547
1663·31=51553
1097·47=51559
2243·23=51589
1259·41=51619
1201·43=51643
2719·19=51661
1667·31=51677
1783·29=51707
1669·31=51739
1399·37=51763
2251·23=51773
1787·29=51823
1103·47=51841
1789·29=51881
1109·47=52123
1409·37=52133
2267·23=52141
1213·43=52159
2269·23=52187
1801·29=52229
2273·23=52279
1217·43=52331
1277·41=52357
1279·41=52439
2281·23=52463
1693·31=52483
1117·47=52499
1811·29=52519
1223·43=52589
2287·23=52601
1283·41=52603
1697·31=52607
1423·37=52651
1699·31=52669
2293·23=52739
1123·47=52781
1427·37=52799
2297·23=52831
1229·43=52847
1289·41=52849
1823·29=52867
1429·37=52873
1291·41=52931
1231·43=52933
1709·31=52979
1433·37=53021
1129·47=53063
1831·29=53099
2309·23=53107
2311·23=53153
1297·41=53177
1237·43=53191
1439·37=53243
1301·41=53341
1721·31=53351
1723·31=53413
1303·41=53423
1447·37=53539
1847·29=53563
1307·41=53587
2333·23=53659
1451·37=53687
1249·43=53707
1733·31=53723
1453·37=53761
2339·23=53797
2341·23=53843
1861·29=53969
1741·31=53971
2347·23=53981
1459·37=53983
2351·23=54073
1319·41=54079
1151·47=54097
1259·43=54137
1867·29=54143
1747·31=54157
1321·41=54161
1153·47=54191
2357·23=54211
1871·29=54259
1873·29=54317
1753·31=54343
1327·41=54407
1471·37=54427
1877·29=54433
1879·29=54491
1759·31=54529
2371·23=54533
1163·47=54661
2377·23=54671
2381·23=54763
1889·29=54781
1481·37=54797
2383·23=54809
1483·37=54871
1277·43=54911
2389·23=54947
1279·43=54997
1487·37=55019
1171·47=55037
2393·23=55039
1777·31=55087
1489·37=55093
1901·29=55129
1283·43=55169
2399·23=55177
1493·37=55241
1783·31=55273
1907·29=55303
1787·31=55397
1289·43=55427
2411·23=55453
1789·31=55459
1499·37=55463
1913·29=55477
1181·47=55507
1291·43=55513
2417·23=55591
2423·23=55729
1297·43=55771
1187·47=55789
1361·41=55801
1801·31=55831
1511·37=55907
1301·43=55943
1931·29=55999
1303·43=56029
1367·41=56047
2437·23=56051
1933·29=56057
1193·47=56071
1811·31=56141
2441·23=56143
1307·43=56201
2447·23=56281
1373·41=56293
1523·37=56351
1201·47=56447
1823·31=56513
1949·29=56521
2459·23=56557
1951·29=56579
1381·41=56621
1531·37=56647
1319·43=56717
2467·23=56741
1831·31=56761
1321·43=56803
2473·23=56879
2477·23=56971
1213·47=57011
1327·43=57061
1543·37=57091
1217·47=57199
1973·29=57217
1847·31=57257
1549·37=57313
1399·41=57359
1979·29=57391
1553·37=57461
1223·47=57481
2503·23=57569
1987·29=57623
1559·37=57683
1861·31=57691
1229·47=57763
1409·41=57769
1993·29=57797
1231·47=57857
1867·31=57877
1997·29=57913
1999·29=57971
1567·37=57979
2521·23=57983
1871·31=58001
1873·31=58063
2003·29=58087
1571·37=58127
1237·47=58139
1877·31=58187
2531·23=58213
1879·31=58249
2011·29=58319
1423·41=58343
2539·23=58397
1579·37=58423
2543·23=58489
2017·29=58493
1427·41=58507
1361·43=58523
1889·31=58559
1583·37=58571
1429·41=58589
2549·23=58627
2551·23=58673
1249·47=58703
1433·41=58753
1367·43=58781
2027·29=58783
2557·23=58811
2029·29=58841
1901·31=58931
1439·41=58999
1373·43=59039
1597·37=59089
1907·31=59117
2039·29=59131
1259·47=59173
1601·37=59237
1913·31=59303
2579·23=59317
1447·41=59327
1381·43=59383
1607·37=59459
1451·41=59491
1609·37=59533
2053·29=59537
1453·41=59573
2591·23=59593
2593·23=59639
1613·37=59681
1459·41=59819
2063·29=59827
1931·31=59861
1619·37=59903
1933·31=59923
1621·37=59977
2069·29=60001
2609·23=60007
1277·47=60019
1279·47=60113
1399·43=60157
2617·23=60191
1627·37=60199
2621·23=60283
1283·47=60301
1471·41=60311
2081·29=60349
2083·29=60407
1949·31=60419
1951·31=60481
2087·29=60523
2633·23=60559
1637·37=60569
2089·29=60581
1289·47=60583
1409·43=60587
1291·47=60677
1481·41=60721
1483·41=60803
2099·29=60871
2647·23=60881
1297·47=60959
1487·41=60967
1489·41=61049
2657·23=61111
1301·47=61147
2659·23=61157
1973·31=61163
1423·43=61189
1493·41=61213
2111·29=61219
1303·47=61241
2663·23=61249
2113·29=61277
1657·37=61309
1979·31=61349
1427·43=61361
1307·47=61429
2671·23=61433
1429·43=61447
1499·41=61459
1663·37=61531
2677·23=61571
1987·31=61597
1433·43=61619
1667·37=61679
2683·23=61709
2129·29=61741
1669·37=61753
1993·31=61783
2131·29=61799
2687·23=61801
2689·23=61847
1439·43=61877
1997·31=61907
2693·23=61939
1511·41=61951
1999·31=61969
2137·29=61973
1319·47=61993
2699·23=62077
1321·47=62087
2141·29=62089
2003·31=62093
2143·29=62147
1447·43=62221
2707·23=62261
2011·31=62341
2711·23=62353
1327·47=62369
1451·43=62393
2713·23=62399
2153·29=62437
1523·41=62443
1453·43=62479
2017·31=62527
2719·23=62537
1693·37=62641
2161·29=62669
1459·43=62737
1531·41=62771
1697·37=62789
2027·31=62837
1699·37=62863
2029·31=62899
2179·29=63191
2039·31=63209
1709·37=63233
1471·43=63253
1543·41=63263
1549·41=63509
2053·31=63643
1553·41=63673
1721·37=63677
1481·43=63683
1723·37=63751
1483·43=63769
2203·29=63887
1559·41=63919
1487·43=63941
2063·31=63953
1361·47=63967
2207·29=64003
1489·43=64027
1733·37=64121
2069·31=64139
2213·29=64177
1493·43=64199
1567·41=64247
1367·47=64249
2221·29=64409
1571·41=64411
1741·37=64417
1499·43=64457
2081·31=64511
1373·47=64531
2083·31=64573
1747·37=64639
2087·31=64697
1579·41=64739
2089·31=64759
1753·37=64861
2237·29=64873
1583·41=64903
1381·47=64907
2239·29=64931
1511·43=64973
2243·29=65047
2099·31=65069
1759·37=65083
2251·29=65279
2111·31=65441
1597·41=65477
1523·43=65489
2113·31=65503
1601·41=65641
2267·29=65743
1777·37=65749
1399·47=65753
2269·29=65801
1531·43=65833
1607·41=65887
2273·29=65917
1609·41=65969
1783·37=65971
2129·31=65999
2131·31=66061
1787·37=66119
1613·41=66133
2281·29=66149
1789·37=66193
1409·47=66223
2137·31=66247
2287·29=66323
1543·43=66349
2141·31=66371
1619·41=66379
2143·31=66433
1621·41=66461
2293·29=66497
1549·43=66607
2297·29=66613
1801·37=66637
1627·41=66707
2153·31=66743
1553·43=66779
1423·47=66881
2309·29=66961
2161·31=66991
1811·37=67007
2311·29=67019
1559·43=67037
1427·47=67069
1637·41=67117
1429·47=67163
1433·47=67351
1567·43=67381
1823·37=67451
2179·31=67549
1571·43=67553
1439·47=67633
2333·29=67657
1831·37=67747
2339·29=67831
2341·29=67889
1579·43=67897
1657·41=67937
1447·47=68009
2347·29=68063
1583·43=68069
2351·29=68179
1663·41=68183
1451·47=68197
1453·47=68291
2203·31=68293
1847·37=68339
1667·41=68347
2357·29=68353
2207·31=68417
1669·41=68429
1459·47=68573
2213·31=68603
1597·43=68671
2371·29=68759
1601·43=68843
2221·31=68851
1861·37=68857
2377·29=68933
2381·29=69049
1867·37=69079
1607·43=69101
2383·29=69107
1471·47=69137
1609·43=69187
1871·37=69227
2389·29=69281
1873·37=69301
2237·31=69347
1613·43=69359
2393·29=69397
2239·31=69409
1693·41=69413
1877·37=69449
1879·37=69523
2243·31=69533
2399·29=69571
1697·41=69577
1481·47=69607
1619·43=69617
1699·41=69659
1483·47=69701
1621·43=69703
2251·31=69781
1487·47=69889
1889·37=69893
2411·29=69919
1627·43=69961
1489·47=69983
1709·41=70069
2417·29=70093
1493·47=70171
2423·29=70267
2267·31=70277
1901·37=70337
2269·31=70339
1637·43=70391
1499·47=70453
2273·31=70463
1907·37=70559
1721·41=70561
1723·41=70643
2437·29=70673
2281·31=70711
1913·37=70781
2441·29=70789
2287·31=70897
2447·29=70963
1511·47=71017
1733·41=71053
2293·31=71083
2297·31=71207
1657·43=71251
2459·29=71311
1741·41=71381
1931·37=71447
1663·43=71509
1933·37=71521
2467·29=71543
2309·31=71579
1523·47=71581
1747·41=71627
2311·31=71641
1667·43=71681
2473·29=71717
1669·43=71767
2477·29=71833
1753·41=71873
1531·47=71957
1949·37=72113
1759·41=72119
1951·37=72187
2333·31=72323
2339·31=72509
1543·47=72521
2341·31=72571
2503·29=72587
2347·31=72757
1693·43=72799
1549·47=72803
1777·41=72857
2351·31=72881
1697·43=72971
1553·47=72991
1973·37=73001
1699·43=73057
2357·31=73067
1783·41=73103
2521·29=73109
1979·37=73223
1787·41=73267
1559·47=73273
1789·41=73349
2531·29=73399
1709·43=73487
2371·31=73501
1987·37=73519
2539·29=73631
1567·47=73649
2377·31=73687
1993·37=73741
2543·29=73747
2381·31=73811
1571·47=73837
1801·41=73841
2383·31=73873
1997·37=73889
2549·29=73921
1999·37=73963
2551·29=73979
1721·43=74003
2389·31=74059
1723·43=74089
2003·37=74111
2557·29=74153
2393·31=74183
1579·47=74213
1811·41=74251
2399·31=74369
1583·47=74401
2011·37=74407
1733·43=74519
2017·37=74629
2411·31=74741
1823·41=74743
2579·29=74791
1741·43=74863
2417·31=74927
2027·37=74999
1597·47=75059
1831·41=75071
2029·37=75073
2423·31=75113
1747·43=75121
2591·29=75139
2593·29=75197
1601·47=75247
1753·43=75379
2039·37=75443
1607·47=75529
2437·31=75547
1609·47=75623
1759·43=75637
2609·29=75661
2441·31=75671
1847·41=75727
1613·47=75811
2447·31=75857
2617·29=75893
2053·37=75961
2621·29=76009
1619·47=76093
1621·47=76187
2459·31=76229
1861·41=76301
2063·37=76331
2633·29=76357
1777·43=76411
1627·47=76469
2467·31=76477
1867·41=76547
2069·37=76553
2473·31=76663
1783·43=76669
1871·41=76711
2647·29=76763
2477·31=76787
1873·41=76793
1787·43=76841
1789·43=76927
1637·47=76939
1877·41=76957
2081·37=76997
1879·41=77039
2657·29=77053
2083·37=77071
2659·29=77111
2087·37=77219
2663·29=77227
2089·37=77293
1801·43=77443
1889·41=77449
2671·29=77459
2503·31=77593
2677·29=77633
2099·37=77663
2683·29=77807
1811·43=77873
1657·47=77879
2687·29=77923
1901·41=77941
2689·29=77981
2693·29=78097
2111·37=78107
2521·31=78151
1663·47=78161
2113·37=78181
1907·41=78187
2699·29=78271
1667·47=78349
1823·43=78389
1913·41=78433
1669·47=78443
2531·31=78461
2707·29=78503
2711·29=78619
2713·29=78677
2539·31=78709
1831·43=78733
2129·37=78773
2543·31=78833
2131·37=78847
2719·29=78851
2549·31=79019
2137·37=79069
2551·31=79081
1931·41=79171
2141·37=79217
1933·41=79253
2557·31=79267
2143·37=79291
1847·43=79421
1693·47=79571
2153·37=79661
1697·47=79759
1699·47=79853
1949·41=79909
2579·31=79949
2161·37=79957
1951·41=79991
1861·43=80023
1867·43=80281
2591·31=80321
1709·47=80323
2593·31=80383
1871·43=80453
1873·43=80539
2179·37=80623
1877·43=80711
1879·43=80797
2609·31=80879
1721·47=80887
1973·41=80893
1723·47=80981
2617·31=81127
1979·41=81139
1889·43=81227
2621·31=81251
1733·47=81451
1987·41=81467
2203·37=81511
2633·31=81623
2207·37=81659
1993·41=81713
1901·43=81743
1741·47=81827
1997·41=81877
2213·37=81881
1999·41=81959
1907·43=82001
2647·31=82057
1747·47=82109
2003·41=82123
2221·37=82177
1913·43=82259
2657·31=82367
1753·47=82391
2659·31=82429
2011·41=82451
2663·31=82553
1759·47=82673
2017·41=82697
2237·37=82769
2671·31=82801
2239·37=82843
2677·31=82987
2243·37=82991
1931·43=83033
2027·41=83107
1933·43=83119
2683·31=83173
2029·41=83189
2251·37=83287
2687·31=83297
2689·31=83359
2693·31=83483
1777·47=83519
2039·41=83599
2699·31=83669
1783·47=83801
1949·43=83807
2267·37=83879
1951·43=83893
2707·31=83917
2269·37=83953
1787·47=83989
2711·31=84041
1789·47=84083
2273·37=84101
2713·31=84103
2053·41=84173
2719·31=84289
2281·37=84397
2063·41=84583
2287·37=84619
1801·47=84647
2069·41=84829
1973·43=84839
2293·37=84841
2297·37=84989
1979·43=85097
1811·47=85117
2081·41=85321
2083·41=85403
2309·37=85433
1987·43=85441
2311·37=85507
2087·41=85567
2089·41=85649
1823·47=85681
1993·43=85699
1997·43=85871
1999·43=85957
1831·47=86057
2099·41=86059
2003·43=86129
2333·37=86321
2011·43=86473
2339·37=86543
2111·41=86551
2341·37=86617
2113·41=86633
2017·43=86731
1847·47=86809
2347·37=86839
2351·37=86987
2027·43=87161
2357·37=87209
2029·43=87247
2129·41=87289
2131·41=87371
1861·47=87467
2137·41=87617
2039·43=87677
2371·37=87727
1867·47=87749
2141·41=87781
2143·41=87863
1871·47=87937
2377·37=87949
1873·47=88031
2381·37=88097
2383·37=88171
1877·47=88219
2153·41=88273
2053·43=88279
1879·47=88313
2389·37=88393
2393·37=88541
2161·41=88601
2063·43=88709
2399·37=88763
1889·47=88783
2069·43=88967
2411·37=89207
2179·41=89339
1901·47=89347
2417·37=89429
2081·43=89483
2083·43=89569
1907·47=89629
2423·37=89651
2087·43=89741
2089·43=89827
1913·47=89911
2437·37=90169
2099·43=90257
2441·37=90317
2203·41=90323
2207·41=90487
2447·37=90539
2213·41=90733
1931·47=90757
2111·43=90773
1933·47=90851
2113·43=90859
2459·37=90983
2221·41=91061
2467·37=91279
2473·37=91501
2129·43=91547
1949·47=91603
2131·43=91633
2477·37=91649
1951·47=91697
2237·41=91717
2239·41=91799
2137·43=91891
2243·41=91963
2141·43=92063
2143·43=92149
2251·41=92291
2153·43=92579
2503·37=92611
1973·47=92731
2161·43=92923
2267·41=92947
1979·47=93013
2269·41=93029
2273·41=93193
2521·37=93277
1987·47=93389
2281·41=93521
2531·37=93647
1993·47=93671
2179·43=93697
2287·41=93767
1997·47=93859
2539·37=93943
1999·47=93953
2293·41=94013
2543·37=94091
2003·47=94141
2297·41=94177
2549·37=94313
2551·37=94387
2011·47=94517
2557·37=94609
2309·41=94669
2203·43=94729
2311·41=94751
2017·47=94799
2207·43=94901
2213·43=95159
2027·47=95269
2029·47=95363
2579·37=95423
2221·43=95503
2333·41=95653
2039·47=95833
2591·37=95867
2339·41=95899
2593·37=95941
2341·41=95981
2237·43=96191
2347·41=96227
2239·43=96277
2351·41=96391
2243·43=96449
2053·47=96491
2609·37=96533
2357·41=96637
2251·43=96793
2617·37=96829
2063·47=96961
2621·37=96977
2371·41=97211
2069·47=97243
2633·37=97421
2377·41=97457
2267·43=97481
2269·43=97567
2381·41=97621
2383·41=97703
2273·43=97739
2081·47=97807
2083·47=97901
2647·37=97939
2389·41=97949
2281·43=98083
2087·47=98089
2393·41=98113
2089·47=98183
2657·37=98309
2287·43=98341
2399·41=98359
2659·37=98383
2663·37=98531
2293·43=98599
2099·47=98653
2297·43=98771
2671·37=98827
2411·41=98851
2677·37=99049
2417·41=99097
2111·47=99217
2683·37=99271
2309·43=99287
2113·47=99311
2423·41=99343
2311·43=99373
2687·37=99419
2689·37=99493
2693·37=99641
2699·37=99863
2437·41=99917
2129·47=100063
2441·41=100081
2131·47=100157
2707·37=100159
2711·37=100307
2333·43=100319
2447·41=100327
2713·37=100381
2137·47=100439
2339·43=100577
2719·37=100603
2141·47=100627
2341·43=100663
2143·47=100721
2459·41=100819
2347·43=100921
2351·43=101093
2467·41=101147
2153·47=101191
2357·43=101351
2473·41=101393
2477·41=101557
2161·47=101567
2371·43=101953
2377·43=102211
2381·43=102383
2179·47=102413
2383·43=102469
2503·41=102623
2389·43=102727
2393·43=102899
2399·43=103157
2521·41=103361
2203·47=103541
2411·43=103673
2207·47=103729
2531·41=103771
2417·43=103931
2213·47=104011
2539·41=104099
2423·43=104189
2543·41=104263
2221·47=104387
2549·41=104509
2551·41=104591
2437·43=104791
2557·41=104837
2441·43=104963
2237·47=105139
2447·43=105221
2239·47=105233
2243·47=105421
2459·43=105737
2579·41=105739
2251·47=105797
2467·43=106081
2591·41=106231
2593·41=106313
2473·43=106339
2477·43=106511
2267·47=106549
2269·47=106643
2273·47=106831
2609·41=106969
2281·47=107207
2617·41=107297
2621·41=107461
2287·47=107489
2503·43=107629
2293·47=107771
2633·41=107953
2297·47=107959
2521·43=108403
2309·47=108523
2647·41=108527
2311·47=108617
2531·43=108833
2657·41=108937
2659·41=109019
2539·43=109177
2663·41=109183
2543·43=109349
2671·41=109511
2549·43=109607
2333·47=109651
2551·43=109693
2677·41=109757
2339·47=109933
2557·43=109951
2683·41=110003
2341·47=110027
2687·41=110167
2689·41=110249
2347·47=110309
2693·41=110413
2351·47=110497
2699·41=110659
2357·47=110779
2579·43=110897
2707·41=110987
2711·41=111151
2713·41=111233
2591·43=111413
2371·47=111437
2719·41=111479
2593·43=111499
2377·47=111719
2381·47=111907
2383·47=112001
2609·43=112187
2389·47=112283
2393·47=112471
2617·43=112531
2621·43=112703
2399·47=112753
2633·43=113219
2411·47=113317
2417·47=113599
2647·43=113821
2423·47=113881
2657·43=114251
2659·43=114337
2663·43=114509
2437·47=114539
2441·47=114727
2671·43=114853
2447·47=115009
2677·43=115111
2683·43=115369
2687·43=115541
2459·47=115573
2689·43=115627
2693·43=115799
2467·47=115949
2699·43=116057
2473·47=116231
2707·43=116401
2477·47=116419
2711·43=116573
2713·43=116659
2719·43=116917
2503·47=117641
2521·47=118487
2531·47=118957
2539·47=119333
2543·47=119521
2549·47=119803
2551·47=119897
2557·47=120179
2579·47=121213
2591·47=121777
2593·47=121871
2609·47=122623
2617·47=122999
2621·47=123187
2633·47=123751
2647·47=124409
2657·47=124879
2659·47=124973
2663·47=125161
2671·47=125537
2677·47=125819
2683·47=126101
2687·47=126289
2689·47=126383
2693·47=126571
2699·47=126853
2707·47=127229
2711·47=127417
2713·47=127511
2719·47=127793
*/



#return SmallKey(27)				/// 
/// Entrada dos enteros positivos n > k.
/// 
// Cambia aquí los valores de n y k:

int n = 6500;
int k = 8;

/// ------------------------------------------------------------------------------------------------


BigList  Parte(big n, int k) 
{
	#Trace clear

	// Valida los argumentos
	if (k > n || k <= 0)
		throw new Exception("Error: n debe ser mayor o igual que k y k > 0");

	// La solución 
	BigList sol = new BigList(); 

	big r = n;
	int d = k;
	big s = 0;

	#: int i = 0;	// Solo para la traza
	#: big suma = 0;

	
	while (r > 0)
	{
		s = r / d;
		r -= s;
		d--;
		
		sol.Add(s);
		#: suma += s;

		#T "d = ", d, "\t\t"
		#T "s = ", s, "\t\t"
		#T "r = ", d, "\t\t"

		#T n, " = "
		#: foreach(big p in sol)
		#T 	p, " + "
		#T r
		#T "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tI=r+suma = ", r+suma, "\t\t\n"
		#: i++; 
	}

	#T "\n\n", i, " iteraciones\n"
	#T n, " = "

	#: int c = 1;
	#: foreach(big p in sol)
	#: {
		#T p
		#: if (c < sol.Count)
			#T " + "
		#: c++;
	#: }
 
	return sol;
	
}

#Return Parte(n, k);public int SumaCubos(int n)  
{
	#Trace clear

	#: // Implementamos inducción mediante un invariante

	int suma = 1;	// suma = 1+...+k. Caso inicial: para k=1, suma=1.
	int sumacubos =1;	// sumacubos=1^3+...+k^3. Caso inicial: para k=1, sumacubos=1.

	#Trace string.Format("Paso k={0:00}: sumacubos = {1:d} \tsuma = {2:d} \tsuma^2 = {3:d} \tsumacubos-suma^2 = {4:d}", 1, sumacubos, suma, suma*suma, sumacubos-suma*suma);

	// El sumatorio se corresponde con un bucle
	for (int k = 2; k <= n; k++)
	{
		suma += k;
		sumacubos += k*k*k;
		
		#: // El invariante
		#Trace string.Format("\nPaso k={0:00}: sumacubos = {1:d} \tsuma = {2:d} \tsuma^2 = {3:d} \tsumacubos-suma^2 = {4:d}", k, sumacubos, suma, suma*suma, sumacubos-suma*suma);
	}
	
	return sumacubos;
}

#Return SumaCubos(10);// 
// Resuelve el TCR especial (los módulos primos entre sí dos  a dos).
// El sistema se da como una lista de números. 
// Cada 2 números son una ecuación:
// x ≡ 2 (mod 3)  
// x ≡ 3 (mod 5)
// x ≡ 2 (mod 7)
//
// se daría como: 2, 3, 3, 5, 2, 7
// o si se quiere más claro, dos por cada línea:
// 2, 3, 
// 3, 5, 
// 2, 7
//

big[] eq = new big[] {
//
//--------------------------------------------------------------------------
// Pon aquí la lista de coeficientes 
// separados por comas: a1, b1, a2, b2,...

	1949, 71*83,
	2757, 41*101,
	2131, 53*59
 
// ----------------------------------------------------------------------------
//
};

string TCR(params big[] args)
{
	int k = args.Length / 2;

	big[] b = new big[k];
	big[] n = new big[k];
	big[] c = new big[k];
	big[] d = new big[k];
	big m = 1;
	big x = 0;



	// Los b[i]
	for (int i = 0; i < 2*k; i+=2)
		b[i/2] = args[i];

	// Los n[i]
	for (int i = 1; i < 2*k; i+=2)
	{
		n[(i-1)/2] = args[i];
	}

	// El módulo
	for (int i = 0; i < k; i++)
	{
		m *= n[i];
	}
	
	#Trace clear
	if (LATEX)
	{
		#Trace "% ---------- LIMEX: solución generada por el programa TCR.cs\n\n"

		// Escribe el sistema
		#Trace "El sistema es\n"
		#Trace "$$\n\\left.\\begin{aligned}\n"
		for (int i = 0; i < k; i++)
		{
			#Trace "\tx&\\equiv ", b[i], "\\ (\\Mod ", n[i], ")\\\\\n"
		} 
		#Trace "\\end{aligned}\\", "right\\}\n$$\n\n"

		#Trace "\n\\vspace*{2ex}\nComprobamos que los modulos son primos entre sí dos a dos:\n\n"

	}
	else
	{
		#Trace "El sistema es\n"
		for (int i = 0; i < k; i++)
			#Trace "\tx ≡ ", b[i], " \t(mod ", n[i], ")\n"

		#Trace "\n\nComprobamos que los modulos son primos entre sí dos a dos:\n"

	}
	

	// Comprueba que todos los n[i] son primos dos a dos
	bool ok = true;
	#Trace "\n"
	if (LATEX)
	for (int i = 0; i < k; i++)
	{
		for (int j = i + 1; j < k; j++)
		{
			if (LATEX)
			{	
				big n1 = n[i];
				big n2 = n[j];
				#Trace "$$\n\\begin{aligned}\n"
				while (n2 != 0)
				{
					#Trace "&\\m", "cd(", n1, ", ", n2, ") & = &"
					if (n1 % n2 == 0)
						#Trace "\\quad", n2

					#Trace "\\\\\n" 
					big t = n1;
					n1 = n2;
					n2 = t % n2;
				}
				#Trace "\\end{aligned}\\", "\n$$\n\n"
			}
			else
			{
				big n1 = n[i];
				big n2 = n[j];
				while (n2 != 0)
				{
					#Trace "m", "cd(", n1, ", ", n2, ") = "
					big t = n1;
					n1 = n2;
					n2 = t % n2;
				}
				#Trace n1, "\n"
			}

			if (mcd(n[i], n[j]) != 1)
			{
				if (LATEX)
					#Trace "\\end{aligned}\\", "right\\}\n$$\n\\vspace*{2ex}\nLos módulos no son primos entre sí dos a dos."
				else
					#Trace "\nLos módulos no son primos entre sí dos a dos."
		
				return "Error";
			}
		}
	}



	if (ok)
	{
		// Los c[i]
		for (int i = 0; i <  k; i++)
		{
			c[i] = 1;
			for (int j = 0; j < k; j++)
				if (j != i) c[i] *= n[j];
		}

		// Los d[i]
		for (int i = 0; i <  k; i++)
		{
			d[i] = modinv(c[i], n[i]);
		}

		// La solución
		x = 0;
		for (int i = 0; i < k; i++)
		{
			x += b[i] * c[i] * d[i];
		}
	}


	if (!LATEX)
	{
		#Trace "\n\nResolviendo:\n\n"

        for (int i = 0; i < k; i++)
          	#Trace string.Format("n[{0}] = {1} \t\tb[{0}] = {2} \t\tc[{0}] = {3} \t\td[{0}] = {3}^-1 (mod {1}) = {4}^-1 (mod {1}) = {5}\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);


		#Trace "\nSolución particular: "
		for (int i = 0; i < k; i++)
		{	
			#Trace b[i], "·", c[i], "·", d[i]
			if (i < k-1)
				#Trace  " + "
		}
		#Trace  " = ", x, " mod ", m, " = ", x % m, "\n"

		#Trace "\nSolución general: x = ", x % m, " + ", m, "·k"
	}

	else
	{	
		#Trace "Podemos entonces aplicar el algoritmo de resolución del Teorema Chino del Resto. Obtenemos los valores:\n\\vspace*{1ex}\n\n" 
           #Trace "\\", "renewcommand{\\", "arraystretch}{1.5}\n$$\n\\begin{array}{|l|l|l|rcrcrcr|}\n\\hline\n";
            for (int i = 0; i < k; i++)
            	#Trace string.Format("n_{{{0}}}={1} & b_{{{0}}}={2} & c_{{{0}}}={3} & d_{{{0}}} &=&{{{3}}}^{{-1}}\\ (\\Mod\\ {1})&=&{{{4}}}^{{-1}}\\ (\\Mod\\ {1})&=&{5}\\\\ \\hline\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);
            #Trace "\\end{array}\n$$\n\\", "renewcommand{\\", "arraystretch}{1}\n";

		#Trace "\n\\vspace*{1ex}\n\nUna solución es $"
		for (int i = 0; i < k; i++)
		{
			#Trace b[i] * c[i] * d[i]
			if (i < k-1)
				#Trace "+"
			else
				#Trace "=", x, "\\equiv ", x % m, "\\ (\\Mod ", m, ")$.\n"
		}

		#Trace "La solución general es $x = ", x%m, " + ", m, "\\cdot  \\lambda",  "$ con $\\lambda\\in\\Z$.\n"	

		#Trace "\n\n% ---------- LIMEX: fin de la solución ----------\n\n"

	}


	if (ok)
		return string.Format("{0} en Z{1}", x % m, m);
	else
		return "Error";
}

#R TCR(eq);	// 
// Resuelve el TCR general (los módulos no necesariamente primos entre sí dos  a dos).
// Cada ecuación es de la forma a·x ≡ b (mod n) 
//
// El sistema se da como una lista de números. 
// Cada 3 números son una ecuación a, b, n:
//  2 x ≡ 2 (mod 3)  
//  3 x ≡ 3 (mod 5)
//  4 x ≡ 2 (mod 7)
//
// se daría como: 2, 2, 3, 3, 3, 5, 4, 2, 7
// o si se quiere más claro, dos por cada línea:
// 2, 2, 3, 
// 3, 3, 5, 
// 4, 2, 7
//

big[] eqdata = new big[] {
//
//--------------------------------------------------------------------------------------
// Pon aquí la lista de coeficientes separados por comas: a1, b1, n1, a2, b2, n2, ...
2,14,20,
1,15,19,
1,3,18
// --------------------------------------------------------------------------------------
//
};

// El sistema de ecuaciones se guarda en una lista
List<equation> seq;

// Representación de la ecuación a·x ≡ b (mod p^e)
public struct equation
{
	public big a;
	public big b;
	public big p;
	public big e;
	
	public equation(big a, big b, big p, big e)
	{
		this.a = a;
		this.b = b;
		this.p = p;
		this.e = e;

		if (a == 0 || p <= 1 || e <= 0)
			throw new Exception("Ecuación mal construida");
	}
	
	public big n { get { return p^e; } }

	public static bool operator==(equation eq1, equation eq2)
	{
		return (eq1.a == eq2.a && eq1.b == eq2.b && eq1.p == eq2.p && eq1.e == eq2.e);
	}
	public static bool operator!=(equation eq1, equation eq2)
	{
		return (eq1.a != eq2.a || eq1.b != eq2.b || eq1.p != eq2.p || eq1.e != eq2.e);
	}


	public equation ToModule()
	{
		return new equation(a % n, b % n, p, e);
	}
		
	public equation Simplify()
	{
		big d = mcd(a, n);
		if (b % d == 0)
			return new equation(a/d, b/d, n/d, 1);
		else
			return new equation (0,0,0,0);
	}

	public equation Normalize()
	{
		if (a == 1) return this;
		
		big a1 =  modinv(a, n);
		return new equation (1, (a1 * b) % n, n, 1);
	}

     	public List<equation> Expand()
      {
		List<equation> res = new List<equation>();

            Factorize factorize = new Factorize(n);
	     	foreach(FactorBlock fb in factorize.FactorList)
	      {
            	equation eq = new equation(a, b % (fb.NumberBase ^ fb.Exponent), fb.NumberBase, fb.Exponent);
                  res.Add(eq);
            }

            return res;
  	}

	public string ToString()
	{
		string sa = "";
		if (a != 1) sa = a.ToString() + "·";

		string se = "";
		if (e != 1) se = "^" + e.ToString() + "";
			
		return string.Format("{0}x ≡ {1} (mod {2}{3})", sa, b, p, se); 
	}

	public string ToTeX()
	{
		string sa = "";
		if (a != 1) sa = a.ToString() + "\\cdot ";

		string se = "";
		if (e != 1) se = "^{" + e.ToString() + "}";
			
		return string.Format("{0}x &\\equiv &{1}\\ (&\\Mod {2}{3})\\\\", sa, b, p, se); 
	}
}

bool SeqEqual(List<equation> seq1, List<equation> seq2)
{
	if (seq1.Count != seq2.Count)
		return false;
	for (int i = 0; i < seq1.Count; i++)
	{
		if (seq1[i] != seq2[i])
			return false;
	}
	return true;
}

void TraceSEQ()
{
	#Trace "\n"
	if (LATEX)
	{
		#Trace "$$\n\\left.\\begin{aligned}\n"
		foreach (equation eq in seq)
		{
			#Trace eq.ToTeX(), "\n";

		} 
		#Trace "\\end{aligned}\\", "right\\}\n$$\n\n"
	}
	else
	{
		foreach (equation eq in seq)
			#Trace eq.ToString(), "\n";
	}
	#Trace "\n"
}


string solve(params big[] coef)
{
	#Trace clear
	
	if (LATEX)
		#Trace "% ---------- LIMEX: solución generada por el programa TCR Generalizado\n\n"

	seq = new List<equation>();

	for (int i = 0; i < coef.Length; i += 3)
	{
		equation ec = new equation(coef[i], coef[i+1], coef[i+2], 1);
		seq.Add(ec);
	}
	
	#Trace "El sistema es:"
	TraceSEQ();

	// Convierte cada ecuacón en la equivalente en su módulo
	List<equation> seq_m = new List<equation>();
	foreach (equation eq in seq)
	{
		equation eq_n = eq.ToModule();
		seq_m.Add(eq_n);
	}
	

	if (!SeqEqual(seq, seq_m))		
	{
		seq = seq_m;
	
		#Trace "Convertimos cada ecuación en la equivalente en su módulo:"
		TraceSEQ();
	}
	
	if (LATEX)
		#Trace "Comprobamos que todas las ecuaciones sean compatibles: cada $\\m", "cd(a_i,n_i)$ debe dividir a $b_i$.\n\n"
	else
		#Trace "Comprobamos que todas las ecuaciones sean compatibles: cada m", "cd(a[i],n[i]) debe dividir a b[i].\n\n"

	for (int i = 0; i < seq.Count; i++)
	{
		equation eq = seq[i];

		big g = mcd(eq.a, eq.p);
		if (eq.b % g != 0)
		{
			if (LATEX)
			{
				#Trace "La ecuación nº ", i+1, " es incompatible, ya que $d_{", i+1, "}=\\m", "cd(a_{", i+1, "}, n_{", i+1, "})=\\m", "cd(", eq.a, ",", eq.n, ")=", mcd(eq.a, eq.n),  "$ no divide a $b_{", i+1, "}=", eq.b, "\\quad\\error$"
				#Trace "\n\n% ---------- LIMEX: fin de la solución ----------\n\n"
			}
			else
			{
				#Trace "La ecuación nº ", i+1, " es incompatible, ya que d=m", "cd(a[", i+1, "], n[", i+1, "])=m", "cd(", eq.a, ",", eq.n, ")=", mcd(eq.a, eq.n),  " no divide a b[", i+1, "]=", eq.b
			}

			return "Sin solución";
		}
	}
	
	// Simplifica las ecuaciones
	List<equation> seq_s = new List<equation>();
	for (int i =0; i < seq.Count; i++)
	{
		equation eq = seq[i];
		
		
		equation eq_s = eq.Simplify();
		if (eq_s.a == 0)
		{
			if (LATEX)
			{
				#Trace "La ecuación nº ", i+1, " es incompatible, ya que $d_{", i+1, "}=\\m", "cd(a_{", i+1, "}, n_{", i+1, "})=\\m", "cd(", eq.a, ",", eq.n, ")=", mcd(eq.a, eq.n),  "$ no divide a $b_{", i+1, "}=", eq.b, "\\quad\\error$"
				#Trace "\n\n% ---------- LIMEX: fin de la solución ----------\n\n"
			}
			else
			{
				#Trace "La ecuación nº ", i+1, " es incompatible, ya que d=m", "cd(a[", i+1, "], n[", i+1, "])=m", "cd(", eq.a, ",", eq.n, ")=", mcd(eq.a, eq.n),  " no divide a b[", i+1, "]=", eq.b
			}

			return "Sin solución";
		}
		seq_s.Add(eq_s);
	}


	if (!SeqEqual(seq, seq_s))		
	{
		seq = seq_s;
		if (LATEX)
			#Trace "Simplificamos cada ecuación dividiendo por el correspondiente $d_i = \\m" + "cd(a_i, n_i)$. \nSi algún $b_i$ no es divisible entre $d_i$, el sistema es incompatible:";
		else
			#Trace "Simplificamos cada ecuación dividiendo por el correspondiente d[i] = m" + "cd(a[i], n[i]). \nSi algún b[i] no es divisible entre d[i], el sistema es incompatible:";
	
		TraceSEQ();
	}

	// Normaliza las ecuaciones
	List<equation> seq_n = new List<equation>();
	foreach (equation eq in seq)
	{
		equation eq_n = eq.Normalize();
		seq_n.Add(eq_n);
	}

	if (!SeqEqual(seq, seq_n))		
	{
		seq = seq_n;
	
	
		if (LATEX)
			#Trace "Normalizamos, haciendo en cada ecuación $b_i = b_i\\cdot a_i^{-1},\\, a_i = 1$:"
		else
			#Trace "Normalizamos, haciendo en cada ecuación b[i] = b[i]·a[i]^-1, a[i] = 1:"

		TraceSEQ();
	}

	// Comprobamos si tiene solución
	#Trace "Cada ecuación es compatible. Ahora estudiamos si \\textit{el sistema}  es compatible:\n"
	#Trace "\n"

	if (LATEX)
		#Trace "$$\n\\begin{aligned}\n"

	bool compatible = true;

	for (int i = 0; i < seq.Count; i++)
	{
		for (int j = i+1; j < seq.Count; j++)
		{
			big d = mcd(seq[i].n, seq[j].n);
			
			if (LATEX)
			{
				if (d == 1 || abs(seq[i].b  - seq[j].b) % d == 0)
				{
					#Trace "\\m", "cd(&", seq[i].n, ", ", seq[j].n, ") & = && ", d, " & \\quad&\\text{ divide a } && (", abs(seq[i].b), " - ", abs(seq[j].b), ") &&\\quad\\acierto\\\\ \n"
				}
				else
				{
					compatible = false;
					#Trace "\\m", "cd(&", seq[i].n, ", ", seq[j].n, ") & = && ", d, " & \\quad&\\text{ no divide a } && (", abs(seq[i].b), " - ", abs(seq[j].b), ") &&\\quad\\error\\\\ \n"
					break;
				}
				
			}
			else
			{
 				if (d == 1 || abs(seq[i].b  - seq[j].b) % d == 0)
				{
					#Trace "m", "cd(", seq[i].n, ", ", seq[j].n, ") = ", d, " divide a (", abs(seq[i].b), " - ", abs(seq[j].b), ") \n"
				}
				else
				{
					compatible = false;
					#Trace "m", "cd(", seq[i].n, ", ", seq[j].n, ") = ", d, " no divide a (", abs(seq[i].b), " - ", abs(seq[j].b), ")\n"
					break;
				}
			}
						
		}
		
		if (!compatible)
			break;

	}
	
	if (LATEX)
		#Trace "\\end{aligned}\n$$\n\n"



	if (compatible)
	{
		if (LATEX)
			#Trace "\nSe cumplen las condiciones de compatibilidad y por tanto el sistema tiene solución.\\quad\\acierto\n\\vspace*{2ex}\n\n"
			
		else
			#Trace "\nSe cumplen la condiciones de compatibilidad y por tanto el sistema tiene solución.\n\n"
	}
	else
	{
		if (LATEX)
		{
			#Trace "\n\nNo se cumple una condición de compatibilidad y por tanto el sistema no tiene solución.\\Par\\error\n\\vspace*{2ex}\n\n"
			#Trace "\n\n% ---------- LIMEX: fin de la solución ----------\n\n"
		}
		else
		{
			#Trace "\n\nNo se cumple una condición de compatibilidad y por tanto el sistema no tiene solución.\n\n"
		}

		return "Sin solución";
	}

	if (compatible)
	{
		// Expande el sistema    
		List<equation> seq_e = new List<equation>();
		foreach (equation ec in seq)
			foreach (equation eq_e in  ec.Expand())
				seq_e.Add(eq_e);
			
		if (!SeqEqual(seq, seq_e))		
		{
			seq = seq_e;
	
			if (LATEX)
				#Trace "Usamos la factorización de cada $n_i$ para expandir cada ecuación en un sistema donde \nlos módulos son potencias de primos:"
			else
				#Trace "Usamos la factorización de cada n[i] para expandir cada ecuación en un sistema donde \nlos módulos son potencias de primos:"
			TraceSEQ();
		}
		
		// Ordena las ecuaciones
		List<equation> seq_o = new List<equation>();
		foreach (equation eq in seq)
			seq_o.Add (eq); 
		seq_o.Sort(delegate(equation e1, equation e2)
			{
				if (e1.p < e2.p) return -1;
				else if (e1.p > e2.p) return 1;
				else if (e1.e < e2.e) return 1;
				else if (e1.e > e2.e) return -1;
				else return 0;
        			});
		if (!SeqEqual(seq, seq_o))		
		{
			seq = seq_o;

			#Trace "Ordenamos según el módulo: ascendente según el primo y, para el mismo primo, descendente según el exponente:"
			TraceSEQ();
		}

		// Como el sistema es compatible, es la primera ecuación del primo p la que se queda
		big p = 1; // valor imposible
		List<equation> seq_p = new List<equation>();
		int r = 0;
		while  (r < seq.Count)
		{
			if (seq[r].p != p)
			{ 
				p = seq[r].p;
				seq_p.Add(seq[r]);
			}
			r++;
		}	 
	
		if (!SeqEqual(seq, seq_p))		
		{
			seq = seq_p;

			#Trace "Como el sistema es compatible, de las ecuaciones con módulo una potencia del mismo primo \nnos quedamos con la de mayor exponente:"
			TraceSEQ();		
		}

	}
	
	#Trace "Resolvemos el sistema mediante el Teorema Chino del Resto:\n"
	string res = TCR(seq);

	if (LATEX)
		#Trace "\n\n% ---------- LIMEX: fin de la solución ----------\n\n"

	return res;
}

string TCR(List<equation> seq)
{
	int k = seq.Count;

	big[] b = new big[k];
	big[] n = new big[k];
	big[] c = new big[k];
	big[] d = new big[k];
	big m = 1;
	big x = 0;

	// Los b[i]
	for (int i = 0; i < k; i++)
		b[i] = seq[i].b;

	// Los n[i]
	for (int i = 0; i < k; i++)
		n[i] = seq[i].n;

	// El módulo
	for (int i = 0; i < k; i++)
		m *= n[i];
	
	// Comprueba que todos los n[i] son primos dos a dos
	bool ok = true;
	for (int i = 0; i < k; i++)
	{
		for (int j = i + 1; j < k; j++)
		{
			if (mcd(n[i], n[j]) != 1)
			{
				ok = false;
				break;
			}
		}
	}

	if (ok)
	{
		// Los c[i]
		for (int i = 0; i <  k; i++)
		{
			c[i] = 1;
			for (int j = 0; j < k; j++)
				if (j != i) c[i] *= n[j];
		}

		// Los d[i]
		for (int i = 0; i <  k; i++)
		{
			d[i] = modinv(c[i], n[i]);
		}

		// La solución
		x = 0;
		for (int i = 0; i < k; i++)
		{
			x += b[i] * c[i] * d[i];
		}
	}

	if (!ok)
	{
		#Trace "Los módulos no son primos entre sí dos a dos."
		return "Error";
	}

	if (!LATEX)
	{
            for (int i = 0; i < k; i++)
            	#Trace string.Format("n[{0}] = {1} \t\tb[{0}] = {2} \t\tc[{0}] = {3} \t\td[{0}] = {3}^-1 (mod {1}) = {4}^-1 (mod {1}) = {5}\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);

		#Trace "\nSolución particular: "
		for (int i = 0; i < k; i++)
		{	
			#Trace b[i], "·", c[i], "·", d[i]
			if (i < k-1)
				#Trace  " + "
		}
		#Trace  " = ", x, " mod ", m, " = ", x % m, "\n"

		#Trace "\nSolución general: x = ", x % m, " + ", m, "·k"
	}

	else
	{	
		#Trace "aplicando el algoritmo de resolución del TCR obtenemos los valores\n\\vspace*{1ex}\n\n" 
           	#Trace "\\", "renewcommand{\\", "arraystretch}{1.5}\n$$\n\\begin{array}{|l|l|l|rcrcrcr|}\n\\hline\n";
            for (int i = 0; i < k; i++)
            	#Trace string.Format("n_{{{0}}}={1} & b_{{{0}}}={2} & c_{{{0}}}={3} & d_{{{0}}} &=&{{{3}}}^{{-1}}\\ (\\Mod\\ {1})&=&{{{4}}}^{{-1}}\\ (\\Mod\\ {1})&=&{5}\\\\ \\hline\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);
            #Trace "\\end{array}\n$$\n\\", "renewcommand{\\", "arraystretch}{1}\n";

		#Trace "\n\\vspace*{1ex}\n\nUna solución es $"
		for (int i = 0; i < k; i++)
		{
			#Trace b[i] * c[i] * d[i]
			if (i < k-1)
				#Trace "+"
			else
				#Trace "=", x, "\\equiv ", x % m, "\\ (\\Mod ", m, ")$.\n"
		}

		#Trace "La solución general es $x = ", x%m, " + ", m, "\\cdot  \\lambda",  "$ con $\\lambda\\in\\Z$.\n"	
	}

	if (ok)
		return string.Format("x ≡ {0} (mod {1})", x % m, m);
	else
		return "Error";
}

#return solve(eqdata);
// 
// Resuelve el TCR general (los módulos no necesariamente primos entre sí dos  a dos).
// Cada ecuación es de la forma a·x ≡ b (mod n) 
//
// El sistema se da como una lista de números. 
// Cada 3 números son una ecuación a, b, n:
//  2 x ≡ 2 (mod 3)  
//  3 x ≡ 3 (mod 5)
//  4 x ≡ 2 (mod 7)
//
// se daría como: 2, 2, 3, 3, 3, 5, 4, 2, 7
// o si se quiere más claro, dos por cada línea:
// 2, 2, 3, 
// 3, 3, 5, 
// 4, 2, 7
//

big[] eqdata = new big[] {
//
//--------------------------------------------------------------------------------------
// Pon aquí la lista de coeficientes separados por comas: a1, b1, n1, a2, b2, n2, ...
3,3,21,
3,5,10,
2,4,9
// --------------------------------------------------------------------------------------
//
};

// El sistema de ecuaciones se guarda en una lista
List<equation> seq;

// Representación de la ecuación a·x ≡ b (mod p^e)
public struct equation
{
	public big a;
	public big b;
	public big p;
	public big e;
	
	public equation(big a, big b, big p, big e)
	{
		this.a = a;
		this.b = b;
		this.p = p;
		this.e = e;

		if (a == 0 || p <= 1 || e <= 0)
			throw new Exception("Ecuación mal construida");
	}
	
	public big n { get { return p^e; } }

	public static bool operator==(equation eq1, equation eq2)
	{
		return (eq1.a == eq2.a && eq1.b == eq2.b && eq1.p == eq2.p && eq1.e == eq2.e);
	}
	public static bool operator!=(equation eq1, equation eq2)
	{
		return (eq1.a != eq2.a || eq1.b != eq2.b || eq1.p != eq2.p || eq1.e != eq2.e);
	}


	public equation ToModule()
	{
		return new equation(a % n, b % n, p, e);
	}
		
	public equation Simplify()
	{
		big d = mcd(a, n);
		if (b % d == 0)
			return new equation(a/d, b/d, n/d, 1);
		else
			return new equation (0,0,0,0);
	}

	public equation Normalize()
	{
		if (a == 1) return this;
		
		big a1 =  modinv(a, n);
		return new equation (1, (a1 * b) % n, n, 1);
	}

     	public List<equation> Expand()
      {
		List<equation> res = new List<equation>();

            Factorize factorize = new Factorize(n);
	     	foreach(FactorBlock fb in factorize.FactorList)
	      {
            	equation eq = new equation(a, b % (fb.NumberBase ^ fb.Exponent), fb.NumberBase, fb.Exponent);
                  res.Add(eq);
            }

            return res;
  	}

	public string ToString()
	{
		string sa = "";
		if (a != 1) sa = a.ToString() + "·";

		string se = "";
		if (e != 1) se = "^" + e.ToString() + "";
			
		return string.Format("{0}x ≡ {1} (mod {2}{3})", sa, b, p, se); 
	}

	public string ToTeX()
	{
		string sa = "";
		if (a != 1) sa = a.ToString() + "\\cdot ";

		string se = "";
		if (e != 1) se = "^{" + e.ToString() + "}";
			
		return string.Format("{0}x &\\equiv &{1}\\ (&\\Mod {2}{3})\\\\", sa, b, p, se); 
	}
}

bool SeqEqual(List<equation> seq1, List<equation> seq2)
{
	if (seq1.Count != seq2.Count)
		return false;
	for (int i = 0; i < seq1.Count; i++)
	{
		if (seq1[i] != seq2[i])
			return false;
	}
	return true;
}

void TraceSEQ()
{
	#Trace "\n"
	if (LATEX)
	{
		#Trace "$$\n\\left.\\begin{aligned}\n"
		foreach (equation eq in seq)
		{
			#Trace eq.ToTeX(), "\n";

		} 
		#Trace "\\end{aligned}\\", "right\\}\n$$\n\n"
	}
	else
	{
		foreach (equation eq in seq)
			#Trace eq.ToString(), "\n";
	}
	#Trace "\n"
}


string solve(params big[] coef)
{
	#Trace clear
	
	if (LATEX)
		#Trace "% ----- LIMEX\n\n"

	seq = new List<equation>();

	for (int i = 0; i < coef.Length; i += 3)
	{
		equation ec = new equation(coef[i], coef[i+1], coef[i+2], 1);
		seq.Add(ec);
	}
	
	#Trace "El sistema es:"
	TraceSEQ();

	// Convierte cada ecuacón en la equivalente en su módulo
	List<equation> seq_m = new List<equation>();
	foreach (equation eq in seq)
	{
		equation eq_n = eq.ToModule();
		seq_m.Add(eq_n);
	}
	

	if (!SeqEqual(seq, seq_m))		
	{
		seq = seq_m;
	
		#Trace "Convertimos cada ecuación en la equivalente en su módulo:"
		TraceSEQ();
	}
	
	bool error = false;
	if (LATEX)
	{
		#Trace "Llamamos $a_i\\cdot x\\equiv b_i\\ (\\Mod n_i)$ a la ecuación $i$ y "
		#Trace "comprobamos que todas las ecuaciones sean compatibles, \n"
		#Trace "para ello cada $\\m", "cd(a_i,n_i)$ debe dividir a $b_i$:\n\n"
		#Trace "\\begin{center}\n\\begin{itemize*}\n"
		for (int i = 0; i < seq.Count; i++)
		{		
			#Trace "\\item[] $\\m", "cd(", seq[i].a, ", ", seq[i].n, ")="
			#Trace mcd(seq[i].a, seq[i].n)
			if (seq[i].b % mcd(seq[i].a, seq[i].n) == 0) 
			{
				#Trace "\\ |\\ ", seq[i].b, "\\quad\\acierto\\qquad$\n"
			}
			else
			{
				error = true;
				#Trace  "\\ \\", "not|\\", (char)59, seq[i].b, "\\quad\\error\\qquad$\n"
			}
		}	
		#Trace "\\end{itemize*}\n\\end{center}\n\n"	
		if (error)
		{
			#Trace "\nEl sistema no tiene solución."
			return "Sin solución";
		}
	}
	else  
	{
		#Trace "Llamamos a[i]x ≡ b[i] (mod n_i) a la ecuación i y "
		#Trace "comprobamos que todas las ecuaciones sean compatibles; para ello cada m", "cd(a[i],n[i]) debe dividir a b[i]:\n\n"
		for (int i = 0; i < seq.Count; i++)
		{		
			#Trace "\\m", "cd(", seq[i].a, ", ", seq[i].n, ")="
			#Trace mcd(seq[i].a, seq[i].n)
			if (seq[i].b % mcd(seq[i].a, seq[i].n) == 0) 
			{
				#Trace " | ", seq[i].b, "\n"
			}
			else
			{
				error = true;
				#Trace " no divide a ", seq[i].b, "\n"
			}
		}	
		if (error)
		{
			#Trace "\nEl sistema no tiene solución."
			return "Sin solución";
		}
	}
	
	
	// Simplifica las ecuaciones
	List<equation> seq_s = new List<equation>();
	for (int i =0; i < seq.Count; i++)
	{
		equation eq = seq[i];
		
		
		equation eq_s = eq.Simplify();
		if (eq_s.a == 0)
		{
			if (LATEX)
			{
				#Trace "La ecuación nº ", i+1, " es incompatible, ya que $d_{", i+1, "}=\\m", "cd(a_{", i+1, "}, n_{", i+1, "})=\\m", "cd(", eq.a, ",", eq.n, ")=", mcd(eq.a, eq.n),  "$ no divide a $b_{", i+1, "}=", eq.b, "\\quad\\error$"
				#Trace "\n\n% ---------- LIMEX: fin de la solución ----------\n\n"
			}
			else
			{
				#Trace "La ecuación nº ", i+1, " es incompatible, ya que d=m", "cd(a[", i+1, "], n[", i+1, "])=m", "cd(", eq.a, ",", eq.n, ")=", mcd(eq.a, eq.n),  " no divide a b[", i+1, "]=", eq.b
			}

			return "Sin solución";
		}
		seq_s.Add(eq_s);
	}


	if (!SeqEqual(seq, seq_s))		
	{
		seq = seq_s;
		if (LATEX)
			#Trace "\nComo cada ecuación es compatible, cada $b_i$ puede dividirse entre el correspondiente $d_i = \\m" + "cd(a_i, n_i)$:";
		else
			#Trace "\nComo cada ecuación es compatible, cada b[i] puede dividirse entre el correspondiente  d[i] = m" + "cd(a[i], n[i]):";
	
		TraceSEQ();
	}

	// Normaliza las ecuaciones
	List<equation> seq_n = new List<equation>();
	foreach (equation eq in seq)
	{
		equation eq_n = eq.Normalize();
		seq_n.Add(eq_n);
	}

	if (!SeqEqual(seq, seq_n))		
	{
		seq = seq_n;
	
	
		if (LATEX)
			#Trace "Normalizamos, haciendo en cada ecuación $b_i = b_i\\cdot a_i^{-1},\\, a_i = 1$:"
		else
			#Trace "Normalizamos, haciendo en cada ecuación b[i] = b[i]·a[i]^-1, a[i] = 1:"

		TraceSEQ();
	}

	// Comprobamos si el sistema tiene solución
	if (LATEX)
	{
		#Trace "Ya hemos visto que cada ecuación es compatible. Ahora estudiamos si \\textit{el sistema}  es compatible:\n\n"
		#Trace "$$\n\\begin{aligned}\n"
	}
	else
	{
		#Trace "Ya hemos visto que cada ecuación es compatible. Ahora estudiamos si el sistema es compatible:\n\n"
	}

	bool compatible = true;

	for (int i = 0; i < seq.Count; i++)
	{
		for (int j = i+1; j < seq.Count; j++)
		{
			big d = mcd(seq[i].n, seq[j].n);
			
			if (LATEX)
			{
				if (d == 1 || abs(seq[i].b  - seq[j].b) % d == 0)
				{
					#Trace "\\m", "cd(&", seq[i].n, ", ", seq[j].n, ") & = && ", d, " & \\quad&\\text{ divide a } && (", abs(seq[i].b), " - ", abs(seq[j].b), ") &&\\quad\\acierto\\\\ \n"
				}
				else
				{
					compatible = false;
					#Trace "\\m", "cd(&", seq[i].n, ", ", seq[j].n, ") & = && ", d, " & \\quad&\\text{ no divide a } && (", abs(seq[i].b), " - ", abs(seq[j].b), ") &&\\quad\\error\\\\ \n"
					break;
				}
				
			}
			else
			{
 				if (d == 1 || abs(seq[i].b  - seq[j].b) % d == 0)
				{
					#Trace "m", "cd(", seq[i].n, ", ", seq[j].n, ") = ", d, " divide a (", abs(seq[i].b), " - ", abs(seq[j].b), ") \n"
				}
				else
				{
					compatible = false;
					#Trace "m", "cd(", seq[i].n, ", ", seq[j].n, ") = ", d, " no divide a (", abs(seq[i].b), " - ", abs(seq[j].b), ")\n"
					break;
				}
			}
						
		}
		
		if (!compatible)
			break;

	}
	
	if (LATEX)
		#Trace "\\end{aligned}\n$$\n\n"



	if (compatible)
	{
		if (LATEX)
			#Trace "\nSe cumplen las condiciones de compatibilidad y por tanto el sistema tiene solución.\\quad\\acierto\n\\vspace*{2ex}\n\n"
			
		else
			#Trace "\nSe cumplen la condiciones de compatibilidad y por tanto el sistema tiene solución.\n\n"
	}
	else
	{
		if (LATEX)
		{
			#Trace "\n\nNo se cumple una condición de compatibilidad y por tanto el sistema no tiene solución.\\Par\\error\n\\vspace*{2ex}\n\n"
			#Trace "\n\n% ----- Fin de LIMEX\n\n"
		}
		else
		{
			#Trace "\n\nNo se cumple una condición de compatibilidad y por tanto el sistema no tiene solución.\n\n"
		}

		return "Sin solución";
	}

	if (compatible)
	{
		// Expande el sistema    
		List<equation> seq_e = new List<equation>();
		foreach (equation ec in seq)
			foreach (equation eq_e in  ec.Expand())
				seq_e.Add(eq_e);
			
		if (!SeqEqual(seq, seq_e))		
		{
			seq = seq_e;
	
			if (LATEX)
				#Trace "Usamos la factorización de cada $n_i$ para expandir cada ecuación en un sistema donde \nlos módulos son potencias de primos:"
			else
				#Trace "Usamos la factorización de cada n[i] para expandir cada ecuación en un sistema donde \nlos módulos son potencias de primos:"
			TraceSEQ();
		}
		
		// Ordena las ecuaciones
		List<equation> seq_o = new List<equation>();
		foreach (equation eq in seq)
			seq_o.Add (eq); 
		seq_o.Sort(delegate(equation e1, equation e2)
			{
				if (e1.p < e2.p) return -1;
				else if (e1.p > e2.p) return 1;
				else if (e1.e < e2.e) return 1;
				else if (e1.e > e2.e) return -1;
				else return 0;
        			});
		if (!SeqEqual(seq, seq_o))		
		{
			seq = seq_o;

			#Trace "Ordenamos según el módulo: ascendente según el primo y, para el mismo primo, descendente según el exponente:"
			TraceSEQ();
		}

		// Como el sistema es compatible, es la primera ecuación del primo p la que se queda
		big p = 1; // valor imposible
		List<equation> seq_p = new List<equation>();
		int r = 0;
		while  (r < seq.Count)
		{
			if (seq[r].p != p)
			{ 
				p = seq[r].p;
				seq_p.Add(seq[r]);
			}
			r++;
		}	 
	
		if (!SeqEqual(seq, seq_p))		
		{
			seq = seq_p;

			#Trace "Como el sistema es compatible, de las ecuaciones con módulo una potencia del mismo primo \nnos quedamos con la de mayor exponente:"
			TraceSEQ();		
		}

	}
	
	#Trace "Resolvemos el sistema aplicando el algoritmo del Teorema Chino del Resto.\n\n"
	string res = TCR(seq);

	if (LATEX)
		#Trace "\n\n% ----- Fin de LIMEX\n\n"

	return res;
}

string TCR(List<equation> seq)
{
	int k = seq.Count;

	big[] b = new big[k];
	big[] n = new big[k];
	big[] c = new big[k];
	big[] d = new big[k];
	big m = 1;
	big x = 0;

	// Los b[i]
	for (int i = 0; i < k; i++)
		b[i] = seq[i].b;

	// Los n[i]
	for (int i = 0; i < k; i++)
		n[i] = seq[i].n;

	// El módulo
	for (int i = 0; i < k; i++)
		m *= n[i];
	
	// Comprueba que todos los n[i] son primos dos a dos
	bool ok = true;
	for (int i = 0; i < k; i++)
	{
		for (int j = i + 1; j < k; j++)
		{
			if (mcd(n[i], n[j]) != 1)
			{
				ok = false;
				break;
			}
		}
	}

	if (ok)
	{
		// Los c[i]
		for (int i = 0; i <  k; i++)
		{
			c[i] = 1;
			for (int j = 0; j < k; j++)
				if (j != i) c[i] *= n[j];
		}

		// Los d[i]
		for (int i = 0; i <  k; i++)
		{
			d[i] = modinv(c[i], n[i]);
		}

		// La solución
		x = 0;
		for (int i = 0; i < k; i++)
		{
			x += b[i] * c[i] * d[i];
		}
	}

	if (!ok)
	{
		#Trace "Los módulos no son primos entre sí dos a dos."
		return "Error";
	}

	if (!LATEX)
	{
            for (int i = 0; i < k; i++)
            	#Trace string.Format("n[{0}] = {1} \t\tb[{0}] = {2} \t\tc[{0}] = {3} \t\td[{0}] = {3}^-1 (mod {1}) = {4}^-1 (mod {1}) = {5}\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);

		#Trace "\nSolución particular: "
		for (int i = 0; i < k; i++)
		{	
			#Trace b[i], "·", c[i], "·", d[i]
			if (i < k-1)
				#Trace  " + "
		}
		#Trace  " = ", x, " mod ", m, " = ", x % m, "\n"

		#Trace "\nSolución general: x = ", x % m, " + ", m, "·k"
	}

	else
	{	
		#Trace "Siguiendo el algoritmo obtenemos los valores:\n\\vspace*{1ex}\n\n" 
           	#Trace "\\", "renewcommand{\\", "arraystretch}{1.5}\n$$\n\\begin{array}{|l|l|l|rcrcrcr|}\n\\hline\n";
            for (int i = 0; i < k; i++)
            	#Trace string.Format("n_{{{0}}}={1} & b_{{{0}}}={2} & c_{{{0}}}={3} & d_{{{0}}} &=&{{{3}}}^{{-1}}\\ (\\Mod\\ {1})&=&{{{4}}}^{{-1}}\\ (\\Mod\\ {1})&=&{5}\\\\ \\hline\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);
            #Trace "\\end{array}\n$$\n\\", "renewcommand{\\", "arraystretch}{1}\n";

		#Trace "\n\\vspace*{1ex}\n\nUna solución es $"
		for (int i = 0; i < k; i++)
		{
			#Trace b[i] * c[i] * d[i]
			if (i < k-1)
				#Trace "+"
			else
				#Trace "=", x, "\\equiv ", x % m, "\\ (\\Mod ", m, ")$.\n"
		}

		#Trace "La solución general es $x = ", x%m, " + ", m, "\\cdot  \\lambda",  "$ con $\\lambda\\in\\Z$.\n"	
	}

	if (ok)
		return string.Format("x ≡ {0} (mod {1})", x % m, m);
	else
		return "Error";
}

#return solve(eqdata);
// 
// Resuelve el TCR especial (los módulos primos entre sí dos  a dos).
// El sistema se da como una lista de números. 
// Cada 2 números son una ecuación:
// x ≡ 2 (mod 3)  
// x ≡ 3 (mod 5)
// x ≡ 2 (mod 7)
//
// se daría como: 2, 3, 3, 5, 2, 7
// o si se quiere más claro, dos por cada línea:
// 2, 3, 
// 3, 5, 
// 2, 7
//

big[] eq = new big[] {
//
//--------------------------------------------------------------------------
// Pon aquí la lista de coeficientes 
// separados por comas: a1, b1, a2, b2,...

	1949, 71*83,
	2757, 41*101,
	2131, 53*59
 
// ----------------------------------------------------------------------------
//
};

string TCR(params big[] args)
{
	int k = args.Length / 2;

	big[] b = new big[k];
	big[] n = new big[k];
	big[] c = new big[k];
	big[] d = new big[k];
	big m = 1;
	big x = 0;



	// Los b[i]
	for (int i = 0; i < 2*k; i+=2)
		b[i/2] = args[i];

	// Los n[i]
	for (int i = 1; i < 2*k; i+=2)
	{
		n[(i-1)/2] = args[i];
	}

	// El módulo
	for (int i = 0; i < k; i++)
	{
		m *= n[i];
	}
	
	#Trace clear
	if (LATEX)
	{
		#Trace "% ----- LIMEX\n\n"

		// Escribe el sistema
		#Trace "El sistema es\n"
		#Trace "$$\n\\left.\\begin{aligned}\n"
		for (int i = 0; i < k; i++)
		{
			#Trace "\tx&\\equiv ", b[i], "\\ (\\Mod ", n[i], ")\\\\\n"
		} 
		#Trace "\\end{aligned}\\", "right\\}\n$$\n\n"

		#Trace "\n\\vspace*{2ex}\nComprobamos que los modulos son primos entre sí dos a dos:\n\n"

	}
	else
	{
		#Trace "El sistema es\n"
		for (int i = 0; i < k; i++)
			#Trace "\tx ≡ ", b[i], " \t(mod ", n[i], ")\n"

		#Trace "\n\nComprobamos que los modulos son primos entre sí dos a dos:\n"

	}
	

	// Comprueba que todos los n[i] son primos dos a dos
	bool ok = true;
	#Trace "\n"

	for (int i = 0; i < k; i++)
	{
		for (int j = i + 1; j < k; j++)
		{
			big n1 = n[i];
			big n2 = n[j];

			if (LATEX)
			{	
				
				#Trace "$$\n\\begin{aligned}\n"
				while (n2 != 0)
				{
					#Trace "&\\m", "cd(", n1, ", ", n2, ") & = & \\\\\n"
					big t = n1;
					n1 = n2;
					n2 = t % n2;
				}
						
				#Trace "&\\m", "cd(", n1, ", ", 0, ") & = & \\quad", n1, "\\\\\n" 
				#Trace "\\end{aligned}\\", "\n$$\n\n"
			}
			else
			{ #Trace n2
				while (n2 != 0)
				{
					#Trace "m", "cd(", n1, ", ", n2, ") = "
					big t = n1;
					n1 = n2;
					n2 = t % n2;
				}
				#Trace "m", "cd(", n1, ", ", 0, ") = "
				#Trace n1, "\n"
			}

			if (mcd(n[i], n[j]) != 1)
			{
				if (LATEX)
					#Trace "\\end{aligned}\\", "right\\}\n$$\n\\vspace*{2ex}\nLos módulos no son primos entre sí dos a dos."
				else
					#Trace "\nLos módulos no son primos entre sí dos a dos."
		
				return "Error";
			}
		}
	}



	if (ok)
	{
		// Los c[i]
		for (int i = 0; i <  k; i++)
		{
			c[i] = 1;
			for (int j = 0; j < k; j++)
				if (j != i) c[i] *= n[j];
		}

		// Los d[i]
		for (int i = 0; i <  k; i++)
		{
			d[i] = modinv(c[i], n[i]);
		}

		// La solución
		x = 0;
		for (int i = 0; i < k; i++)
		{
			x += b[i] * c[i] * d[i];
		}
	}


	if (!LATEX)
	{
		#Trace "\n\nResolviendo:\n\n"

        for (int i = 0; i < k; i++)
          	#Trace string.Format("n[{0}] = {1} \t\tb[{0}] = {2} \t\tc[{0}] = {3} \t\td[{0}] = {3}^-1 (mod {1}) = {4}^-1 (mod {1}) = {5}\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);


		#Trace "\nSolución particular: "
		for (int i = 0; i < k; i++)
		{	
			#Trace b[i], "·", c[i], "·", d[i]
			if (i < k-1)
				#Trace  " + "
		}
		#Trace  " = ", x, " mod ", m, " = ", x % m, "\n"

		#Trace "\nSolución general: x = ", x % m, " + ", m, "·k"
	}

	else
	{	
		#Trace "Podemos entonces aplicar el algoritmo de resolución del Teorema Chino del Resto. Obtenemos los valores:\n\\vspace*{1ex}\n\n" 
           	#Trace "\\", "renewcommand{\\", "arraystretch}{1.5}\n$$\n\\begin{array}{|l|l|l|rcrcrcr|}\n\\hline\n";
            for (int i = 0; i < k; i++)
            	#Trace string.Format("n_{{{0}}}={1} & b_{{{0}}}={2} & c_{{{0}}}={3} & d_{{{0}}} &=&{{{3}}}^{{-1}}\\ (\\Mod\\ {1})&=&{{{4}}}^{{-1}}\\ (\\Mod\\ {1})&=&{5}\\\\ \\hline\n", i+1, n[i], b[i], c[i], c[i] % n[i], d[i]);
            
		#Trace "\\end{array}\n$$\n\\", "renewcommand{\\", "arraystretch}{1}\n";

		#Trace "\n\\vspace*{1ex}\n\nUna solución particular es "
		#Trace "\n$$\\begin{array}{rcrcrcl}\n"
		#Trace "\\multicolumn{1}{c}{b_i} && \\multicolumn{1}{c}{c_i} && \\multicolumn{1}{c}{d_i} && \\multicolumn{1}{c}{n_1\\cdot n_2\\cdot n_3}\\\\ \\hline"
				
           	for (int i = 0; i < k; i++)
		{
 			#Trace b[i], " & \\cdot & ", c[i], " & \\cdot & ", d[i], " & + & ";
			if (i == k-1)
				#Trace "\\Mod ", m, "\\\\\n" 
			else
				#Trace "\\\\\n"
		}
		#Trace "\\end{array}$$\n" 



		#Trace "La solución general es $x = ", x%m, " + ", m, "\\cdot  \\lambda",  "$ con $\\lambda\\in\\Z$.\n"	

		#Trace "\n\n% ----- Fin de LIMEX\n\n"

	}


	if (ok)
		return string.Format("{0} en Z{1}", x % m, m);
	else
		return "Error";
}

#R TCR(eq);	int f(int n)
{
	if (n % 2 == 0)
		return 1;
	else if (n % 3 == 0)
		return 2;
	else
		return 3;
}

int go(int s)
{
	#Trace clear

	for (int i = 0; i <= 24; i++)
	{
		int ki = s + f(i);
		#Trace ki, "\n";
	
		if ((i+1) % 6 == 0)
			#Trace "\n"
	}
	return 0;
}

#return go(1);int[] primes = big.primesBelow2000;

/*

int euclides(int n)
{
	#trace "e_{", i, "} = "
	big e = 1;
	for (int i=0; i<n; i++)
	{
		e *= primes[i];
	}

	e++;

	FactorizeLenstra fact = new FactorizeLenstra();
	fact.Factorize(e);	
	#Trace fact.Factors
	#Trace replace "·", "\\cdot "
	return 0;
}
*/

big Fer(int n)
{
	big dos = 2;
	big dos_elevado_a_n = dos^n;

	big nf = (dos^dos_elevado_a_n)+1 ;

	FactorizeLenstra fac = new FactorizeLenstra();
	fac.Factorize(nf);
	#Trace "F_{", n, "} & = & ", nf;	
	if (fac.Factors.Count > 1)
	{
		#Trace " & = & ", fac.Factors
	}
	return nf;
}
	
int test(int n)
{
	for (int i = 1; i <= n; i++)
	{
		Fer(i);
		#Trace "\\\\ \n"
	}
	#Trace replace "·", "\\cdot "
	return 0;
}

#Return test(5);

bool test()
{
	#Trace clear
	big p1 = 2;
	big p2 = 3;

	while (p2 - p1 < 100)
	{
		p1 = p2;
		p2 = p2.NextPrime();
		#Trace set p1, "\t", p2, "\t->", p2-p1, "\n";
	}
	
	// Comprobamos
	for (big n = p1+1; n < p2; n++)
	{
		if (n.isProbablePrime())
		{
			#Trace n, " es primo";
			return false;
		}
	}
	
	return true;

}

#return test()int lista()
{
	string A = " ABCDEFGHIJKLMNÑOPQRSTUVWXYZ";

	for (int i = 1; i < 28; i++)
	{
		for (int j = 1; j < 28; j++)
		{
			char[] x = {A[i], A[j]};
			string s = new string(x);
			int n = 100*i+j;
			#Trace s, " ", (100*i+j).ToString("0000"), " \t", ((n*n) % 100000).ToString("00000"), "\n";
		}
	}
	return 0;
}

#return lista();public big CalculaAlgo(big n)  
{
	#Trace clear
	big r = n;

	#Trace r.ToString(2), "\n";

	big u = r & 1;  // Obtiene el bit menos significativo

	#Trace u, "\n";	

	r >>= 1; // Elimina el bit menos significativo

	#Trace r.ToString(2), "\n";

	return r;
	
}

public big BSuma(big a, big b)
{
	string abin = a.ToString(2);
	string bbin = b.ToString(2);
	
	// Competa con ceros a la izquierda hasta que ambos sean del mismo tamaño
	if (abin.Length > bbin.Length)
		bbin = padleft(bbin, abin.Length, '0');
	else if (bbin.Length > abin.Length)
		abin = padleft(abin, bbin.Length, '0');
	int len = abin.Length;
	
	#Trace abin, "\n", bbin, "\n"

	// Recorre los bits de las cadenas binarias
	bool mellevo = false;
	string suma = "";

	for (int i = len - 1; i >= 0; i--)
	{
		if (abin[i] == '1' && bbin[i] == '1')
		{
			if (mellevo)
			{
				suma = "1" + suma;
				mellevo = false;
			}
			else
			{
				suma = "0" + suma;
				mellevo = true;
			}
		}
		else if (abin[i] == '0' && bbin[i] == '1')
		{
			if (mellevo)
			{
				suma = "0" + suma;
				mellevo = true;
			}
			else
			{
				suma = "1" + suma;
				mellevo = false;
			}
		}
		else if (abin[i] == '1' && bbin[i] == '0')
		{
			if (mellevo)
			{
				suma = "0" + suma;
				mellevo = true;
			}
			else
			{
				suma = "1" + suma;
				mellevo = false;
			}
		}
	
		else
		{
			if (mellevo)
			{
				suma = "1" + suma;
				mellevo = false;
			}
			else
			{
				suma = "0" + suma;
				mellevo = false;
			}
		}
		
	} 	

 	#Trace suma;

	return new big(suma, 2);	
}

#Return(BSuma(10, 200));big SmallKey(big n)
{
	List<big[]> l = new List<big[]>();
	
/*
	List<big> primes = new List<big>();
	big pr = 2;
	while (pr < n)
	{
		primes.Add(pr);
		pr = pr.NextPrime();
	
*/
	big pq = 1;
	big p = 2;
	while (p < n)
	{
		big q = 3;
		while (q < p)
		{
			if (p * q > n)
			{
				pq = p * q;
				l.Add(new big[] {p, q, pq});
				#Trace p, "·", q, "=", p*q, "\n";
			}

			q = q.NextPrime();
		}
		p = p.NextPrime();
	}

	l.Sort((x, y) => (x[2] == y[2] ? 0 : (x[2] > y[2] ? 1 : -1)));
	
	#Trace clear
	foreach(big[] x in l)
	{
		#Trace x[0], "·", x[1], "=", x[2], "\n";
	}	
	return pq;
} 


/*

para n=2727 obtiene 
59·47=2773
67·41=2747
211·13=2743
547·5=2735
911·3=2733		

para n = 27 obtiene
7·5=35
11·3=33 <-----------------
11·5=55
11·7=77
13·3=39
13·5=65
13·7=91
13·11=143
17·3=51
17·5=85
17·7=119
17·11=187
17·13=221
19·3=57
19·5=95
19·7=133
19·11=209
19·13=247
19·17=323
23·3=69
23·5=115
23·7=161
23·11=253
23·13=299
23·17=391
23·19=437

*/



#return SmallKey(27)				big b1 = 369;
big b2 = 10;

bool TestModFloor()
{
	#T "\\" + "renewcommand*{\\arraystretch}{1.5}\n"
	#T "$$\n\\begin{array}{|r|r||r|r|r||r|r|r|r|r|r|r|r|r|r|r|r|r|r|}\n"
	#T "\\hline\n"

	// Los números:
	#T "x & y & "

	// La división con resto positivo (calculadora):
	#T "x / y & "
	#T "x \\% y & "
	#T "x - \\left[(x\\%y)+y\\cdot x/y \\" + "right]& "

	// La división euclídea (Knuth):
	#T "\\f" + "loor{x/y} & "
	#T "x\\ \\MMod y & "
	#T "x - \\left[(x\\ \\MMod y)+y*\\f" + "loor{x/y}\\" + "right] "
	
	#T "\\\\ \\hline\n"

	#T line(b1, b2)
	#T line(b1, -b2)	
	#T line(-b1, b2)	
	#T line(-b1, -b2)	
	#T "\\end{array}\n"
	#T "$$\n"
	#T "\\" + "renewcommand*{\\arraystretch}{1}\n"
	return true;
}

string line(big x, big y)
{
	return string.Format(
		"{0} & 	{1} & 	{2} & 	{3}     &   {4} &				{5}   & 		{6}  	& 		{7}           		      \\\\ \\hline\n",
	        x,    	y,   		x/y,  	x%y, 	 	x - ((x%y)+y*(x/y)),  	x.Floor(y), 	x.Mod(y), 	 	x - (x.Mod(y) + y*x.Floor(y))
	);
}

#return TestModFloor();
//
// Hace una búsqueda exhaustiva de todas las unidades módulo n.
// Muestra las unidades en parejas (a, a^-1), excepto 
// cuando a^-1=a, que muestra solo (a) y en línea aparte.
//
// Introduce aquí el valor de n:
//

int n = 1239;

// ------------------------------------------------------------------------------

int ListaUnidades(int n)
{
	if (n >= 10000)
		throw new Exception("n debe ser menor que 10000");

	int k = 0;
	
	List<big> units = new List<big>();
 
	#Trace clear
	#: string nunit = "Buscando las";
	#Trace nunit, " unidades en Z*", n, " \nSe agrupan los pares (elemento, inverso):\n";
	#: int j = 0;
	for (int i = 1; i < n; i++)
	{
		if (mcd(i, n) == 1)
		{
			if (!units.Contains(i))
			{
				k++;
				if (!units.Contains(i))
					units.Add(i);
				big inv = modinv(i, n);
				if (i != inv)
				{
					k++;
					units.Add(inv);
					#: j++;
					#Trace "(", i, ", ", inv, ") " 
					#: if (j % 5 == 0)
					#: {
					#: 	#Trace "\n"
					#:	j = 0;
					#: } 
					
				}
				#: else
				#: {
					#Trace "\n\n(", i, ")\n\n"
					#: j = 0;
				#: }
			}
		}
	}

	#Trace replace nunit, k.ToString()

	return k;
}



#Return ListaUnidades(n);