Debian Codegraph @

current/shared/Primes.java

raw symbols SCIP
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 * @author Brian Bushnell
14 * @date Oct 9, 2012
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 // int cnt=0;
77
78 for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){
79 // cnt++;
80 // if(cnt>10000){break;}
81 long x=Long.parseLong(s.trim());
82
83 // System.out.println("cnt="+cnt+", x="+x+", next="+next);
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){//Out of bounds
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 // for(long p : primes){
114 // if(p>=x){return p;}
115 // }
116 // throw new RuntimeException("No primes big enough for "+x);
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){//Out of bounds
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;//-1, 0, -300302388, 2
140 assert(x>=primes[0]) : loc0+", "+loc+", "+x+", "+primes[0];
141 while(primes[loc]>x){loc--;}
142 return primes[loc];
143
144 // for(int i=primes.length-1; i>=0; i--){
145 // if(primes[i]<=x){return primes[i];}
146 // }
147 // throw new RuntimeException("No primes small enough for "+x);
148 }
149
150
151 // /**
152 // * @return
153 // */
154 // private static long[] fetchPrimes() {
155 // String fname=Data.findPath("?primes.txt.gz");
156 //
157 // TextFile tf=new TextFile(fname, false);
158 // String[] lines=tf.toStringLines();
159 // long[] array=new long[lines.length];
160 // for(int i=0; i<lines.length; i++){
161 // array[i]=Long.parseLong(lines[i].trim());
162 // }
163 // return array;
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 }