Post Reply 
Micro-challenge: Special Event
12-27-2022, 08:28 PM (This post was last modified: 12-27-2022 08:29 PM by John Keith.)
Post: #19
RE: Micro-challenge: Special Event
(12-26-2022 02:00 PM)Thomas Klemm Wrote:  This example is from \(\pi\) Unleashed.
Quote:6.1 The spigot algorithm in detail

(…)

But occasionally we come across numbers in which this factor is not constant,
e.g. the numbers which correspond to the expression "2 weeks, 3 days, 4 hours and 5 minutes".
Because the ratio of weeks to days = 1 : 7, days to hours = 1 : 24 and hours to minutes = 1 : 60,
when converting this number to a decimal number (with the unit "weeks"),
three different factors have to be considered, namely 1/7, 1/24 and 1/60.
Hence, to answer the question how many weeks are in the example, we have to calculate the following:

\(
\begin{align}
2 + \frac{1}{7}\left(3 + \frac{1}{24}\left(4 + \frac{1}{60}\left(5\right)\right)\right)
\end{align}
\)


Example

Code:
b = [7, 24, 60]
d = [3, 4, 5]

spigot(d, b, 20)

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 …
Keep in mind that the initial digit 2 is omitted.

The expression in your example can be reduced to a single fraction, in this case 4945/2016, hence the repeating decimals. This is a natural fit for Gerald H's program ratio2decimal. Here is a simple program using similar inputs to your Python program. Usage: HP 49 or 50 in exact mode.
Level 3: the leading digit, 0 if there is none, in this case 2.
Level 2: a list of bases, e.g. {7 24 60}
Level 1: a list of "digits", e.g. {3 4 5}

Result: a string, "2.45287_698412_", where the underscores surround the repeating digits.

Code:

\<< DUP SIZE \-> n
  \<< 2.
    \<<                     @ Empty program for DOLIST interleaves lists.
    \>> DOLIST EVAL         @ Dump all terms onto stack.
    1. n
    START SWAP / +          @ Divide and sum over each pair of terms.
    NEXT EVAL RATIO2DECIMAL @ Convert to decimal with repeating digits if any.
  \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Micro-challenge: Special Event - EdS2 - 12-19-2022, 04:05 PM
RE: Micro-challenge: Special Event - johnb - 12-24-2022, 05:43 PM
RE: Micro-challenge: Special Event - EdS2 - 12-23-2022, 07:08 AM
RE: Micro-challenge: Special Event - johnb - 12-24-2022, 05:50 PM
RE: Micro-challenge: Special Event - John Keith - 12-27-2022 08:28 PM



User(s) browsing this thread: 1 Guest(s)