Coding Challenge #139: Calculating Digits of Pi with Collisions

The Coding Train
14 Mar 201931:41
EducationalLearning
32 Likes 10 Comments

TLDRIn this coding challenge, the creator reminisces about a 2019 video from 3Blue1Brown that demonstrated how two bouncing blocks could produce the digits of pi. Inspired, the creator attempts to replicate this phenomenon using JavaScript and object-oriented programming within the p5 Web Editor. The video documents the process of setting up blocks, implementing collision detection, and applying physics for elastic collisions. It explores the limitations of Euler integration and attempts to improve accuracy by increasing the number of time steps in the simulation. The result is a visually engaging and educational journey that successfully visualizes pi's digits through code.

Takeaways
  • πŸ˜€ The video script recalls January 2019 and the release of a video by 3Blue1Brown about a counting puzzle involving blocks bouncing and producing the digits of pi.
  • πŸ“š The video was educational, teaching math concepts through the unexpected appearance of pi in a physical collision scenario.
  • πŸŽ₯ 3Blue1Brown is a favored YouTube channel of the speaker for learning math and drawing inspiration.
  • πŸ€– The speaker attempts to recreate the 3Blue1Brown experiment using coding, specifically JavaScript, to simulate the collision of two blocks and generate digits of pi.
  • πŸ“ The script discusses the physics of elastic collisions, including the conservation of momentum and kinetic energy, which are key to understanding the block collisions.
  • πŸ‘Ύ The coding process involves creating a Block class, setting velocities, and writing functions to check for collisions and bounce the blocks.
  • πŸ”’ The script explores the concept of Euler integration and its limitations in accurately simulating physical movements over time.
  • 🎡 The video includes the addition of sound effects to enhance the visual simulation of the blocks 'clacking' upon collision.
  • πŸš€ The speaker experiments with different techniques, such as increasing the number of time steps in the simulation to improve accuracy.
  • πŸ“ˆ The script highlights the challenge of achieving high precision in a JavaScript simulation, especially when attempting to calculate many digits of pi.
  • πŸŽ‰ The video concludes with the successful demonstration of calculating 11 digits of pi using the simulation, showcasing the power of creative coding and math.
Q & A
  • What was the main topic of the video about?

    -The main topic of the video was demonstrating how the digits of pi can appear from the number of times two blocks collide in a simulation, inspired by a video from the 3Blue1Brown YouTube channel.

  • What is the name of the YouTube channel that inspired the video?

    -The YouTube channel that inspired the video is called 3Blue1Brown.

  • What concept from physics does the video touch upon?

    -The video touches upon the concept of perfectly elastic collisions in physics.

  • What is the significance of the number pi in the video?

    -The number pi is significant because the number of times the blocks collide in the simulation results in the digits of pi, showcasing a surprising connection between a mathematical constant and a physical phenomenon.

  • What is the role of Euler integration in the video?

    -Euler integration is used in the video to simulate the motion of the blocks over time, but it is also discussed as a limitation due to its inaccuracy with larger time steps.

  • What is the issue with using Euler integration for the simulation?

    -The issue with Euler integration in the simulation is that it can become inaccurate when using large time steps, leading to a less realistic representation of the physical phenomenon.

  • What is the 'boilerplate code' mentioned in the script?

    -The 'boilerplate code' refers to the basic, reusable code that the creator uses in many of their examples to save time and avoid retyping common functionalities.

  • What does the video creator attempt to do in the coding challenge?

    -The video creator attempts to code their own version of the 3Blue1Brown's collision simulation in JavaScript, aiming to replicate the appearance of pi's digits from the collisions.

  • What is the role of the 'Block' class in the code?

    -The 'Block' class is used to define the properties and behaviors of the blocks in the simulation, including their position, size, velocity, and methods for updating their state and handling collisions.

  • How does the video address the problem of accuracy in the simulation?

    -The video addresses the problem of accuracy by increasing the number of time steps in the simulation, which allows for smaller increments in time and a more precise representation of the blocks' motion.

  • What is the significance of the paper 'Playing Pool With Pi' mentioned in the video?

    -The paper 'Playing Pool With Pi' is significant as it explores the concept of pi appearing from billiard ball collisions, providing a theoretical foundation for the phenomenon demonstrated in the video.

  • How does the video creator handle the collision with a wall?

    -The video creator handles the collision with a wall by implementing a function that reverses the velocity of the block upon collision, simulating a perfectly elastic collision with an immovable object of infinite mass.

  • What is the final outcome of the coding challenge in the video?

    -The final outcome of the coding challenge is successfully simulating the appearance of the first 11 digits of pi through the collisions of two blocks in a browser using JavaScript.

Outlines
00:00
πŸ“š Reminiscing January 2019's Mathematical Marvel

The speaker fondly recalls January 2019 and a captivating video from the educational YouTube channel 3Blue1Brown that explained a unique counting puzzle involving two bouncing blocks. The video demonstrated how the number of times the blocks collided could reveal digits of pi, an intriguing concept that combined physics and mathematics. The speaker also references a 2003 paper by G. Galperin and a Numberphile video for further exploration of the phenomenon. The channel's focus on coding is then introduced, with the speaker's intention to code a simulation of the 3Blue1Brown video in JavaScript, aiming to replicate the appearance of pi through collisions.

05:02
🎯 Setting Up the Coding Challenge

The speaker begins the coding challenge by establishing a basic object-oriented system in JavaScript, defining a Block class with properties for position, size, and velocity. The initial setup includes drawing the blocks and loading a sound effect. The speaker then proceeds to write functions for updating block positions based on velocity and checks for collisions between the two blocks. A 'collide' function is implemented to determine if the blocks intersect, crucial for the simulation's core mechanics.

10:04
πŸš€ Implementing Elastic Collision Dynamics

The coding challenge continues with the implementation of elastic collision physics between the two blocks. The speaker introduces the concept of mass and uses formulas from physics to calculate new velocities after a collision, ensuring the conservation of momentum and kinetic energy. The 'bounce' function is developed to update the velocities of the blocks based on their masses and initial velocities. The speaker also discusses the implications of equal versus unequal masses in the collision outcome.

15:05
πŸ—οΈ Adding Realism with Wall Collisions and Sound Effects

The simulation becomes more realistic as the speaker adds a 'hitWall' function to simulate collisions with an immovable object, treating it as an object with infinite mass. The block's velocity is reversed upon hitting the wall, mimicking a perfectly elastic collision. Additionally, sound effects are integrated to enhance the auditory experience of the simulation, with the clacking sound playing upon collisions.

20:06
πŸ”’ Counting Collisions to Reveal Pi

The speaker introduces a counting mechanism to tally the number of collisions, aiming to observe the digits of pi emerge from the simulation. The count is displayed on the screen, and the speaker experiments with adjusting the mass of the blocks to influence the count's progression. The speaker also contemplates the stopping criteria for the count and the limitations of Euler integration in the simulation's accuracy.

25:09
πŸ› οΈ Refining the Simulation with Smaller Time Steps

To improve the simulation's accuracy, the speaker implements a technique using smaller time steps, effectively increasing the number of updates per frame to more closely approximate continuous time. This approach helps in creating a more precise simulation, allowing for a better representation of the physical behavior of the blocks. The speaker also discusses the trade-off between accuracy and performance, especially as the number of time steps increases.

30:10
🎢 Achieving High-Precision Pi Digits with Sound Integration

The speaker successfully generates multiple digits of pi through the simulation and contemplates the integration of sound effects that play upon collisions. The challenge of managing the sound playback within the increased number of time steps is addressed, ensuring that the clacking sound enhances rather than distracts from the simulation. The speaker demonstrates achieving up to seven digits of pi with a high number of time steps, showcasing the effectiveness of the refined simulation.

πŸŽ‰ Pushing the Limits to Ten Million Time Steps

In an ambitious attempt, the speaker pushes the simulation to process up to ten million time steps per draw, resulting in a significant increase in computational demand but also in the accuracy of the simulation. The speaker successfully achieves 11 digits of pi through this method, demonstrating the potential of the approach despite the computational intensity and slowdown in the animation's frame rate.

🎈 Reflecting on the Coding Challenge and Future Improvements

The speaker concludes the coding challenge by reflecting on the process and outcomes, acknowledging the successful generation of 11 digits of pi through elastic collisions. They also consider alternative methods and optimizations for the simulation, such as using BigDecimal for higher precision or exploring different integration techniques. The speaker invites the audience to share their thoughts and solutions on thecodingtrain.com and hints at potential future live streams and coding attempts.

πŸŽ‰ Celebrating Pi Day with a Creative Coding Challenge

The speaker ends the video with a light-hearted celebration of Pi Day, expressing excitement about the successful coding challenge and the creative approach to generating digits of pi. They humorously mention Tau Day and the arrival of summer, signing off with a cheerful tune that encapsulates the joy of mathematical discovery and the fun of coding.

Mindmap
Keywords
πŸ’‘January 2019
January 2019 is a temporal reference point mentioned in the script, indicating a simpler time before the complexities of the present. It sets the stage for the introduction of a significant event or phenomenon that occurred during this period. In the script, it is used to evoke nostalgia and to lead into the discussion of a video released in that month.
πŸ’‘3Blue1Brown
3Blue1Brown is a popular YouTube channel known for its educational content, particularly in the field of mathematics. The channel is mentioned as a source of inspiration and learning for the speaker. It is central to the video's theme as it is the creator of the video 'The most unexpected answer to a counting puzzle' that the speaker discusses.
πŸ’‘Elastic Collision
An elastic collision is a type of collision in physics where both momentum and kinetic energy are conserved. In the script, the concept is applied to a simulation of two blocks colliding and bouncing off each other. The speaker uses the formula for elastic collision to calculate the new velocities of the blocks after the collision, which is a key part of the coding challenge presented in the video.
πŸ’‘Pi (Ο€)
Pi, represented by the Greek letter Ο€, is a mathematical constant that represents the ratio of a circle's circumference to its diameter. In the video, the digits of pi appear as a result of the number of times two blocks collide with each other, which is a fascinating mathematical phenomenon that the speaker aims to replicate through coding.
πŸ’‘Coding Challenge
A coding challenge is a task or problem that requires writing computer code to solve. In the script, the speaker embarks on a coding challenge to recreate the phenomenon shown in the 3Blue1Brown video using JavaScript and browser technology. The challenge involves simulating the collision of two blocks and observing the digits of pi emerge from the count of their collisions.
πŸ’‘Object-Oriented System
An object-oriented system is a programming paradigm based on the concept of 'objects', which can contain data in the form of fields and code in the form of methods. In the script, the speaker uses an object-oriented approach to create a Block class with properties like position, size, and velocity, which is essential for the simulation of the blocks' movement and collision.
πŸ’‘Euler Integration
Euler integration is a simple method for numerically solving ordinary differential equations by approximating the value of a function over small time steps. In the video, the speaker discusses the limitations of Euler integration in accurately simulating the physics of the block collisions and explores ways to improve the simulation's accuracy.
πŸ’‘Collision Detection
Collision detection is the process of detecting when two objects in a simulation have come into contact with each other. In the script, the speaker writes a function to detect collisions between the two blocks, which is crucial for the simulation to know when to apply the elastic collision formula and update the blocks' velocities.
πŸ’‘Optics
Optics is the branch of physics that studies the behavior and properties of light, including its reflection and refraction. In the script, the speaker mentions a connection made by 3Blue1Brown between the optics and the phenomenon of counting collisions to derive the digits of pi, indicating a deeper, more complex relationship between these seemingly unrelated fields.
πŸ’‘JavaScript
JavaScript is a high-level, interpreted programming language commonly used for enhancing web pages with interactive elements. In the video, the speaker uses JavaScript to code the simulation of the block collisions in a web browser, demonstrating how the language can be used for more than just simple web interactions.
πŸ’‘p5.js
p5.js is a JavaScript library that simplifies the process of creating graphics and interactive experiences in web browsers. The speaker mentions using p5.js in the script, indicating that the coding challenge is likely being conducted within this library's framework, which provides a more accessible way to handle graphics and animations.
πŸ’‘Floating Point Numbers
Floating point numbers are a computer representation of real numbers, allowing for the storage of decimal values. In the script, the speaker discusses the limitations of floating point numbers in JavaScript, which have a finite precision and can lead to inaccuracies in calculations, especially when dealing with high-precision values like the digits of pi.
πŸ’‘Time Steps
Time steps refer to the individual increments of time used in a simulation to update the state of the system. The speaker in the script uses the concept of time steps to improve the accuracy of the simulation by making smaller, more frequent updates to the positions and velocities of the blocks, allowing for a more precise calculation of collisions.
Highlights

In January 2019, a video from 3Blue1Brown on a counting puzzle involving blocks and pi was released.

The video demonstrated an unexpected connection between the number of times two blocks clacked and the digits of pi.

The concept of the video was not new, with a 2003 paper by G. Galperin titled 'Playing Pool With Pi' discussing a similar phenomenon.

The speaker plans to code a simulation of the 3Blue1Brown video in JavaScript to explore the pi digits through collisions.

A basic object-oriented system is established with a Block class and properties for position, size, and velocity.

A collision detection function is implemented to determine when two blocks intersect.

The bounce function is created to simulate perfectly elastic collisions between blocks with different masses.

A hitWall function is introduced to handle collisions with an immovable object, simulating a wall.

The speaker attempts to count the number of collisions to generate digits of pi, facing challenges with Euler integration.

To improve accuracy, smaller time steps are introduced in the simulation, reducing the inaccuracies of Euler integration.

The speaker successfully generates multiple digits of pi using the simulation, highlighting the power of small time steps.

The use of sound in the simulation adds to the user experience, with the clacking sound playing upon collisions.

The speaker discusses the limitations of Euler integration and considers alternative integration techniques for more accurate simulations.

The speaker successfully achieves 10 digits of pi through the simulation, demonstrating the potential of the method.

The video concludes with the speaker reflecting on the challenge and inviting the audience to share their thoughts on optimizing the simulation.

Links to related resources, including the 3Blue1Brown video series and other attempts at the challenge, are provided for further exploration.

The speaker humorously acknowledges the upcoming Tau Day and the passing of time, ending the video on a light-hearted note.

Transcripts
Rate This

5.0 / 5 (0 votes)

Thanks for rating: