The following function generates all amicable numbers
in the range 2 to y.
http://mathschallenge.net/index.php?section=project&ref=problems&id=95
has info on amicable chains.
A more J like way to try and implement this function
would be to notice that at its core, is a recursive
call to (sum of divisors), but the main difficulty
with that approach is the need to reinitialize the
chain tracking list (variable a in function below)
before each call, and also maintain that lookup chain
structure on each iteration.
In a general sentence (f g y), is there a way to
update a global variable in a one liner such that it
would be equivalent to the following?
t =. g y
gvar =: 0
f t
where y is a list of arguments to be ^:_ (power limit)
processed, is there a way to inject some one time
administrative code once for each argument (that
doesn't repeat on each iteration)
amichain 2000
┌─┬──┬───────┬───────┬───┬─────────┬─────────┐
│6│28│220 284│284
220│496│1184 1210│1210 1184│
└─┴──┴───────┴───────┴───┴─────────┴─────────┘
amichain =: 3 : 0
out=. 0$0
for_ii. i. y. do.
a=. 0$0
n=. ii_index+2
while. 1 do.
n=. (+/ divisors n)
if. n=1 do. break. end.
if. n>y. do. break. end. NB. artificial constraint
if. (n = (ii_index+2)) do.
out =. out ,<((ii_index+2), a )
break. end.
if. n e. a do. break. end.
a=. a , n
end.
end.
out
)
divisors =: ~.@(*/@#~ #:@i.@<:@(2&^)@#)@q:
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm