[Jprogramming] Recursive verbs
'Mike Day' via Programming
programming at jsoftware.com
Tue Nov 27 10:37:51 UTC 2018
This is iterative rather than recursive, and exploits the gerund form
of dyadic power,
whose usage I'm still learning:
x u^:(v0`v1`v2)y ↔ (x v0 y)u^:(x v1 y) (x v2 y) NB. from the
Vocabulary
Here we have:
u =: (2 4 <:@* ])@{:@] NB. generate next TWO terms given the first
one or two.
v0 =: v2 =: [
v1 =: i.@]
f =: (0-.~,)@:(u^:(v0`v1`v2)) NB. r.arg approx half number of
required terms
NB. it's easy enough to get the number of terms right, at the expense of
NB. making this reply more complicated than it needs to be!
4 f 3
4 7 15 29 59
This sort of explains what's happening:
4 (u^:(v0`v1`v2)) 4
4 0
7 15
29 59
117 235
_3 (u^:(v0`v1`v2)) 3
_3 0
_7 _13
_27 _53
The fragment (0-.~,)@: just ravels the result and removes the zero.
Otherwise, you could iterate a verb on (ai, sign i), or, if you always
want to
apply it to a positive starting integer, do ai -> -a(i+1), using the
sign of the
term, and then take the absolute values of the resulting series.
Cheers,
Mike
On 27/11/2018 07:09, Skip Cave wrote:
> How would you write an implicit or explicit recursive verb for this
> sequence formula?
>> a1=.4
>> a1 , (a2=.1-~2*a1) , (a3=.1+~2*a2) , (a4=.1-~2*a3) , (a5=.1+~2*a4) ...
> (an=.1(+-)~2*an-1
>>> The verb would be specified as x f y, where x is the starting integer a1,
> and y is the number of terms (n) - or vice versa.
>> The result will be a vector n items long. Note the alternating sign in each
> term
>>> Example:
>> 4 f 5
>> 4 7 15 29 59
>>> Skip
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
More information about the Programming
mailing list