4 package kezvh
.primitive
;
6 import java
.math
.BigInteger
;
7 import java
.util
.Arrays
;
8 import java
.util
.HashMap
;
9 import java
.util
.Iterator
;
10 import java
.util
.LinkedList
;
11 import java
.util
.List
;
13 import java
.util
.Random
;
19 public class Primitive
{
20 private static LinkedList
<BigInteger
> pfacts
= new LinkedList
<BigInteger
>();
21 private static LinkedList
<BigInteger
> primes
= new LinkedList
<BigInteger
>();
23 private static enum Case
{
24 FACTORIAL
, PRIMORIAL
, EXPONENTIAL
27 private static final Case type
= Case
.PRIMORIAL
;
29 private static final BigInteger
increment(final BigInteger i
) {
30 switch (Primitive
.type
) {
32 return i
.add(BigInteger
.ONE
);
34 return i
.nextProbablePrime();
38 throw new RuntimeException();
43 final BigInteger max
= new BigInteger("5000");
45 for (BigInteger cpf
= BigInteger
.valueOf(2), i
= BigInteger
.valueOf(2); i
.compareTo(max
) < 0; i
= Primitive
.increment(i
), cpf
= cpf
.multiply(i
)) {
46 Primitive
.pfacts
.addFirst(cpf
);
47 Primitive
.primes
.addFirst(i
);
50 System
.out
.println("number of pfacts: " + Primitive
.pfacts
.size());
51 System
.out
.println("bits in largest element: " + Primitive
.primes
.getFirst().toString(2).length());
52 System
.out
.println("bits in largest pfact: " + Primitive
.pfacts
.getFirst().toString(2).length());
55 private static Map
<Integer
, Character
> rad
= new HashMap
<Integer
, Character
>() {
57 final char[] chars
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?".toCharArray();
58 for (int i
= 0; i
< chars
[i
]; i
++)
59 this.put(i
, chars
[i
]);
66 public static void main(final String
... args
) {
73 public static void two() {
74 // final Random r = new Random(0);
75 BigInteger foo
= new BigInteger("1").nextProbablePrime(); // 686
76 for (int i
= 0; i
< 100; i
++) {
77 System
.out
.println(Primitive
.format(foo
));
78 foo
= foo
.nextProbablePrime(); // 686
80 System
.out
.println(Primitive
.format(foo
));
86 public static void zero() {
87 final Random r
= new Random();
88 System
.out
.println(Primitive
.format(new BigInteger(500, r
).nextProbablePrime())); // .nextProbablePrime()
94 public static void pointFive() {
95 // final Random r = new Random();
96 // BigInteger baz = new BigInteger(70, r).nextProbablePrime();
97 BigInteger baz
= new BigInteger("1107336284477113445057");
98 System
.out
.println(baz
.bitLength());
99 final LinkedList
<BigInteger
> dfs
= Primitive
.dfs(baz
);
101 System
.out
.println("null...");
104 final Iterator
<BigInteger
> i
= dfs
.descendingIterator();
106 while (i
.hasNext()) {
107 final BigInteger next
= i
.next();
108 final BigInteger fact
= baz
.subtract(next
);
109 final int o
= Primitive
.pfacts
.indexOf(fact
);
111 throw new RuntimeException(fact
+ " is not a factorial...? next = " + next
+ ", baz = " + baz
);
112 System
.out
.println(baz
+ " - " + (Primitive
.pfacts
.size() - o
+ 1) + "! = " + next
);
115 System
.out
.println(dfs
);
121 public static void three() {
122 System
.out
.println(Primitive
.format(Primitive
.pfacts
.get(100).nextProbablePrime()));
124 System
.out
.println(Primitive
.format(Primitive
.pfacts
.get(99).nextProbablePrime()));
126 System
.out
.println(Primitive
.format(Primitive
.pfacts
.get(98).nextProbablePrime()));
132 public static void one() {
133 final Random r
= new Random();
134 // final BigInteger foo = new BigInteger("59");
135 final BigInteger bar
= new BigInteger(80, r
).nextProbablePrime();
136 final BigInteger foo
= new BigInteger(80, r
).nextProbablePrime();
137 // final BigInteger bar = new BigInteger("509");
138 final BigInteger baz
= foo
.multiply(bar
);
139 System
.out
.println(baz
.toString(2).length());
141 // System.out.print(foo + ": ");
142 // System.out.println(Primitive.format(foo));
143 // System.out.print(bar + ": ");
144 // System.out.println(Primitive.format(bar));
145 System
.out
.println(baz
+ ": ");
146 System
.out
.println(Primitive
.format(baz
));
152 public static void four() {
153 final Random r
= new Random(System
.nanoTime() + System
.currentTimeMillis());
157 for (i
= 0; i
< 100; i
++) {
158 final BigInteger baz
= new BigInteger(70, r
).nextProbablePrime();
159 final List
<BigInteger
> dfs
= Primitive
.dfs(baz
);
164 System
.out
.println(j
+ " / " + i
);
165 // System.out.println(baz);
167 // System.out.println(dfs);
169 // System.out.println("not");
175 public static void five() {
176 BigInteger bucket
= BigInteger
.valueOf(4);
178 int counts
= 0, hits
= 0;
179 for (BigInteger i
= BigInteger
.valueOf(3); true; i
= i
.nextProbablePrime()) {
180 if (i
.compareTo(bucket
) >= 0) {
181 System
.out
.println(bits
+ ": " + hits
+ " / " + counts
+ " = " + (double) hits
/ counts
);
182 bucket
= bucket
.shiftLeft(1);
187 if (Primitive
.hasPropertyOfDifference(i
))
195 public static void six() {
196 final Random r
= new Random();
197 for (int i
= 100; i
< 101; i
++) {
199 for (int j
= 0; j
< 100; j
++) {
200 final BigInteger bi
= new BigInteger(10 * i
, r
).nextProbablePrime();
201 if (Primitive
.hasPropertyOfDifference(bi
))
204 System
.out
.println(i
* 10 + ": " + hits
+ " / " + 100);
211 public static void seven() {
212 final Random r
= new Random();
213 final BigInteger bi
= new BigInteger(100, r
).nextProbablePrime();
214 final int[] partsb
= Primitive
.moogle2(bi
);
216 for (int i
= 1; i
< partsb
.length
; i
++) {
217 final int tmp
= partsb
[i
];
219 final BigInteger pp
= Primitive
.unmoogle(partsb
);
220 if (pp
.isProbablePrime(100))
221 System
.out
.println(i
);
225 System
.out
.println("done");
231 public static void eight() {
232 final Random r
= new Random();
233 final BigInteger bi
= new BigInteger(200, r
).nextProbablePrime();
234 final int[] partsb
= Primitive
.moogle2(bi
);
235 System
.out
.println(bi
);
236 System
.out
.println(Arrays
.toString(partsb
));
237 final List
<BigInteger
> hmm
= Primitive
.reach(partsb
);
238 System
.out
.println(hmm
);
245 public static void nine() {
246 final Random r
= new Random();
247 final BigInteger bi1
= new BigInteger(501, r
).nextProbablePrime();
248 final BigInteger bi2
= new BigInteger(501, r
).nextProbablePrime();
249 final BigInteger bi
= bi1
.multiply(bi2
);
250 final BigInteger bi3
= Primitive
.squareRoot(bi
);
251 System
.out
.println(bi
);
252 System
.out
.println(bi3
.multiply(bi3
));
253 System
.out
.println(bi3
.bitLength());
257 private static BigInteger
squareRoot(final BigInteger bi
) {
258 final BigInteger two
= BigInteger
.valueOf(2);
259 final BigInteger n
= bi
;
260 BigInteger i
= BigInteger
.ONE
;
264 final BigInteger f
= n
.divide(g
);
265 final BigInteger h
= f
.add(g
);
267 } while (!g
.subtract(i
).equals(BigInteger
.ZERO
));
271 private static List
<BigInteger
> reach(final int[] partsb
) {
273 for (int i
= 0; i
< partsb
.length
- 1; i
++) {
276 final int tmp
= partsb
[i
];
277 while (partsb
[i
] > 0) {
279 final BigInteger pp
= Primitive
.unmoogle(partsb
);
280 if (BigInteger
.ONE
.equals(pp
))
281 return new LinkedList
<BigInteger
>() {
283 this.add(BigInteger
.ONE
);
287 if (pp
.isProbablePrime(100)) {
288 final List
<BigInteger
> maybe
= Primitive
.reach(partsb
);
300 private static int[] moogle2(final BigInteger bi
) {
301 BigInteger divisor
= bi
.add(BigInteger
.ZERO
);
303 final int[] values
= new int[Primitive
.primes
.size()];
305 final Iterator
<BigInteger
> i
= Primitive
.primes
.descendingIterator();
306 while (i
.hasNext()) {
307 final BigInteger prime
= i
.next();
308 final BigInteger
[] divs
= divisor
.divideAndRemainder(prime
);
309 values
[j
] = divs
[1].intValue();
314 int x
= values
.length
- 1;
315 while (values
[x
] == 0)
318 final int[] values2
= new int[x
+ 1];
319 for (int y
= 0; x
>= 0; y
++, x
--)
320 values2
[y
] = values
[x
];
325 private static BigInteger
unmoogle(final int[] remainders
) {
326 BigInteger sum
= BigInteger
.valueOf(remainders
[remainders
.length
- 1]);
327 final Iterator
<BigInteger
> i
= Primitive
.pfacts
.descendingIterator();
328 for (int j
= remainders
.length
- 2; j
>= 0; j
--) {
329 final BigInteger pfact
= i
.next();
330 sum
= sum
.add(pfact
.multiply(BigInteger
.valueOf(remainders
[j
])));
335 private static boolean hasPropertyOfDifference(final BigInteger bi
) {
336 final Iterator
<BigInteger
> i
= Primitive
.pfacts
.descendingIterator();
337 while (i
.hasNext()) {
338 final BigInteger n
= i
.next();
339 if (n
.compareTo(bi
) >= 0)
341 if (bi
.subtract(n
).isProbablePrime(100))
344 throw new RuntimeException();
351 public static String
format(final BigInteger bi
) {
352 return Primitive
.format(bi
, " ");
358 public static final BigInteger THREE
= BigInteger
.valueOf(3);
360 private static LinkedList
<BigInteger
> dfs(final BigInteger b
) {
361 final Iterator
<BigInteger
> i
= Primitive
.pfacts
.descendingIterator();
362 while (i
.hasNext()) {
363 final BigInteger pfact
= i
.next();
364 // for (int i = 0; i < Primitive.pfacts.size(); i++) {
365 // final BigInteger pfact = Primitive.pfacts.get(Primitive.pfacts.size() - i - 1);
366 if (b
.compareTo(pfact
) < 0)
369 final BigInteger next
= b
.subtract(pfact
);
370 if (next
.equals(BigInteger
.ONE
))
371 return new LinkedList
<BigInteger
>() {
373 this.add(BigInteger
.ONE
);
377 if (next
.isProbablePrime(100)) {
378 final LinkedList
<BigInteger
> possible
= Primitive
.dfs(next
);
379 if (possible
!= null) {
393 public static String
format(final BigInteger bi
, final String delim
) {
394 BigInteger current
= bi
.abs();
395 final StringBuilder sb
= new StringBuilder();
397 boolean hasvalues
= false;
398 int i
= Primitive
.pfacts
.size() + 1;
399 if (!"".equals(delim
))
400 System
.out
.println("original is " + bi
.isProbablePrime(100));
401 for (final BigInteger pfact
: Primitive
.pfacts
) {
404 while (current
.compareTo(pfact
) >= 0) {
406 current
= current
.subtract(pfact
);
411 if (!"".equals(delim
)) {
412 final List
<Integer
> haszx
= new LinkedList
<Integer
>();
413 final List
<Integer
> haszm
= new LinkedList
<Integer
>();
415 for (int o
= 1; o
<= i
+ 1; o
++)
416 if (current
.subtract(Primitive
.pfacts
.get(Primitive
.pfacts
.size() - o
)).isProbablePrime(100))
419 BigInteger blargh
= current
.abs();
420 // System.out.println(Primitive.primes.get(Primitive.pfacts.size() - i).intValue());
421 // System.out.println(pfact + " " + Primitive.primes.get(Primitive.pfacts.size() - i));
422 for (int q
= 0; q
<= Primitive
.primes
.get(Primitive
.pfacts
.size() - i
).intValue(); q
++) {
423 if (blargh
.isProbablePrime(100))
425 if (!blargh
.equals(bi
) && !"".equals(delim
) && !BigInteger
.ONE
.equals(blargh
) && !BigInteger
.ZERO
.equals(blargh
) && BigInteger
.ZERO
.equals(bi
.remainder(blargh
)))
426 System
.out
.println("= " + blargh
+ " * " + bi
.divide(blargh
));
427 blargh
= blargh
.add(pfact
);
429 System
.out
.println(haszm
);
430 System
.out
.println(haszx
);
431 System
.out
.println("- " + x
+ " * p" + i
+ "# is " + current
.isProbablePrime(100));
433 if (Primitive
.rad
.containsKey(x
))
434 sb
.append(Primitive
.rad
.get(x
) + delim
);
436 sb
.append(Primitive
.format(BigInteger
.valueOf(x
), "") + delim
);
440 sb
.append(Primitive
.rad
.get(current
.intValue()));
441 return sb
.toString();