import java.io.*; import java.math.*; //==========================================================C // OS(Quadratic Sieve) Function Program C // 2次ふるい法javaプログラム ふるい改良1  C //   ふるいは倍精度浮動小数点を使用し、本来のふるい処理 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 // (1) Setting sieving base value C // Sieving base value: SBV[NS][3] C // (2) Sieving C // Sieved Matrix: MQS[ND][NB] C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/08/15 C // ( Tokyo Kougei University ) C // 後 保範: 東京工芸大学 C //==========================================================C public class QS2{ //==========================================================C // Global data (Constant and Work area) C //==========================================================C public int PTB[] = new int[2000]; public int FTB[] = new int[1000]; public int SBV[][] = new int[3000][3]; public double STB[][] = new double[2][1000000]; public int MQS[][] = new int[1100][1000]; public int AM[][] = new int[1100][1000]; public int EM[][] = new int[1100][1000]; public int LN[][] = new int[300][1000]; public int PW[][] = new int[300][1000]; public BigInteger MX[] = new BigInteger[1100]; public BigInteger FactV[][] = new BigInteger[1000][4]; public int NOS; //==========================================================C public BigInteger QS(BigInteger N, int NP, int LP, int NV[]) //==========================================================C // OS(Quadratic Sieve) Program C // 2次ふるい法javaプログラム ふるい改良1  C //----------------------------------------------------------C // N Bint, In, Input Value for Factorizing C // NP int, In, Size of Prime Table C // LP int, In, Size of one sieve deta C // NV[3] int, Out, NV[0]=NB, NV[1]=ND, NV[2]=NS C // return Bint, Out, M=sqrt(N) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/08/15 C // ( Tokyo Kougei University ) C // 後 保範: 東京工芸大学 C //==========================================================C { int IWK[] = new int[100000]; int NIWK = 100000, BV; 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); // BV = FTB[NB-1]*NB/2; BV = FTB[NB-1]*10; int NS = SBASE(N, M, NB, BV); if(Debug == 1) { for (int k=0; k NB ) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/08/15 C // ( Tokyo Kougei University ) C //==========================================================C { int SV, ND=0; double DV[] = new double[2]; BigInteger MS, BLP; String s1; // Initial set s1 = Integer.toString(LP); BLP = new BigInteger(s1); s1 = N.toString(); DV[0] = Double.parseDouble(s1); s1 = M.toString(); DV[1] = Double.parseDouble(s1); // Main Loop SV = 0; MS = M; while (ND < NB) // Sieving by QS { Dsieve(DV, NS, SV, LP); // Select and set matrix ND = Ssieve(LP, ND, N, MS, NB); SV += LP; MS = MS.add(BLP); } NOS = SV; return ND;} //==========================================================C public int Ssieve(int LP, int NO, BigInteger N, BigInteger MS, int NB) //==========================================================C // Sieving by QS C // select STB and set MQS(sieved matrix) C // STB[0][*] = X*X - N C // STB[1][*] = multiply for base factor C //----------------------------------------------------------C // LP int, In, Size of one sieve deta C // NO int, In, Number of old selected sieve data C // N Bint, In, Number for Factorizing C // MS Bint, In, Start value (M+SV) C // NB int, In, Size of Factor Base (FTB[NB]) C // return int, out, Number of new selected sieve data C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/08/15 C // ( Tokyo Kougei University ) C //==========================================================C { int k, j, NN; int PW[] = new int[2100]; double VL; BigInteger One = new BigInteger("1"); BigInteger X, X2; // Initial set X = MS; NN = NO; VL = FTB[NB-1]; // Main Loop for (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 SBV C // SBV[*][0] = P in FTB C // SBV[*][1] = Q = P^l (l=1,2,...) C // SBV[*][2] = M-S (mod Q) (0 =< S < Q) C //----------------------------------------------------------C // N Bint, In, Input Value for Factorizing C // M Bint,In, M=sqrt(N) C // NB int, In, Size of Factor Base C // MV int, In, Maximum value for sieving value C // return int, Out, Size of SBV(Sieving base value) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2008/08/15 C // ( Tokyo Kougei University ) C //==========================================================C { int NS = 0; int k, j, S2, Nin; int P, Q, IM; // main Loop for (k=0; k set FTB C //----------------------------------------------------------C // N Bint, In, Input Value for Factorizing 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; } } }