1
0

First Commit

This commit is contained in:
William Welna
2016-11-26 11:04:06 -06:00
parent 273fc77eca
commit 7a20a393d4
6 changed files with 298 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
/*
Copyright (C) 2016 William Welna (wwelna@occultusterra.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
public class MegaBitMap {
long[] bits=null;
public MegaBitMap(int bytes) {
this.bits = new long[(bytes/8)+1];
}
int whichlong(long index) {
return (int) (index/64);
}
int whichbit(long index) {
return (int) (index%64);
}
public boolean toggle(long index) {
bits[whichlong(index)] ^= (1l << whichbit(index));
return get(index);
}
public void set(long index, boolean value) {
if(value)
bits[whichlong(index)] |= (1l << whichbit(index));
else
bits[whichlong(index)] &= ~(1l << whichbit(index));
}
public boolean get(long index) {
if((bits[whichlong(index)]&(1l << whichbit(index)))!=0)
return true;
else
return false;
}
}
+78
View File
@@ -0,0 +1,78 @@
/*
Copyright (C) 2016 William Welna (wwelna@occultusterra.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.io.FileInputStream;
import java.math.BigInteger;
import java.util.ArrayList;
public class Primes32C implements AutoCloseable {
FileInputStream in = null;
byte[] prime_bytes = new byte[4];
ArrayList<BigInteger> cache = new ArrayList<BigInteger>();
int position = 0;
static final int HOWMANY=203280220;
static final long LASTVALUE=4294967291l;
public Primes32C(String path) throws Exception {
in = new FileInputStream(path);
}
public long getprime() throws Exception {
return getBigPrime().longValue();
}
public BigInteger getBigPrime() throws Exception {
if((this.position>(cache.size()-1)) || cache.size() == 0)
for(int x=0; x<100000; ++x)
cache.add(new BigInteger(Long.toUnsignedString(readprime())));
if(this.position>HOWMANY)
throw new Exception("No More Primes");
return cache.get(this.position++);
}
public int getCacheSize() {
return cache.size();
}
long readprime() throws Exception {
long r=0;
if(in.read(prime_bytes)!=-1) {
r |= (prime_bytes[3]&0xffL);
r |= (prime_bytes[2]&0xffL)<<8;
r |= (prime_bytes[1]&0xffL)<<16;
r |= (prime_bytes[0]&0xffL)<<24;
return r;
} else {
return 0;
}
}
public void reset() {
this.position = 0;
}
public void close() throws Exception {
in.close();
}
}
+50
View File
@@ -0,0 +1,50 @@
/*
Copyright (C) 2016 William Welna (wwelna@occultusterra.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.io.FileInputStream;
public class Primes32U implements AutoCloseable {
FileInputStream in = null;
byte[] prime_bytes = new byte[4];
public Primes32U(String path) throws Exception {
in = new FileInputStream(path);
}
public long getprime() throws Exception {
long r=0;
if(in.read(prime_bytes)!=-1) {
r |= (prime_bytes[3]&0xffL);
r |= (prime_bytes[2]&0xffL)<<8;
r |= (prime_bytes[1]&0xffL)<<16;
r |= (prime_bytes[0]&0xffL)<<24;
return r;
} else {
return -1;
}
}
public void close() throws Exception {
in.close();
}
}
+40
View File
@@ -0,0 +1,40 @@
/*
Copyright (C) 2016 William Welna (wwelna@occultusterra.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class PrimesSwapBytes {
public static void main(String args[]) throws Exception {
FileInputStream in = new FileInputStream(args[0]);
FileOutputStream ou = new FileOutputStream(args[1]);
byte[] in_bytes = new byte[4];
byte[] ou_bytes = new byte[4];
while(in.read(in_bytes)!=-1) {
ou_bytes[0] = in_bytes[3];
ou_bytes[1] = in_bytes[2];
ou_bytes[2] = in_bytes[1];
ou_bytes[3] = in_bytes[0];
ou.write(ou_bytes);
}
}
}
+23
View File
@@ -1,2 +1,25 @@
# SieveEratosthenes
Sieve of Eratosthenes to find all 32-bit prime numbers
### License
Copyright (C) 2016 William Welna (wwelna@occultusterra.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+50
View File
@@ -0,0 +1,50 @@
/*
Copyright (C) 2016 William Welna (wwelna@occultusterra.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
public class SieveEratosthenes {
public static void main(String args[]) throws Exception {
long timeStart, runtime;
// Maxint = 536870911 = 4294967295 / 8
MegaBitMap map = new MegaBitMap(536900000); // Rounded off, just the tip
long findto = 0xFFFFFFFFl;//4294967295l;
timeStart = System.currentTimeMillis();
for(long x=2; x*x<findto; ++x)
if(!map.get(x))
for(long y=x; x*y <= findto; ++y)
map.set(x*y, true);
runtime = System.currentTimeMillis()-timeStart;
System.out.println("Finished at "+runtime/1000);
FileOutputStream ou = new FileOutputStream("SieveEratosthenes.dat", true);
long count=0;
for(long x=3; x<=findto; ++x)
if(!map.get(x)) {
ou.write(ByteBuffer.allocate(4).putInt((int) x).array());
count++;
}
System.out.println("Primes "+count);
runtime = System.currentTimeMillis()-timeStart;
System.out.println("Total Runtime "+runtime/1000);
}
}