4,494
edits
EllisMorgan (talk | contribs) (A traditional function solution to the problem of Shirley's weight discussed at the BAA Webinar on 20th.September 2020) |
No edit summary |
||
Line 1: | Line 1: | ||
At the BAA webinar on 20th | At the [[BAA webinar]] on 20th September 2020, [[Ray Polivka]] described a programming test set in the 1950s in the course "Programs1" in the [[wikipedia:ILLIAC|ILLIAC]] work at [[wikipedia:Illinois University|Illinois University]]: | ||
The webinar participants discussed this for quite a while at the end of which Curtis Jones said | {{Quote|Shirley's weight at birth was X tons stored in location A, For the first 20 weeks her weight increase linearly by 8 oz per week. After that the rate of growth declines by a constant factor, held in location B, every four weeks. What is Shirley's weight at 16 weeks and at 52 weeks?}} | ||
Polivka observed that since the pupils were writing [[wikipedia:binary code|binary code]] with a handful of [[wikipedia:Hardware register|register]]s and only 1 Kb of memory, this was quite a stiff problem to encounter on your first course. | |||
The webinar participants discussed this for quite a while at the end of which Curtis Jones said [[Adám Brudzewsky]]'s approach was a good example of APL as a [https://www.jsoftware.com/papers/tot.htm tool of thought]. Ellis Morgan commented that the problem is effectively a [[sum]] of a [[wikipedia:geometric series|geometric series]] here. The function <source lang=apl inline>Shirley</source> is a [[traditional function]] which one might have [[APL\360|written in 1970]] (except for the [[Mix]] in the definition of <source lang=apl inline>wt</source>): | |||
<source lang=apl> | |||
wt←xb | wt←xb Shirley w;a;b;c;k;m;n;r | ||
⍝ weight of Shirley in ounces after "w" weeks ("w" may be a vector or whatever) | ⍝ weight of Shirley in ounces after "w" weeks ("w" may be a vector or whatever) | ||
a b←(16×2240)1×2↑xb ⍝ birth weight in ounces, monthly growth decay factor | a b←(16×2240)1×2↑xb ⍝ birth weight in ounces, monthly growth decay factor | ||
Line 15: | Line 15: | ||
r←b*n ⍝ growth factor to apply in last complete month | r←b*n ⍝ growth factor to apply in last complete month | ||
k←32 ⍝ growth per month in ounces for the first 20 weeks | k←32 ⍝ growth per month in ounces for the first 20 weeks | ||
⍝ wt=sum of (birth, first 20 weeks growth, the next complete months, the last month part) | |||
wt←+⌿a⍪↑(k×5⌊m)(k×b×(n×b=1)+(1-r)÷(b=1)+1-b)(b×r×(m>5)×k×m-c) | wt←+⌿a⍪↑(k×5⌊m)(k×b×(n×b=1)+(1-r)÷(b=1)+1-b)(b×r×(m>5)×k×m-c) | ||
</source> | |||
Starting with a birth weight of 100 ounces (six pounds four ounces), using a decay factor of 0.9 (meaning that growth in this month is 0.9 times the growth in last month) and using UK Imperial tons<ref>In UK Imperial units, an ounce is 16 drams, a pound is 16 ounces, a stone is 14 pounds, a hundredweight is 8 stone, and a ton is 20 hundredweights. A UK ton is the "long ton" of 2240 pounds (2240=20×8×14). A US "ton" is a "short ton" of 2000 pounds. The metric "ton" (or "tonne") has 2204.6226 pounds, it being 1000 kilograms.</ref> we get this answer: | |||
[https://tio.run/##bVJBbqswEN1zilFXdhMqoKXSF@q/xD@BTYaAgg0yTmku0FZVKrWrrHqBXqDLLrgJF2nHQNMk@gjJBj@/92bmibr0FxtRVsuvr/7xobX9/cudhH95YUrcQJuIRCZpskpUohPj9c9v0GKxzC1U2R5VaKjWOsUGRGbRwFl7RihcNcDcVokNSAQBt5jaygC9bS4s3qLhHv2WpMnC624XRVcBD2nt71/JhHucoCyMzX9k91pzUJW2ebmBpalaAiwwJaFMOA2idaT99im4iGeOW/DuI7yG6XG0tgKNwmBjYWGE8kDRlbb7uAI4hI2FELhca2FG0caDlMBEr@AYnVaqLtHiHqcJF/Tbx9SPD3F6rSR1ipp4cmNqYRSMwh4YIpDn@lhmKnks1pkTdV0OgygF1XPM6cGKOC4j@C9HjVNRB2PMHGmOkBWG2H69DP2wN81aOedsGMz8BDXxzgcCjXenbprxZPA56tbCWArCkL1Zv/0U/fM7JYCtul3sWszdTnY7pmm5CfmMhb6hcbLhI/QlZ3RqCKD@xrzbEVr5KfcozxTqMQKMhUHgEjClDIKLP3wfYApGHH0D Try it online!] | |||
<source lang=apl> | |||
⌊0.5+((100÷16×2240) 0.9) Shirley 16 52 ⍝ calculate Shirley's weight in ounces | |||
228 424 | |||
</source> | |||
For the <source lang=apl inline>n</source> complete months after month 5 the total weight increase is <source lang=apl inline>k×+/b*⍳n</source> which we know is <source lang=apl inline>k×b×(1-b*n)÷1-b</source> ([[wikipedia:Geometric_series#Sum|sum of a geometric series]]). | |||
Even if Shirley lives to be 100, applying <source lang=apl inline>+/</source> over all the weeks is unlikely to cause any problematic performance. | |||
[https://tio.run/##bVJLbtswFNzrFA9ZkbUViIplIBCSS/QEpExZgkVKoOgovkAaBA7QrrzqrqteIMsudBNexH2UFNc2SgggKc6beZ/hTRWudryq18eje/3WWffy/VnA16I0ldxBl/JUpFm6SVWqUxO495/QyXJdWKjzE6rUUG91JlvguZUGbrobRMlNC8QfFd@BkMDhSWa2NoBfV3Arn6ShAf4WqEnYsj/E8SKiDHf38gOT8MsLitLY4lP2pDUHVWtbVDtYm7pDwEpmKJRzr4G0ntTt36LbZOa5Oe0/2BKm5WltDVpyI1sLK8NVAApDuv5jAXAOGwtBcLXV3IyibQAZgpFewSU6q1VTSStPOI24yO1fszA5x@mtEtgpbOJVxNTCOBqFAzBIIL7oS5mp5LFYnxxvmmoYRMWxnkvOADbIcRfDfzkaORV1NsbckxYS8tIg279chn7Yh3arfOZkGMz8CjXxzgcCLZ@vs2nHlyHPUbfhxqIRBu/N3P4Pd@@/0QFk0x8S32LqT6I/EI3bA6MzwkKD4yTDhYWCEnw1CFCPCe0PiFZhRgP08/HIogjYAnD0bOnefp05ghB886aYjBfd3t/RT0tHPiKJ/wI Try it online!] | |||
<source lang=apl> | |||
100 14 16 16⊤⌊0.5+16×((100÷16×2240)0.93)Shirley 0 16 52 | |||
100 14 16 16⊤⌊0.5+16×((100÷16×2240)0.93) | |||
0 1 1 | 0 1 1 | ||
6 0 13 | 6 0 13 | ||
4 4 15 | 4 4 15 | ||
0 0 4 | 0 0 4 | ||
</source> | |||
So we see, with these assumptions, Shirley grows from 6 pounds and 4 ounces at birth to 1 stone and 4 ounces at 16 weeks and to 1 stone, 13 pounds, 15 ounces and 4 drams when a year old. | So we see, with these assumptions, Shirley grows from 6 pounds and 4 ounces at birth to 1 stone and 4 ounces at 16 weeks and to 1 stone, 13 pounds, 15 ounces and 4 drams when a year old. | ||
=== Notes === | |||
<references/> | |||
[[Category:Examples]][[Category:Dyalog APL examples]] |