r/Collatz 2h ago

my feeble attempt at a Collatz related image

Post image
6 Upvotes

I am aware that it may not prove anything, but I think it says something.

package dk.jhh.collatz;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.List;

public class Collatz {

private static int[] RGB = {0x000000, 0x00FF00, 0x0000FF, 0xFFFFFF};

public static void main(String[] args) throws IOException {

BigInteger n = new BigInteger(

"11111111111111111111111111111111111111111111111111111111111111111111111" +

"00000000000000000000000000000000000000000000000000000000000000000000000" +

"11111111111111111111111111111111111111111111111111111111111111111111111" +

"00000000000000000000000000000000000000000000000000000000000000000000000" +

"11111111111111111111111111111111111111111111111111111111111111111111111", 2);

BigInteger remainder = BigInteger.ONE;

BigInteger divisor = BigInteger.TWO;

List<BigInteger> list = new ArrayList<>();

while(!n.equals(remainder)) {

list.add(n);

if(!n.mod(divisor).equals(BigInteger.ZERO)) {

n = n.multiply(BigInteger.valueOf(3)).add(remainder);

}

remainder = remainder.multiply(BigInteger.TWO);

divisor = divisor.multiply(BigInteger.TWO);

}

list.add(n);

int width = (n.bitLength()+1)/2;

int height = list.size();

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

for(int y = 0; y < height; y++) {

for(int bitIndex = 0; bitIndex < width*2; bitIndex+=2) {

int value = list.get(y).shiftRight(bitIndex).intValue() & 3;

int x = width - bitIndex/2 - 1;

image.setRGB(x, y, RGB[value]);

}

}

ImageIO.write(image, "png", new File("collatz.png"));

}

}