import java.io.*; import java.math.*; //==========================================================C // OS(Quadratic Sieve) Program C // 2次ふるい法javaプログラム 原理プログラム C //----------------------------------------------------------C // 1. M = (int)sqrt(N) C // using Bsqrt C // 2. Seting the Factor Base C // Prime Table: PTB[NP] C // Factor Base: FTB[NB] C // 3. Sieving by QS C // Sieved Matrix: MQS[ND][NB] C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/07/27 C // ( Tokyo Kougei University ) C // 後 保範: 東京工芸大学 C //==========================================================C public class QS{ //==========================================================C // Global data (Constant and Work area) C //==========================================================C public int PTB[] = new int[500]; public int FTB[] = new int[250]; public int MQS[][] = new int[300][250]; public int AM[][] = new int[300][250]; public int EM[][] = new int[300][250]; public int LN[][] = new int[30][150]; public int PW[][] = new int[30][250]; public BigInteger MX[] = new BigInteger[300]; public BigInteger FactV[][] = new BigInteger[30][4]; public int NOS; //==========================================================C public BigInteger QS(BigInteger N, int NP, int NV[]) //==========================================================C // OS(Quadratic Sieve) Program C //----------------------------------------------------------C // N Bint, In, Input Value for Factorizing C // NP int, In, Size of Prime Table C // NV[2] int, Out, NV[1]=NB, NV[2]=ND C // return Bint, Out, M=sqrt(N) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/07/27 C // ( Tokyo Kougei University ) C //==========================================================C { int IWK[] = new int[10000]; int NIWK = 10000; int Debug = 0; // M = (int)sqrt(N) BigInteger M = Bsqrt(N); if(Debug==1) { System.out.println("M="+M.toString()); } // Seting the Factor Base TPRIM(NP); int NB = BASE(N, NP); if(Debug==1) { for (int k=0; k NH) { break; } NS1 = j - (LPM+2)%j; if(NS1 == j) { NS1 = 0; } NS = (j*(NS1%2) + NS1)/2; for (i=NS; i= NP) { break; } } } } //==========================================================C public BigInteger BFact(BigInteger X, int NB, int PW[]) //==========================================================C // Factorization with Primes C // X=FTB[0]^PW[0]*...*FTB[N-1]*PW[N-1]*Y C //----------------------------------------------------------C // X Big, In, Target value C // NB int, In, Size of Factor Base C // PW[NP] int, Out, Power of Factor Base C // return Big, Out, No factorized value (Y) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2007/12/31 C // ( Tokyo Kougei University ) C //==========================================================C { BigInteger Y, PK, P0; BigInteger Zero = new BigInteger("0"); Y = X; // Main Loop for (int k=0; k set FTB C //------------------------------------------------------------C // N Bint,In, Number of Factorized C // NP int, In, Size of Prime Table C // return int, Out, Size of Factor Base C //------------------------------------------------------------C // Written by Yasunori Ushiro , 2008/01/12 C // ( Tokyo Kougei University ) C //============================================================C { int NB = 0; int k, j, S2, Nin; // main Loop for (k=0; k=8) m = 6; if(n >=30) m = 8; // BigInteger t = sqrt(x) by Newton Method if(t.compareTo(Zero) != 0) { for (k = 1; k <= m; k++) { w = t.multiply(t); u = w.add(x); w = t.add(t); t = u.divide(w); } } t = t.add(One); return t; } //==========================================================C // Gauss Elimination Program with 0-1 C // Make matrix AM(0-1) from MQS & EM(unit) C // Gauss Elimination A+E C // A=AM[N][M], E=EM[N][N] C //----------------------------------------------------------C // N int, in, Number of rows for AM, EM C // M int, in, Number of columns for AM C // return int, Out, ID >= 1; Number of dependency C // = 0; no-dependency C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/07/27 C // ( Tokyo Kougei University ) C //==========================================================C public int Gauss(int N, int M) throws Exception { int ID, IP, IW; int k, j, i, KM, kk; int IO=0; // Setting Output File FileOutputStream fo = new FileOutputStream("Gauss.txt"); PrintWriter out = new PrintWriter(fo,true); // Make matrix AM(0-1) from MQS & EM(unit) for (k=0; k=1) { out.println("Input"); for (i=0; i KM) KM = kk; IP = 0; if(AM[kk][k] == 0) { // Search Pivot IP = -1; for (i=kk+1; i 0) { ID = N - kk; } return ID; } //==========================================================C // Factor : Factorizeing by the QS C // using: MQS[N][M], EM[N][N], FTB[M] C // output: LN[NO][], PW[NO][M] C //----------------------------------------------------------C // NV Bint, In, Input Value for Factorizing C // MV Bint, In, MV=sqrt(NV), Number of QS starting C // N int, In, Number of rows for MQS, EM C // M int, In, Number of columns for EM C // NO int, In, Number of factorized value C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/07/31 C // ( Tokyo Polytechnic University ) C //==========================================================C public void Factor(BigInteger NV, BigInteger MV, int N, int M, int NO) { int k, j, i, NN, NOP, CN; BigInteger X, Y, XV, YV, YI; BigInteger One = new BigInteger("1"); String s1; // Set to dependet column for (k=0; k= 1) { s1 = Integer.toString(IPW); YI = new BigInteger(s1); s1 = Integer.toString(FTB[i]); YV = new BigInteger(s1); YV = YV.modPow(YI, NV); Y = Y.multiply(YV); Y = Y.mod(NV); } } FactV[k][0] = X; FactV[k][1] = Y; // Comput GCD(X+Y,NV) and GCD(X-Y,NV) XV = X.add(Y); X = XV.gcd(NV); Y = NV.divide(X); FactV[k][2] = X; FactV[k][3] = Y; } } }