| 1 |
package shared; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.Arrays; |
| 5 |
|
| 6 |
import dna.Data; |
| 7 |
import fileIO.ByteFile1; |
| 8 |
import fileIO.TextFile; |
| 9 |
import fileIO.TextStreamWriter; |
| 10 |
import structures.LongList; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class Primes { |
| 18 |
|
| 19 |
public static void main(String[] args){ |
| 20 |
|
| 21 |
if(args.length==3){makePrimes(args);} |
| 22 |
else{ |
| 23 |
|
| 24 |
|
| 25 |
System.out.println(primeAtLeast(100)); |
| 26 |
System.out.println(primeAtLeast(1000)); |
| 27 |
System.out.println(primeAtLeast(10000)); |
| 28 |
System.out.println(primeAtLeast(100000)); |
| 29 |
System.out.println(primeAtLeast(1000000)); |
| 30 |
System.out.println(primeAtLeast(10000000)); |
| 31 |
System.out.println(primeAtLeast(100000000)); |
| 32 |
System.out.println(primeAtLeast(1000000000)); |
| 33 |
System.out.println(primeAtLeast(10000000000L)); |
| 34 |
System.out.println(primeAtLeast(100000000000L)); |
| 35 |
System.out.println(primeAtLeast(1000000000000L)); |
| 36 |
System.out.println(primeAtLeast(10000000000000L)); |
| 37 |
System.out.println(primeAtLeast(100000000000000L)); |
| 38 |
System.out.println(primeAtLeast(1000000000000000L)); |
| 39 |
|
| 40 |
|
| 41 |
System.out.println(primeAtMost(100)); |
| 42 |
System.out.println(primeAtMost(1000)); |
| 43 |
System.out.println(primeAtMost(10000)); |
| 44 |
System.out.println(primeAtMost(100000)); |
| 45 |
System.out.println(primeAtMost(1000000)); |
| 46 |
System.out.println(primeAtMost(10000000)); |
| 47 |
System.out.println(primeAtMost(100000000)); |
| 48 |
System.out.println(primeAtMost(1000000000)); |
| 49 |
System.out.println(primeAtMost(10000000000L)); |
| 50 |
System.out.println(primeAtMost(100000000000L)); |
| 51 |
System.out.println(primeAtMost(1000000000000L)); |
| 52 |
System.out.println(primeAtMost(10000000000000L)); |
| 53 |
System.out.println(primeAtMost(100000000000000L)); |
| 54 |
System.out.println(primeAtMost(1000000000000000L)); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
public static void makePrimes(String[] args){ |
| 62 |
|
| 63 |
|
| 64 |
String in=args[0]; |
| 65 |
String out=args[1]; |
| 66 |
double mult=Double.parseDouble(args[2]); |
| 67 |
assert(mult>=1); |
| 68 |
|
| 69 |
long next=1; |
| 70 |
|
| 71 |
if(!new File(in).exists()){throw new RuntimeException("File not found: "+in);} |
| 72 |
TextFile tf=new TextFile(in, true); |
| 73 |
TextStreamWriter tsw=new TextStreamWriter(out, true, false, false); |
| 74 |
tsw.start(); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 79 |
|
| 80 |
|
| 81 |
long x=Long.parseLong(s.trim()); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if(x>=next){ |
| 86 |
tsw.print(x+"\n"); |
| 87 |
next=(long)(x*mult); |
| 88 |
} |
| 89 |
} |
| 90 |
tsw.poison(); |
| 91 |
tf.close(); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public static long primeAtLeast(long x){ |
| 97 |
int loc=Arrays.binarySearch(primes, x); |
| 98 |
if(loc<0){ |
| 99 |
loc=-(loc+1); |
| 100 |
assert(loc>=primes.length || primes[loc]>x) : x; |
| 101 |
} |
| 102 |
if(loc>=primes.length){ |
| 103 |
long d=(long)Math.pow(x, 0.4); |
| 104 |
long a=primeAtLeast(x/d); |
| 105 |
long b=primeAtLeast(x/a); |
| 106 |
long c=a*b; |
| 107 |
assert(c>=x && c<(x*9)/8) : x+", "+a+", "+b+", "+c+", "+d; |
| 108 |
return c; |
| 109 |
} |
| 110 |
while(primes[loc]<x){loc++;} |
| 111 |
return primes[loc]; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
public static int primeAtMost(int x){ |
| 120 |
return (int)primeAtMost((long)x); |
| 121 |
} |
| 122 |
|
| 123 |
public static long primeAtMost(final long x){ |
| 124 |
final int loc0=Arrays.binarySearch(primes, x); |
| 125 |
int loc=loc0; |
| 126 |
if(loc<0){ |
| 127 |
loc=-(loc+1); |
| 128 |
assert(loc>=primes.length || primes[loc]>x) : x; |
| 129 |
} |
| 130 |
assert(loc>=0) : loc+", "+x; |
| 131 |
if(loc>=primes.length){ |
| 132 |
final long d=(long)Math.pow(x, 0.4); |
| 133 |
final long a=primeAtMost(x/d); |
| 134 |
final long b=primeAtMost(x/a); |
| 135 |
final long c=a*b; |
| 136 |
assert(c<=x && c>(x*7)/8) : x+", "+a+", "+b+", "+c+", "+d; |
| 137 |
return c; |
| 138 |
} |
| 139 |
assert(loc>=0) : loc+", "+x; |
| 140 |
assert(x>=primes[0]) : loc0+", "+loc+", "+x+", "+primes[0]; |
| 141 |
while(primes[loc]>x){loc--;} |
| 142 |
return primes[loc]; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private static long[] fetchPrimes() { |
| 167 |
String fname=Data.findPath("?primes.txt.gz"); |
| 168 |
|
| 169 |
ByteFile1 tf=new ByteFile1(fname, false); |
| 170 |
LongList list=new LongList(); |
| 171 |
for(byte[] line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 172 |
list.add(Parse.parseLong(line)); |
| 173 |
} |
| 174 |
return list.toArray(); |
| 175 |
} |
| 176 |
|
| 177 |
public static final long[] primes=fetchPrimes(); |
| 178 |
|
| 179 |
} |