Micro-challenge: Special Event
|
12-26-2022, 02:00 PM
(This post was last modified: 12-26-2022 02:03 PM by Thomas Klemm.)
Post: #17
|
|||
|
|||
RE: Micro-challenge: Special Event
(12-24-2022 07:20 PM)Albert Chan Wrote: BTW, radix can be non-integers too. This example is from \(\pi\) Unleashed. Quote:6.1 The spigot algorithm in detail The Spigot algorithm makes it possible to calculate the base-10 representation digit by digit. In each step, the "digits" are multiplied by 10 and divided by their basis. Quote:On every division, the remainder is retained and the integer quotient is carried over to the next decimal place. Here's a Python program that does this: Code: def spigot(d, b, n): Example Code: b = [7, 24, 60] Code: [4, 5, 2, 8, 7, 6, 9, 8, 4, 1, 2, 6, 9, 8, 4, 1, 2, 6, 9, 8] Compare this to the calculated value: Code: 2.45287 698412 698412 69841 … We can list the intermediate results and see how the calculation becomes periodic: Code: [3, 16, 50] Calculating Digits of \(\pi\) Here's the interesting relation to \(\pi\): \( \begin{align} \pi = 2 + \frac{1}{3}\left(2 + \frac{2}{5}\left(2 + \frac{3}{7}\left(2 + \cdots \right)\right)\right) \end{align} \) Thus we can write in this specific basis: \(\pi = 2.2222\cdots\). We can use the same function to calculate \(\pi\) digit by digit: Code: from mpmath import mp Code: [11, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6] Imperial Units There are many ways to use this algorithm when it comes to converting imperial units: Warning: I've skipped over some details. So I recommend using a proper implementation instead of my Python program if you want to calculate more digits of \(\pi\). |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)