Shirley

From APL Wiki
Jump to navigation Jump to search

At the BAA webinar on 20th September 2020, Ray Polivka described a programming test set in the 1950s in the course "Programs1" in the ILLIAC work at Illinois University:

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 binary code with a handful of registers 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 tool of thought. Ellis Morgan commented that the problem is effectively a sum of a geometric series here. The function Shirley is a traditional function which one might have written in 1970 (except for the Mix in the definition of wt):

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)
 a b←(16×2240)1×2↑xb     ⍝ birth weight in ounces, monthly growth decay factor
 a←(⌊0.5+16×a)÷16        ⍝ to nearest dram
 m←w÷4          ⍝ weeks to lunar months
 c←⌊m           ⍝ complete months
 n←0⌈c-5        ⍝ number of complete months after 20 weeks
 r←b*n          ⍝ growth factor to apply in last complete month
 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)

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[1] we get this answer:

Try it online!

      ⌊0.5+((100÷16×2240) 0.9) Shirley 16 52    ⍝ calculate Shirley's weight in ounces 
228 424

For the n complete months after month 5 the total weight increase is k×+/b*⍳n which we know is k×b×(1-b*n)÷1-b (sum of a geometric series).

Even if Shirley lives to be 100, applying +/ over all the weeks is unlikely to cause any problematic performance.

Try it online!

      100 14 16 16⊤⌊0.5+16×((100÷16×2240)0.93)Shirley 0 16 52
0 1  1
6 0 13
4 4 15
0 0  4

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

  1. 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.