Odin SolutionsOdin Solutions

📉

Collatz Conjecture

Week 16, 2026

All Solutions

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) }