If all you want is the sum of divisors, you don't have to enumerate
the divisors as m70 does:
divsum =: [: */ [: ((^>:) %&:<: [)/ (__&q:)
divsum 28
56
Henry Rich
On 8/9/2011 8:10 PM, David Ward Lambert wrote:
> To encourage use of existing j resources, sentences for divisors and a
> perfect number test are in j phrases "numbers and counting".
>
> http://www.jsoftware.com/docs/help701/phrases/numbers_counting.htm
>
> Your factoring algorithm involves a common problem, that of reusing an
> intermediate result within a tacit expression. You recompute the
> intermediate value. Here is a solution similar to yours that stores the
> intermediate vector. Monad proper_divisors is explicit. The tacit
> solutions I can think of seem contrived.
>
> 9!:7'+++++++++|-'
>
> NB. 22 won't be a divisor of 25
> possible_divisors =: [:>: [: i. [:<. -:
>
> possible_divisors&.>24 9
> +--------------------------+-------+
> |1 2 3 4 5 6 7 8 9 10 11 12|1 2 3 4|
> +--------------------------+-------+
>
> proper_divisors=: 3 : 'pd #~ 0 = (pd=. possible_divisors y) | y'
>
> proper_divisors&.>24 9
> +--------------+---+
> |1 2 3 4 6 8 12|1 3|
> +--------------+---+
>
> perfect=: = [: +/ proper_divisors
>
> (#~ perfect)}.i.200 NB. OOPS!
> |length error: proper_divisors
> | pd#~0=pd |y[pd=.possible_divisors y
>
> (#~ perfect"0)}.i.200 NB. perfect needs to be rank 0
> 6 28
>
> perfect=: = ([: +/ proper_divisors)"0 NB. build in rank 0
>
> (#~ perfect)}.i.200
> 6 28
>
>
> Note that by using the built in factoring algorithms J can detect large
> perfect numbers, whereas computing i.2305843008139952128x would consume
> several scribes and a lot of papyrus.
>
> m70 2305843008139952128x
> 1
>
>
>
>> Date: Tue, 9 Aug 2011 22:54:35 +0300
>> From: Uriel Zylbermann<[email protected]>
>> Subject: [Jprogramming] Hi, I'm new. monad on list length error
>> To: [email protected]
>> Message-ID:
>> <CAMA-M3kRKQ6uf-AzwLZE+=xyo-vf4pmxqwbg2rlejudeqe-...@mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>>
>> Hi, I'm new to J so my programs might be a bit more complicated than needed,
>> I would be happy if you could tolerate that(and perhaps even give me fixes
>> when needed).
>>
>> anyway, I have a monad function that checks if a number is a perfect number:
>>
>> (=([:+/((0=((|~)([:>:[:i.-&1)))#([:>:[:i.-&1))))
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm