In [1]:
#!groovy
println "The Groovy version is " + GroovySystem.version
The Groovy version is 2.4.10
In [ ]:
// Code itself
// run it in your terminal
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Random;
public class AsciiPrime {
private char[] number;
private final int PRIME_CERTAINTY = 100;
private final char FILL_DIGIT = 9;
private final char[] POSSIBLE_DIGITS = {0, 3, 5, 6, 8, 8, 8};
private ArrayList<Integer> fill_positions;
private int width, height;
private Random rnd = new Random();
public AsciiPrime(String prime) {
fill_positions = new ArrayList<>();
String[] rows = prime.split("\n");
height = rows.length;
width = rows[0].length();
number = new char[width * height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
assert rows[y].length() == width : String.format("row %d length was %d, but should be %d", y, rows[y].length(), width);
char cur_number = rows[y].charAt(x);
number[y * width + x] = cur_number;
if (cur_number - '0' == FILL_DIGIT)
fill_positions.add(y * width + x);
}
}
boolean isPrime() {
BigInteger intNumber = new BigInteger(new String(number));
return intNumber.isProbablePrime(PRIME_CERTAINTY);
}
void changeRandomPos() {
int pos = fill_positions.get(rnd.nextInt(fill_positions.size()));
char fill_digit;
while (true) {
fill_digit = (char) (POSSIBLE_DIGITS[rnd.nextInt(POSSIBLE_DIGITS.length)] + '0');
if (number[pos] != fill_digit)
break;
}
number[pos] = fill_digit;
}
public String toString() {
StringBuilder builder = new StringBuilder(width * height + height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
builder.append(number[y * width + x] - '0');
}
builder.append("\n");
}
return builder.toString();
}
public static void main(String[] args) {
String input = "20181111111111111111111111111111111111\n" +
"11111111111111111199111111111111111111\n" +
"11111111111111111999911111111111111111\n" +
"11111111111111119999991111111111111111\n" +
"11111111111111199999999111111111111111\n" +
"11111111111111999999999991111111111111\n" +
"11111111111199999999999999111111111111\n" +
"11111111111111199999999111111111111111\n" +
"11111111111111999999999911111111111111\n" +
"11111111111119999999999991111111111111\n" +
"11111111111999999999999999911111111111\n" +
"11111111119999999999999999991111111111\n" +
"11111111999999999999999999999911111111\n" +
"11111199999999999999999999999999111111\n" +
"11111111111119999999999999111111111111\n" +
"11111111111999999999999999911111111111\n" +
"11111111119999999999999999991111111111\n" +
"11111111999999999999999999999911111111\n" +
"11111199999999999999999999999999111111\n" +
"11119999999999999999999999999999991111\n" +
"11999999999999999999999999999999999911\n" +
"11111111111111111999911111111111111111\n" +
"11111111111111111999911111111111111111\n" +
"11111111111111111999911111111111111111";
AsciiPrime prime = new AsciiPrime(input);
int iterations = 0;
while (!prime.isPrime()) {
prime.changeRandomPos();
iterations++;
if ((iterations % 50) == 0)
System.out.println("Iteration: " + iterations);
}
System.out.println("Iteration: " + iterations);
System.out.println(prime);
}
}
In [ ]:
// Run it in your terminal
// I ran it on my mac
#!java ChristmasPrime.java
Output
Iteration: 50
Iteration: 100
Iteration: 150
Iteration: 200
Iteration: 250
Iteration: 300
Iteration: 350
Iteration: 400
Iteration: 450
Iteration: 500
Iteration: 550
Iteration: 600
Iteration: 650
Iteration: 700
Iteration: 750
Iteration: 800
Iteration: 850
Iteration: 900
Iteration: 950
Iteration: 1000
Iteration: 1050
Iteration: 1100
Iteration: 1150
Iteration: 1200
Iteration: 1250
Iteration: 1300
Iteration: 1350
Iteration: 1400
Iteration: 1450
Iteration: 1500
Iteration: 1550
Iteration: 1600
Iteration: 1650
Iteration: 1700
Iteration: 1750
Iteration: 1800
Iteration: 1850
Iteration: 1900
Iteration: 1929
20181111111111111111111111111111111111
11111111111111111166111111111111111111
11111111111111111363811111111111111111
11111111111111115888551111111111111111
11111111111111150503666111111111111111
11111111111111035080586581111111111111
11111111111180336385368808111111111111
11111111111111153088863111111111111111
11111111111111585808355011111111111111
11111111111110806668638501111111111111
11111111111880558836086035811111111111
11111111115008866088853680851111111111
11111111068838330568836835088311111111
11111188665033008538086886380380111111
11111111111116860663580865111111111111
11111111111886585668088665311111111111
11111111118083588358086868681111111111
11111111336608558555585388603811111111
11111138508888563886033686308988111111
11118086088858858866886866865888061111
11088383888083353885530530880330088511
11111111111111111886811111111111111111
11111111111111111335311111111111111111
11111111111111111363511111111111111111
In [ ]: