A script written in C that does a non-graphical simulation of the phenomena showcased in this 3Blue1Brown short and explained in Why Do Colliding Blocks Compute Pi?.
Description of the problem: A small block is at rest on a plane with a vertical wall on the left. A block N times in mass is sliding from the right to the left with some speed, the maximum number of elastic collisions that will ever happen for a scenario will be PI * sqrt(N) and will thus show the digits of PI if N is 100 to some power.
To use compile the main.c
file (gcc main.c
). You can run the a.out
executeable with the following options.
./a.out -h
will display a help message with pretty detailed insturctions.
./a.out -n 100 -e -m -v -p
will display all information after every collisions, including nice pictures/diagrams of the blocks direction of motion.
Output:
# Transcribed Input Command
Masses: 1.0000 100.0000
Initial Velocities: 0.0000 -1.0000
Display options:
Energy
Momentum
Velocity
Pictures
For help use -h option
# Collision States:
Collision: 0
0.000000 -1.000000
| # <---#
Momentum: Small mass: 0.0000 Big mass: -100.0000 Total: -100.000000
Energy: Small mass: 0.0000 Big mass: 0.5000 Total: 0.500000
Collision: 1
-1.980198 -0.980198
| <--# <#
Momentum: Small mass: -1.9802 Big mass: -98.0198 Total: -100.000008
Energy: Small mass: 1.9606 Big mass: 0.4804 Total: 2.440986
Collision: 2
1.980198 -0.980198
| #--> <#
Momentum: Small mass: 1.9802 Big mass: -98.0198 Total: -96.039604
Energy: Small mass: 1.9606 Big mass: 0.4804 Total: 2.440986
Collision: 3
-3.881972 -0.921576
| <--# <#
Momentum: Small mass: -3.8820 Big mass: -92.1576 Total: -96.039597
Energy: Small mass: 7.5349 Big mass: 0.4247 Total: 7.959506
...
Collision: 30
1.509540 0.988541
| #-> #->
Momentum: Small mass: 1.5095 Big mass: 98.8541 Total: 100.363617
Energy: Small mass: 1.1394 Big mass: 0.4886 Total: 1.627962
# Final State:
Collision: 0
0.477858 0.998858
| #> #-->
Momentum: Small mass: 0.4779 Big mass: 99.8858 Total: 100.363625
Energy: Small mass: 0.1142 Big mass: 0.4989 Total: 0.613033
# Total Collision Count:
31
Feel free to play around. For no particular reason all values are C floats so menage your expectations for overflows etc.