23
The 3Blue1Brown Youtube channel released a video a year ago called "Why do colliding blocks compute pi?" which describes a model where a block A
of mass \$a\$ slides into a block B
of mass \$b\$, which then pushes block B
into a wall, causing it to bounce off the wall and then collide again with block A
.
The miracle of this process is that if \$a/b = 10^{2n-2}\$ the number of total collisions (both between A
and B
and between B
with the wall) is given by the first \$n\$ digits of \$\pi\$.
Example output
+-------+---+--------+
| a | b | output |
+-------+---+--------+
| 1 | 1 | 3 |
| 2 | 1 | 5 |
| 3 | 1 | 5 |
| 4 | 1 | 6 |
| 5 | 1 | 7 |
| 10 | 3 | 6 |
| 7 | 2 | 6 |
| 9 | 2 | 7 |
| 1 | 2 | 3 |
| 1 | 5 | 2 |
| 100 | 1 | 31 |
| 10000 | 1 | 314 |
+-------+---+--------+
(These values were calculated using this web applet from Reddit user KyleCow1. Please let me know if I've made any mistakes.)
Challenge
Your challenge is to take two positive integers \$a, b \in \mathbb N_{>0}\$, and output the number of collisions in this scenario. Your program should be able to handle all \$a, b \leq 10\,000\$. This is a code-golf challenge, so the shortest program wins.
can we input b,a instead of a,b? – ngn – 2020-01-29T03:00:29.253
2..or a+ib as a complex number? – ngn – 2020-01-29T03:06:14.850
1Sure, either of these inputs is fine. – Peter Kagey – 2020-01-29T07:15:35.130
@ngn in what way would the complex number help? – RGS – 2020-01-29T07:51:10.183
3@RGS if your language has a concise way of getting the argument of a complex number (the "theta"), then arctg(b/a) could be theta(a+ib), but i'm not sure it would help much in this case, as b/a is under a sqrt – ngn – 2020-01-29T08:00:36.663
The case where
– Peter Kagey – 2020-02-04T22:00:55.343b=1
is now on the OEIS as A331859.