Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated function application in APL

Tags:

apl

Given the integers p, q and n I would like to generate a vector of length n with the elements

(⌊p ÷ q) (⌊(⌊p ÷ q) ÷ q) ...

In other words I want to construct a vector where the i:th element is {⌊⍵ ÷ q} applied i times on p. How can I do that in APL?

like image 430
August Karlstrom Avatar asked Feb 01 '26 03:02

August Karlstrom


1 Answers

This short solution works using scan \ works in all APLs that support even basic dfns (inline lambdas):

1↓{⌊⍵÷q}\(1+n)⍴p

Try it online!

Note its inefficiency; it recalculates from the beginning for every term.

A more involved solution avoids recalculation by using / to iterate:

r←,p
{r,←⌊(¯1↑r)÷q}/(1+n)⍴1
1↓r

Try it online!

like image 194
Adám Avatar answered Feb 02 '26 15:02

Adám



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!