A small collection of code explorations of classic math and physics problems.
This repository gathers short Python scripts written while learning, each one poking at a well-known problem in number theory or cellular automata. The two topics covered so far are the Collatz conjecture (the 3n+1 problem) and Conway's Game of Life. The Collatz scripts print sequences and plot stopping times to look for patterns in the data.
The Collatz conjecture states that, starting from any positive integer, the rule "halve it if even, otherwise multiply by 3 and add 1" always eventually reaches 1. Two scripts explore this:
collatzconjecture.pyasks for a positive integer and prints its full Collatz trajectory: the complete sequence of values from the starting number down to 1.collatzconjecture2.pycomputes the stopping time (the number of steps to reach 1) for every integer from 1 up to a chosen limit, then draws a scatter plot of stopping time against starting integer and saves it to a file you name.
The saved plots in this repo show the familiar fan-shaped structure of Collatz
stopping times. The image below is collatz_plot.png, generated for starting
integers up to 1000:
The other PNGs (collatz_plot1.png through collatz_plot4.png) are the same
kind of scatter plot run over larger ranges (for example up to 10000).
The gameoflife/ directory holds work on Conway's Game of Life, a cellular
automaton on a grid of cells that are either alive or dead. Each step, every
cell's next state is decided by counting its eight neighbours: a live cell with
two or three live neighbours survives, a dead cell with exactly three live
neighbours becomes alive, and all other cells die or stay dead.
gameoflife/gameoflifetests2.pyis aunittestsuite for aConwaysGameclass. It exercises setting the world size, populating cells, counting live neighbours, advancing the grid onestep(), the well-known glider pattern, and a__str__rendering of the grid.gameoflife/gameoflife2.pyis the intended home for theConwaysGameimplementation. It is currently empty, so the tests describe the expected interface but will not pass until the class is written.gameoflife/README.mdhas notes on the simulation and its dependencies.
Clone the repository and run the scripts with Python 3:
git clone https://github.com/iteba15/Math-Physics-sets.git
cd Math-Physics-sets
# Print the Collatz trajectory of a single number
python collatzconjecture.py
# Plot stopping times up to a chosen limit and save the figure
python collatzconjecture2.pycollatzconjecture2.py imports matplotlib, so install it first:
pip install matplotlibThe Game of Life notes also mention numpy and pygame. The Collatz scripts
themselves use only the standard library plus matplotlib.
Exploratory hobby and learning code. The scripts run interactively from the command line and are kept simple on purpose. The Game of Life class is not yet implemented, so its tests are aspirational for now.
Allan Kiplagat Iteba (GitHub @iteba15), BSc Astrophysics & Space Science, University of Nairobi.
- LinkedIn: (link to be added)
- ResearchGate: (link to be added)
