📉
Collatz Conjecture
Week 16
From Wikipedia: "The Collatz conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1. It concerns sequences of integers in which each term is obtained from the previous term as follows: if a term is even, the next term is one half of it. If a term is odd, the next term is 3 times the previous term plus 1. The conjecture is that these sequences always reach 1, no matter which positive integer is chosen to start the sequence. The conjecture has been shown to hold for all positive integers up to 2.36 x 1021, but no general proof has been found."
Simply:
- Start from some number, n
- If n is even, half it
- If n is odd, multiply it by 3 and add 1
- Repeat until you get to 1 (or find a number that does not converge to 1 - of which none have ever been found)
Your task is to create an image with resolution 1000x1000 representing numbers 1 to 1,000,000 - the first row will represent values of n 1-1000, the next row 1001 to 2000 and so on
Pixel components should be coloured according to the following rules:
- red: a count of the even numbers in the sequence
- green: a count of the odd numbers in the sequence
- blue: the quotient of the maximum number in the sequence divided by the starting number
Note that a pixel value should be capped at 255 if it exceeds it
Note: it is also important your program outputs a lossless image format like .png - something like a jpg will be compressed and pixel values may not be the original ones you generated
For example, if we start with a value of n = 5, then we get the following series:
The RGB pixel values would hence be (4, 2, 3)
- red: 4 - there are 4 even numbers in this sequence
- green: 2 - there's only 2 odd numbers
- blue: 3 - since the quotient of dividing the biggest number (16) by the starting number (5) is 3
An example 10x10 image can be seen below (i.e. values of n from 1 to 100) or by clicking here
Click to upload or drag & drop image here...
Hints
Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts
| Release Day | Hint |
|---|---|
| 2 | You can either do this with nested loops both from 1-1000 or with a loop 1 to 1000 * 1000, then use mod and div operations to calculate the row and column |
| 3 | A simple pre or post-conditional loop to keep looping until you get to 1 and updating the even/odd counts and max number as you go - you can use these values to then calculate the rgb values |
| 4 | As of Jan 2025, all numbers up to approximately 2 ^ 71 have been tested and found to abide by the Collatz Conjecture - we therefore know every number in the range we are testing will converge to 1, hence that's the only case we have to handle |
| 5 | If we were testing more, the way to determine if a number breaks the conjecture is if a number is repeated in a sequence - i.e. causing a loop. e.g. suppose our sequence goes: x y z x - since we have seen 'x' in our sequence already, then we know we have entered an infinite loop and the next numbers will be y and z respectively, since from any number, there is only one sequence the following numbers can be |