All SolutionsAll Solutions

📉

Collatz Conjecture

Week 16, 2026

Iterate all values and store to RGB pixels | greenya | Odin Solutions

package main import "core:image" import "core:image/bmp" WIDTH, HEIGHT :: 1000, 1000 RESULT_BMP :: "result.bmp" main :: proc () { pixels := make([][3] u8, WIDTH*HEIGHT) defer delete(pixels) for i in 0..<WIDTH*HEIGHT { evens, odds, quotient := collatz(i + 1) pixels[i] = { u8(min(evens, 255)), u8(min(odds, 255)), u8(min(quotient, 255)), } } write_bmp(pixels, WIDTH, HEIGHT, RESULT_BMP) } collatz :: proc (start_n: int) -> (evens, odds, quotient: int) { n := start_n max_n := n for { if n % 2 == 0 { evens += 1 n /= 2 } else { odds += 1 n = 3*n + 1 max_n = max(max_n, n) } if n == 1 { odds += 1 break } } quotient = max_n / start_n return } write_bmp :: proc (pixels: [][3] u8, width, height: int, filename: string) { img, ok := image.pixels_to_image(pixels, width, height) assert(ok) err := bmp.save(filename, &img) assert(err == nil) }

Python - Pillow Lib + collatzConjecture function | BMC | Python Solutions

from PIL import Image def collatzConjecture(n:int, row:int, col:int) -> None: evenCount = 0 oddCount = 0 maxNum = n startNum = n while n > 1: if n > maxNum: maxNum = n if n % 2 == 0: evenCount += 1 n //= 2 else: oddCount += 1 n = n*3 + 1 oddCount += 1 # inc by 1 because of n maxNumQ = maxNum // startNum if startNum == 1: # special case because of 1 evenCount += 2 oddCount += 1 maxNumQ += 3 setPixelValue(evenCount, oddCount, maxNumQ, row, col) def setPixelValue( evenCount :int, oddCount :int, maxNumQ :int, row :int, col :int ) -> None: global ans r = evenCount if evenCount < 255 else 255 g = oddCount if oddCount < 255 else 255 b = maxNumQ if maxNumQ < 255 else 255 ans[row][col] = (r, g, b) def makeFinalImage(someArray: list) -> None: global ROWS, COLS flat_data = [pixel for row in someArray for pixel in row] img = Image.new('RGB', (COLS, ROWS)) img.putdata(flat_data) img.save('ans1.png') # -- main -- ROWS = 1000 COLS = 1000 n = 1 ans = [[(0,0,0) for col in range(COLS)] for row in range(ROWS)] for row in range(ROWS): for col in range(COLS): collatzConjecture(n, row, col) n += 1 if n % 1000 == 0: print(f"{n/(ROWS*COLS) * 100}% completed...") print(f"REACHED : {n}") print("\n -- PROCESS COMPLETED -- \n") makeFinalImage(ans) print("\n -- IMAGE MADE -- \n")

python | devilssnare | Python Solutions

from PIL import Image WIDTH = 1000 HEIGHT = 1000 LIMIT = WIDTH * HEIGHT img = Image.new("RGB", (WIDTH, HEIGHT)) pixels = img.load() def collatz_pixel(n: int): x = n even_count = 0 odd_count = 0 max_value = x if x == 1: seq = [1, 4, 2, 1] even_count = sum(1 for v in seq if v % 2 == 0) odd_count = sum(1 for v in seq if v % 2 == 1) max_value = max(seq) else: while True: if x % 2 == 0: even_count += 1 x //= 2 else: odd_count += 1 x = 3 * x + 1 if x != 1 else 1 if x > max_value: max_value = x if x == 1: odd_count += 1 break r = min(even_count, 255) g = min(odd_count, 255) b = min(max_value // n, 255) return (r, g, b) for n in range(1, LIMIT + 1): px = (n - 1) % WIDTH py = (n - 1) // WIDTH pixels[px, py] = collatz_pixel(n) img.save("collatz.png", "PNG") print("Saved image")

using "stb_image_write.h" to save RGB math | Draw6 | C++ Solutions

#include "allheaders.hpp" #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" using namespace std; void save_frame(int frame, uint8_t * data); // Saves image bool compare(int a, int b);//function to sort sequence vector (decending) int iRed, iGreen, iBlue, iSeq = 0;//iterators int iRow, iCol = 1;//height,width int iPixal = 0;//pixel number int iSeed = 1;//pixal manipulator vector<int> Sequence;//stores largest number in Sequence vector<int> vRed;//store iRed vector<int> vGreen;//store iGreen vector<int> vBlue;//store iBlue int main() { iRow = 0; iCol = 0; int b; for(int a = 1; a <= 1000; ++a) { ++iRow; iCol = 0; iRed, iGreen, iBlue = 0; for(int b = 1; b <= 1000; ++b) { iPixal++; iCol++; iRed, iGreen, iBlue = 0; iSeed = iPixal; iSeq = 0; while(iSeed > 1) { if((iSeed % 2) == 0) { Sequence.push_back(iSeed); ++iRed; iSeed /= 2; } else { Sequence.push_back(iSeed); ++iGreen; iSeed *= 3; ++iSeed; } sort(Sequence.begin(), Sequence.end(), compare); iSeq = Sequence[0]; iBlue = iSeq / iPixal; if(iSeed == 1) { iGreen += 1; } } if(iRed > 255){iRed = 255;} if(iGreen > 255){iGreen = 255;} if(iBlue > 255){iBlue = 255;} vRed.push_back(iRed); vGreen.push_back(iGreen); vBlue.push_back(iBlue); Sequence.clear(); iRed = 0; iGreen = 0; iBlue = 0; } } vRed[0] = 2; vGreen[0] = 2; vBlue[0] = 4; cout << '\007'; cout << "size of vRed: " << vRed.size() << "\n"; uint8_t image_data[1000 * 1000 * 3]; iPixal = 0; for (int y = 0; y < 1000; y++) { for (int x = 0; x < 1000; x++) { iPixal++; image_data[3 * (y * 1000 + x)] = vRed[iPixal] ; image_data[3 * (y * 1000 + x) + 1] = vGreen[iPixal] ; image_data[3 * (y * 1000 + x) + 2] = vBlue[iPixal]; } } save_frame(1, image_data); return 0; } bool compare(int a, int b)//sort for largest number in Sequence (decending) { return a > b; } void save_frame(int frame, uint8_t * data) // Saves image to file { string fileZ = "week16result.png"; stbi_write_png(fileZ.c_str(), 1000, 1000, 3, data, 1000 * 3); return; }