Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,64 @@
import java.util.Arrays;
public class Ulam{
enum Direction{
RIGHT, UP, LEFT, DOWN;
}
private static String[][] genUlam(int n){
return genUlam(n, 1);
}
private static String[][] genUlam(int n, int i){
String[][] spiral = new String[n][n];
Direction dir = Direction.RIGHT;
int j = i;
int y = n / 2;
int x = (n % 2 == 0) ? y - 1 : y; //shift left for even n's
while(j <= ((n * n) - 1 + i)){
spiral[y][x] = isPrime(j) ? String.format("%4d", j) : " ---";
switch(dir){
case RIGHT:
if(x <= (n - 1) && spiral[y - 1][x] == null && j > i) dir = Direction.UP; break;
case UP:
if(spiral[y][x - 1] == null) dir = Direction.LEFT; break;
case LEFT:
if(x == 0 || spiral[y + 1][x] == null) dir = Direction.DOWN; break;
case DOWN:
if(spiral[y][x + 1] == null) dir = Direction.RIGHT; break;
}
switch(dir){
case RIGHT: x++; break;
case UP: y--; break;
case LEFT: x--; break;
case DOWN: y++; break;
}
j++;
}
return spiral;
}
public static boolean isPrime(int a){
if(a == 2) return true;
if(a <= 1 || a % 2 == 0) return false;
long max = (long)Math.sqrt(a);
for(long n = 3; n <= max; n += 2){
if(a % n == 0) return false;
}
return true;
}
public static void main(String[] args){
String[][] ulam = genUlam(9);
for(String[] row : ulam){
System.out.println(Arrays.toString(row).replaceAll(",", ""));
}
System.out.println();
for(String[] row : ulam){
System.out.println(Arrays.toString(row).replaceAll("\\[\\s+\\d+", "[ * ").replaceAll("\\s+\\d+", " * ").replaceAll(",", ""));
}
}
}

View file

@ -0,0 +1,67 @@
import java.awt.*;
import javax.swing.*;
public class LargeUlamSpiral extends JPanel {
public LargeUlamSpiral() {
setPreferredSize(new Dimension(605, 605));
setBackground(Color.white);
}
private boolean isPrime(int n) {
if (n <= 2 || n % 2 == 0)
return n == 2;
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(getForeground());
double angle = 0.0;
int x = 300, y = 300, dx = 1, dy = 0;
for (int i = 1, step = 1, turn = 1; i < 40_000; i++) {
if (isPrime(i))
g.fillRect(x, y, 2, 2);
x += dx * 3;
y += dy * 3;
if (i == turn) {
angle += 90.0;
if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1))
step++;
turn += step;
dx = (int) Math.cos(Math.toRadians(angle));
dy = (int) Math.sin(Math.toRadians(-angle));
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Large Ulam Spiral");
f.setResizable(false);
f.add(new LargeUlamSpiral(), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

View file

@ -0,0 +1,79 @@
import java.awt.*;
import javax.swing.*;
public class UlamSpiral extends JPanel {
Font primeFont = new Font("Arial", Font.BOLD, 20);
Font compositeFont = new Font("Arial", Font.PLAIN, 16);
public UlamSpiral() {
setPreferredSize(new Dimension(640, 640));
setBackground(Color.white);
}
private boolean isPrime(int n) {
if (n <= 2 || n % 2 == 0)
return n == 2;
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(2));
double angle = 0.0;
int x = 280, y = 330, dx = 1, dy = 0;
g.setColor(getForeground());
g.drawLine(x, y - 5, x + 50, y - 5);
for (int i = 1, step = 1, turn = 1; i < 100; i++) {
g.setColor(getBackground());
g.fillRect(x - 5, y - 20, 30, 30);
g.setColor(getForeground());
g.setFont(isPrime(i) ? primeFont : compositeFont);
g.drawString(String.valueOf(i), x + (i < 10 ? 4 : 0), y);
x += dx * 50;
y += dy * 50;
if (i == turn) {
angle += 90.0;
if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1))
step++;
turn += step;
dx = (int) Math.cos(Math.toRadians(angle));
dy = (int) Math.sin(Math.toRadians(-angle));
g.translate(9, -5);
g.drawLine(x, y, x + dx * step * 50, y + dy * step * 50);
g.translate(-9, 5);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Ulam Spiral");
f.setResizable(false);
f.add(new UlamSpiral(), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}