cfam-matrix Prime Sieves (java code)


import java.io.*;
import java.util.*;
import java.text.*;
import java.text.DecimalFormat.*;
import java.security.*;

// __
class PrmSieve18{
// __ the product of these prime numbers gives you the largest sieve that a java long (2^63 - 1) can cover
private final long[] lSvPrms = new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L};
private int iSL; // sieve length
// __ W
private File PDatDir; // Path of the Data Directory
private final String aPDatDir = "pdat"; // Data Directory
// __
private final int iSvBfr = 16384; // sieve buffer (I did test many options)
private final int iBitsPByte = 8; // defined in java but not in ANSI C/C++
private final byte[] bBitsSet = new byte[]{
(byte)128, // 2^7 10000000 // no unsigned byte in Java
64, // 2^6 01000000
32, // 2^5 00100000
16, // 2^4 00010000
8, // 2^3 00001000
4, // 2^2 00000100
2, // 2^1 00000010
1 // 2^0 00000001
};
private final long lHGapMax = 510L; // ((510/2)=255 max. value that can be represented in a byte)
private final String aNwLn = System.getProperty("line.separator");

// __ rpt
private final int iChrPL = 120; // characters per line
private final String aMrgn = " "; // margin between primes
private final String aPCF = "0.000000#%"; // percent format
private final DecimalFormat DFPerCent = new DecimalFormat(aPCF);
private final String aDF6thP = "0.000000";
private final DecimalFormat DF6thP = new DecimalFormat(aDF6thP);
private final String aDFIPS = "0.000";
private final DecimalFormat DFFIPS = new DecimalFormat(aDFIPS);

// __
PrmSieve18(int iSL) throws IOException{
String aMsx;
// __
if(iSL == 1){
aMsx = aNwLn + "// __ iSL: |" + iSL + "| sieve would consist of even numbers only!";
throw new IOException(aMsx);
}
else if(iSL < 1){
aMsx = aNwLn + "// __ number of primes used for the sieve may maximally be " + lSvPrms.length + " from 2 to " + lSvPrms[lSvPrms.length - 1];
throw new IOException(aMsx);
}
else if(lSvPrms.length < iSL){
aMsx = aNwLn + "// __ Warning! Maximal length of the sequence of multiplied primes is: " + lSvPrms.length ;
System.err.println(aMsx); System.out.println(aMsx);
iSL = lSvPrms.length;
}// (lSvPrms.length < (iSL - 1))
this.iSL = iSL;
long lMultPrms = 2;
for(int i = 1; (i < this.iSL); ++i){ lMultPrms *= lSvPrms[i]; }
aMsx = aNwLn + "// __ largest sequence of products of primes for a sieve will be " + getFctrsAsText(iSL) + " := " + lMultPrms;
System.err.println(aMsx); System.out.println(aMsx);
}

// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

// __
public String getFctrsAsText(int iFctrs){
StringBuffer aB = new StringBuffer();
aB.append("( 2");
for(int i = 1; (i 0){ lDigBs *= lDecBs; lQ = lN/lDigBs; ++iDPlcs; }
return(iDPlcs);
}

// ~ ~ ~ ~ ~ ~ RPT ~ ~ ~ ~ ~ ~
// __ 4 testing/printing purposes
private String getBitSeq(byte b){
StringBuffer aB = new StringBuffer();
for(int i = (bBitsSet.length - 1); (i > -1); --i){
if((b & (bBitsSet[i] & 0xFF)) == (bBitsSet[i] & 0xFF)){ aB.append("1"); }
else{ aB.append("0"); }
}// i [0, bBitsSet.length)
return(aB.toString());
}

// __
public long[][] setSieves(File ODir) throws FileNotFoundException, IOException, NoSuchAlgorithmException{
// __
if(!ODir.exists()){
File OSubDir = new File(System.getProperty("user.dir"));
ODir = new File(OSubDir, aPDatDir);
if(!ODir.exists()){
if(!ODir.mkdirs()){ throw new IOException( aNwLn + "// __ Prime data directory not found and it could not be made! |" + ODir.getAbsolutePath() + " | make sure the partition is mounted rw!"); }
}
}
// __
PDatDir = ODir;
long[][] lSvsL = null;
// __
int iTp = lSvPrms.length, iBSz, iTmsTp, iRmndrTp, iIxByte, iIxBit, iIxBitSeq, iDPlcs, iSpc, iHPL, iDPlcCnt, iIxL;
long lFctr, lPrevFctr = 2, lFctrM1, lTtlBytes, lFrstBitFfst, lTtlCvrd, lQ, lHGap, lHGapGrtr, lHGapD2, lHole, lPrevHole = Long.MIN_VALUE, lTtlHGps, lTm00;
double dCvrd, dPrevCvrd = .5;
long[] lSRng = new long[lSvPrms.length];
String aFctrs, aOFlHGps, aFlNm, aThrdNm;
byte[] bSvBfr;
File OFl;
FileOutputStream FOS;
BufferedOutputStream BOS;
Runtime JRnTm = Runtime.getRuntime();
KThrdPPRptSieve00 KPPThrdS;

// __
for(int iPrms = 2; (iPrms < (iSL + 1)); ++iPrms){
lFctr = 1L; for(int i = 0; (i iBitsPByte*lTtlBytes){ lTtlBytes += 1; }

iBSz = iSvBfr;
if(lTtlBytes < (long)iSvBfr){ iBSz = (int)lTtlBytes; }
bSvBfr = new byte[iBSz];
for(int j = 0; (j < iBSz); ++j){ bSvBfr[j] = (byte)0; }

System.err.println("// __ iPrms: | " + iPrms + " |, lFctr: | " + lFctr + " := " + aFctrs + " |, lTtlBytes: | " + lTtlBytes + " |, bSvBfr.length: | " + bSvBfr.length + " | ");
System.out.println("// __ iPrms: | " + iPrms + " |, lFctr: | " + lFctr + " := " + aFctrs + " |, lTtlBytes: | " + lTtlBytes + " |, bSvBfr.length: | " + bSvBfr.length + " | ");

// __ Start of ranges
lTtlCvrd = 0; lFrstBitFfst = 0;
for(int k = 0; (k 0){ iRngs[1] = new int[]{iRmndrTp, 1}; }
else{ iRngs[1] = new int[]{-1, 0}; };

// __ debugging
for(int i = 0; (i < iRngs.length); ++i){
System.err.println("// __ iRngs[" + i + "][0]: | " + iRngs[i][0] + " |, iRngs[" + i + "][1]: | " + iRngs[i][1] + " |");
System.out.println("// __ iRngs[" + i + "][0]: | " + iRngs[i][0] + " |, iRngs[" + i + "][1]: | " + iRngs[i][1] + " |");
}// i [0, iRngs.length)

// __ file name of hole gaps in primes' sieve
aOFlHGps = "hgps_" + lSvPrms[0] + "-" + lSvPrms[(iPrms - 1)] + ".bin";
OFl = new File(ODir, aOFlHGps);
OFl.delete(); // @! todo do not overwrite if previously calculated. check metadata such as length and sha256 sum

// __
for(int i = 0; (i < iRngs.length); ++i){
for(int j = 0; (j < iRngs[i][1]); ++j){
for(int k = 0; (k < iPrms); ++k){
for(long l = lSRng[k]; ((l < lFctrM1) && ((l - lFrstBitFfst) < (long)iBitsPByte*iRngs[i][0])); l += lSvPrms[k]){
iIxByte = ((int)(l - lFrstBitFfst))/iBitsPByte;
iIxBit = (int)(l - lFrstBitFfst - iBitsPByte*iIxByte);
iIxBitSeq = bBitsSet.length - iIxBit - 1;
// __
if((bSvBfr[iIxByte] & (bBitsSet[iIxBitSeq] & 0xFF)) != (bBitsSet[iIxBitSeq] & 0xFF)){
bSvBfr[iIxByte] = (byte)((bSvBfr[iIxByte] + bBitsSet[iIxBitSeq]) & 0xFF);
++lTtlCvrd;
}
}// l
}// k [0, iPrms)
// __
BOS = new BufferedOutputStream(new FileOutputStream(OFl, true));
// __
for(int iSB = 0; (iSB < iRngs[i][0]); ++iSB){
// __ ((lFrstBitFfst + iBitsPByte*iSB + iBFfst) < lFctrM1)) because last bit may be within a byte
for(int iBFfst = 0; ((iBFfst < bBitsSet.length) && ((lFrstBitFfst + iBitsPByte*iSB + iBFfst) < lFctrM1)); ++iBFfst){
iIxBitSeq = bBitsSet.length - iBFfst - 1;
if((bSvBfr[iSB] & (bBitsSet[iIxBitSeq] & 0xFF)) == 0){
lHole = lFrstBitFfst + iBitsPByte*iSB + iBFfst;
lHGap = lHole - lPrevHole;
// __
if(lHGap 0){ lHGapD2 = (lHGap >> 1); BOS.write((byte)lHGapD2); }
else{ BOS.write((byte)1); }
lPrevHole = lHole; ++lTtlHGps;
if(lHGap > lHGapGrtr){ lHGapGrtr = lHGap; }
}// (lHGap < lHGapMax)
else{ throw new IOException( aNwLn + "// __ !(lHGap < lHGapMax) lHGap: | " + lHGap + " |, lHGapMax: | " + lHGapMax + " | "); }
}// ((bSvBfr[iSB] & (bBitsSet[iIxBitSeq] & 0xFF)) == 0)
}// iBFfst [0, bBitsSet.length) bit offsets (0:= holes) in current buffer
}// iSB sieve buffer
// __
BOS.close();

// __ resetting firstr bit offset, start of range and buffer with 0`s
lFrstBitFfst += (long)iBitsPByte*iRngs[i][0];
for(int k = 0; (k < iPrms); ++k){
lQ = (long)(lFrstBitFfst/lSvPrms[k]); long lR = (lFrstBitFfst - lQ*lSvPrms[k]);
lSRng[k] = lFrstBitFfst + (lSvPrms[k] - lR) - 1; // remainder form previous buffer & only diffs needed
}// k [0, iPrms)
// __
for(int k = 0; (k iSvBfr), IsRmv = IsZip;
aThrdNm = "_" + lSvPrms[0] + "-" + lSvPrms[(iPrms - 1)] + "_" + (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date(OFl.lastModified()));
rptSieves00(aThrdNm, OFl.getAbsolutePath(), iDPlcs, iHPL, aOTxtFl, IsOverWrite, IsZip, IsRmv);
// __
lPrevFctr = lFctr;
}// ((lFctr/lPrevFctr) == lSvPrms[iPrms - 1])
}// iPrms [2, (iTp + 1))
// __
return(lSvsL);
}

// __ @! "synchronized" paranoid safety thread's work is independent and self reporting
private synchronized void rptSieves00(String aThrdNm, String aIFlPath, int iDPlcs, int iHPL, String aOTxtFl, boolean IsOverWrite, boolean IsZip, boolean IsRmv) throws FileNotFoundException, NoSuchAlgorithmException{
(new KThrdPPRptSieve00(aThrdNm, aIFlPath, iDPlcs, iHPL, aOTxtFl, IsOverWrite, IsZip, IsRmv)).start();
}
}

// __
public class PrmSieve18Test{
private static final String aNwLn = System.getProperty("line.separator");
private static final String aEnc = "UTF-8";
// __
private static final void setOutErr(File ODir, String aPrfx, long lTm00, boolean IsOut, boolean IsErr){
String aLogFl = aPrfx + "_" + (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date(lTm00));
try{
if(IsOut){ PrintStream POS = new PrintStream((new FileOutputStream(new File(ODir, aLogFl + ".out.txt"))), true, aEnc); System.setOut(POS); }
if(IsErr){ PrintStream PES = new PrintStream((new FileOutputStream(new File(ODir, aLogFl + ".err.txt"))), true, aEnc); System.setErr(PES); }
}catch(FileNotFoundException FNFX){ FNFX.printStackTrace(); }
catch(IOException IOX){ IOX.printStackTrace(); }
}

// __
public static void main(String[] args){
// __
long lTm00 = System.currentTimeMillis();
String aKNm = "PrmSieve18Test";
File ODir = new File(System.getProperty("user.dir"));
setOutErr(ODir, aKNm, lTm00, true, false);
// __
int iSvL = 1;
iSvL = 7;
try{
PrmSieve18 PS = new PrmSieve18(iSvL);
PS.setSieves(ODir);
}catch(FileNotFoundException FNFX){ FNFX.printStackTrace(System.err); }
catch(IOException IOX){ IOX.printStackTrace(System.err); }
catch(NoSuchAlgorithmException NSxAlgoX){ NSxAlgoX.printStackTrace(System.err); }
// __
}
}


%d bloggers like this: