[Question]
  [
The 3Blue1Brown Youtube channel released a video a year ago called "[Why do colliding blocks compute pi?](https://www.youtube.com/watch?v=jsYwFizhncE)" 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](https://editor.p5js.org/KyleCow/full/SJbg0W6z4) from Reddit user [KyleCow1](https://www.reddit.com/r/3Blue1Brown/comments/agpnl3/i_made_the_collisions_demo_in_p5_js/). 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](/questions/tagged/code-golf "show questions tagged 'code-golf'") challenge, so the shortest program wins.
  
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~18~~ 16 bytes
```
¯1+⌈○÷¯3○.5*⍨⎕÷⎕
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v8/7VHbhOpD6w21H/V0PJrefXj7ofXGQFrPVOtR74pHvVsPb3/Uu6v2//8QkDog/1HnoiIg81FXc5o@UMZKPS0xM0cdyALKFT3qblHPz1av5TJUAAFDhRAFYy4jONuUyxiJbQJnm3GZwtnmXIYGILYxWNwcLG4EZlvC2eZQ843A5kPYpkC2EVCvAcxeQxAHyIVwTAA "APL (Dyalog Classic) – Try It Online")
rendered as the equivalent `{¯1+⌈○÷¯3○.5*⍨⍵÷⍺}` in the tio link, to facilitate testing; `⎕` means evaluated input; `⍺` and `⍵` are the arguments to an anonymous function
$$\left\lceil\frac\pi{\textrm{arctg}\sqrt\frac ba}\right\rceil-1$$
(that's the formula from [the video](https://www.youtube.com/watch?v=jsYwFizhncE). i recommend watching it. it's well explained and the animations are great for building intuition.)
[Answer]
# Jelly, ~~10~~ 9 bytes
My first Jelly submission :')
```
÷½ÆṬØP÷Ċ’
```
*-1 byte* thanks to Mr.Xcoder
Uses the formula as in the video. Receives the input flipped; OP gave permission. It probably has room for further golfing, so be sure to give me feedback!
```
÷½        divide b by a and take square root
  ÆṬ      take the ArcTan of that; then
      ÷   divide
    ØP    pi
          by the number we had.
       Ċ  Round up
        ’ and subtract one
```
[Try it online](https://tio.run/##y0rNyan8///w9kN7D7c93Lnm8IyAw9uPdD1qmPn///9oQx0FZGSso2AEQ6YQwViQIiMdYx0THVMdQwMdcx1LHUMQNDAAYQODWAA)
[Answer]
# JavaScript (ES7), ~~41~~ 39 bytes
Takes input as `(a)(b)`.
```
a=>b=>3.14159265/Math.atan((b/a)**.5)|0
```
[Try it online!](https://tio.run/##fdBLDoIwFAXQuavosCWx/0IYwA5cxAPBT0hrpHHk3quRCf3EOz25Ly/3Di9Yx@ft4Y/WnacwdwG6fuh6RYUWppW1YSfwVwoeLMYDA1JV1JA3D6Ozq1smurgLnrFA3xAsCEGMIXWIVUZqElV/VUdaJ2oibRIV/Keq3G22rixrG2l2OVJVVrOpzL7i@61EzpzvWIcP "JavaScript (Node.js) – Try It Online")
### How?
Instead of using a *ceil* function and subtracting \$1\$, we deliberately use a slightly underestimated approximation of \$\pi\$ and *floor* the result with a bitwise OR.
For \$a,b\le 10000\$, it was empirically proven to give the same results as this safer 44-byte version:
```
with(Math)f=a=>b=>ceil(PI/atan((b/a)**.5))-1
```
[Try it online!](https://tio.run/##fdBBDoIwEAXQvadgOUMCbSmVuIC9CxOvMCAIhFAjjR6/GtnQlvi3L38y@SO9aGmew8Mks7611r4H08OFTI9dSWVVl1XTDhNcz4wMzQA1I4zjVCEmwjZ6XvTUppO@Qwci@gZBIEaMRfLgauao8lT@1dzRo6fK0cJTwX8q97vF2s329eRocNlRua9q1Sz4im@3EiFzvuHcfgA "JavaScript (Node.js) – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 32 bytes
```
a#b=ceiling(pi/atan(sqrt$b/a))-1
```
[Try it online!](https://tio.run/##bdBRC8IgEAfw932Kg/ag4NicW6MH@yLVwy2kJGdr2uc3cRRMEl/8ecefuzu6hzImBNyN8qq00fZGZl2jR0vca/HlWCOlFQ9eOe9AwolwBvEKygr4HtIm6zcm/liXbL@xPtmwMd7EhLxwYNDmdkiWNScTucWcNgtp1lF4zr@Pjl6KYkJt4@Tzoq2HEtAYIGeCDEYGz7enUB0h7g@kXJ9pVeED "Haskell – Try It Online")
[Answer]
# Mathematica, ~~30~~ 29 bytes, 25 characters
```
⌈Pi/ArcTan@Sqrt[#2/#]⌉-1&
```
[Try it online](https://tio.run/##y00syUjNTSzJTE78n2b7/1FPR0CmvmNRckhinkNwYVFJtLKRvrJh7KOeTl1Dtf9p0YY6hrEK@voKAUWZeSVcadFGaHxjNL4JGt8UjW9ooGOMImCuY4TCt0TjG2LwTdEMNMCwwgBV7D8A)
*-1 byte* thanks to ExpiredData
Uses the formula explained in the video.
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~49~~ 43 bytes
```
->a,b{(-1.arg/((a*b)**0.5+b.i).arg).ceil-1}
```
[Try it online!](https://tio.run/##Tc7BjoIwFAXQvV9xUzaApVIQzZigq1nNwoQtYdEizpiY0aAsDPTbmUeJzSRNm3N7@9K206/xnI/RXnHd@5EUqv1e@b4KdRCGsciWWlyCKQxE3VyukTRjuQBKyUErrfiExCKbkf7H2mIzI7PYzpAxPXdXW47E4cPi3bNIHWhI8p4Qz5@Qzi5ZU1SJRtU/ON0wKA7NceueAxVb5DiXU1SR7t3zAeb1lOZTAwew4xfDDuyzKI4FMxjg9cpw2rVBvqezNfC9ntomYIvm9zT@AQ "Ruby – Try It Online")
-5 thans to G B, nicely done
-1 replacing `1i*b` with `b.i`
[Answer]
# Excel, 35 ~~37~~ bytes
-2 bytes thanks to @Chronocidal
```
=CEILING(PI()/(ATAN((B1/A1)^.5)),1)-1
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~63~~ \$\cdots\$ ~~47~~ 46 bytes
```
f(a,b){a=ceil(acos(-1)/atan(sqrt(b*1./a)))-1;}
```
[Try it online!](https://tio.run/##bVFta4MwEP7eX3EIQmLj1Fo7inNfxn7FlBFj7GRWOxOYTPzrc/EVLQuX43LPPU8uF2ZeGOu6FFES44YGjGc5oqwUyHSwRSUtkPiqJIoN58GiGGPT8dsuKyRcaVYgDM0O1OoTkgv5Tt8iCKBxCBwIuASOBDwCjk3gkcBZRaPZ9uBsu/W3/Hjhr80d5EbzxuSKyOsbZ5InI9UdSpSdhjtPczBquL2ac5zYrCyEBPZBK0N5zj55NYpoYf16COvzi9qeRmB9drWJnZYVoL6BrEh4rWi2P4VPILIfXqZobg1bU8JYMj7s90P1PMP5OVQpTbMc8MjfwPEMx//CQsH9b0KMtwBXwDKpe@atUiUp0vSEgJ6A@dx7XYSFenuvRUCQZUIiCHg0qbe7tvtlaU4vojO/OzO//gE "C (gcc) – Try It Online")
*Saved ~~11~~ 12 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)!!!*
Uses the formula from the video as [ngn](https://codegolf.stackexchange.com/users/24908/ngn) recommended.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 30 bytes
```
/t©ažq;‚Δ{ÐO;Å¼®‹èsO;‚}нžqs/î<
```
[Try it online!](https://tio.run/##yy9OTMpM/f9fv@TQysSj@wqtHzXMOjel@vAEf@vDrYf2HFr3qGHn4RXF/iDx2gt7gSqK9Q@vs/n/39AACLgMAQ "05AB1E – Try It Online")
Almost definitely golfable..
Implements the formula but because 05AB1E doesn't have arctan we have to calculate it, this does it by bisection.
---
# Explanation
```
/t                                - sqrt(a/b)
  ©                               - store this value
   ažq;‚                          - the array [0, pi/2] 
        Δ                         - repeat until the array passed in does not change
         {Ð                       - sort then triplicate array 
           O;Å¼                   - arctan of the average of the array (bisection)
               ®‹                 - is this greater than the sqrt(a/b)? 0 if so 1 if not
                 èsO;‚            - Take the index from the current two guesses and combine with their average
                      }н          - Stop looping and get the first value
                                  - (this should be close enough to arctan(sqrt(a/b))
                        žqs/      - push pi then divide it by the atan(sqrt(a/b)) guess
                            î<    - ceil and decrement
```
[Answer]
# [Rust](https://www.rust-lang.org/), 59 bytes
```
|a:f64,b:f64|(f64::acos(-1.)/(b/a).sqrt().atan()).ceil()-1.
```
[Try it online!](https://tio.run/##hZBNboMwEEb3OcUXFpEtuRB@kqignKCLHgChykRGQqKmwc6KcHZqOxRCNrWsGfvNzJPl7qb0WEl881oSin4DsxqhUeE83nlaHRNW2ngnJqQpv7SKvIU@DUgZcOqra6cJ9bnmZpz6F1E3hJr6aD3ZZtZpobTCGbkjQB4ymB0X7A9EDhwWEL@CxIHjAg4OnBYQ7o1y1XJiiFbg3YHnGQfiFTDi6Nm6fzw2XLGZJhMuMpdcqNoOu5wzlAztTReoJXaPT@gnB8CVEp3@EtctqQg3V9jvRjmdqBudsddK8BT9YDps8hisn2azLwhelLbsJP9NDy7@dLXUjdwS7/PDM5Vh/AU "Rust – Try It Online")
[Answer]
# J, 19 bytes
```
_1+1p1>.@%_3 o.2%:%
```
Try in Jconsole as the TIO seems to be quite weird.
Usage: `f=:_1+1p1>.@%_3 o.2%:%`
# Example
`f 10000` => `314`
[Answer]
# [Python 3](https://docs.python.org/3/), 55 bytes
```
lambda a,b:ceil(pi/atan((b/a)**.5)-1)
from math import*
```
[Try it online!](https://tio.run/##fdDfCoIwFAbwe5/iQDebrHSuJQk9iXkxK3Hg/jB209MvjaSp1Ln98X18HPv0vdEsdJdrGIRq7wIEaavbQw7Iykx4oRFqM4HT9MDxnuKkc0aBEr4HqaxxPg2dcVMIpIa6poQ2BN63A5ZAdHURG18a@2PH2E5L47GVS6M5YR/c5EpS/Ow8x7bujI2tjX@tWG/J56Vjjm5w5gmPozYVWCe1Rx0af4txeAE "Python 3 – Try It Online")
[Answer]
# AWK, ~~56~~ 55 bytes
```
{x=atan2(0,-1)/atan2(($2/$1)^.5,1);$0=int(x);$0-=$0~x}1
```
[Try it online!](https://tio.run/##JclBCoAgFATQvadw4UJB839NIsKrBC4jcCUkRF3dvrZ4zDCTrrO1u8ZUUnYStEFl/y6FswLVPgWNahMQj1xk7c1EAW99sDXkyBzxZCaBIHDPFu7YSnAINMK4oOcH "AWK – Try It Online")
I know I can save a few bytes by hardcoding Pi to some arbitrary precision, but I'm not sure how accurate it is outside of the provided test cases.
[Answer]
# [PHP](https://php.net/), 46 bytes
```
<?=ceil(pi()/atan(($argv[2]/$argv[1])**.5))-1;
```
[Try it online!](https://tio.run/##hdLNCoMwDADgu08RZIdWpm2qRZz7eYQ9wPQgIlMQJ875@p1O2GEj9hYSviSE9HVvzPFyKqumZX3DuCjGomNsVwz36aZysQaYc88LNOc@pqYq6we4WZd117GuBpiK9lU9D0vCTR1nLeMeABD8M7gBg9/2KPDbEHjgfuQKlQUqCoYWGFIwssCIgtoCNQVRzjKkYChQUjJeRipKKhFTMLHAhNzVAnETagrqDSjn82y9jvw/jzFzVkqDbw "PHP – Try It Online")
Not very original I guess, applying the formula from the video..
]