Short of actually understanding the algorithm, I'll apply the transformations I see, shortening the sentence by 20 characters:

A =: 12 60   NB. input
B =: 16 108  NB. correct result

NB. the original definition and test case
sum =: (*/@:-&1@:{. %~ */@:-&1@:({. ^ 1&+@:,@:}.))@:(2&p:) - ]
assert B-: sum&>A     NB. independently verified

NB. we'll shorten from there
assert B-: ((*/@:-&1@:{. %~ */@:-&1@:({. ^ 1&+@:,@:}.))@:(2&p:) - ])&>A
NB. to here
assert B-: (-~ ((^ >:)/ %&(*/@:<:) {.)@:(2&p:))&>A

NB. these are the transformations.

NB. original
assert B-: ((*/@:-&1@:{. %~ */@:-&1@:({. ^ 1&+@:,@:}.))@:(2&p:) - ])&>A

NB. for 1&+ use >:
assert B-: ((*/@:-&1@:{. %~ */@:-&1@:({. ^ >:@:,@:}.))@:(2&p:) - ])&>A

NB. remove ravel, recognize that 2&p: has two items
NB. insert a hook in place of ({. ^ >:@:,@:}.)
assert B-: ((*/@:-&1@:{. %~ */@:-&1@:((^ >:)/))@:(2&p:) - ])&>A

NB. remove the parentheses about the new hook, because
NB. the preceding @: means the verb to @:'s left is monadic and so
NB. the / (insertion) can apply to the entire verb.
assert B-: ((*/@:-&1@:{. %~ */@:-&1@:(^ >:)/)@:(2&p:) - ])&>A

NB. swap the order of division to remove ~ (passive)
assert B-: ((*/@:-&1@:(^ >:)/ % */@:-&1@:{.)@:(2&p:) - ])&>A

NB. convert the monadic fork (u - ]) into the hook (-~ u)
assert B-: (-~ (*/@:-&1@:(^ >:)/ % */@:-&1@:{.)@:(2&p:))&>A

NB. observe (*/@:-)&1 is "insert multiply after subracting 1"
NB. permitting <: (decrement) in place of -&1 .
assert B-: (-~ (*/@:<:@:(^ >:)/ % */@:<:@:{.)@:(2&p:))&>A

NB. divide---but first perform the common operation (*/@:<:) on x and y
assert B-: (-~ ((^ >:)/ %&(*/@:<:) {.)@:(2&p:))&>A

Date: Tue, 10 Jun 2014 18:10:19 +0100
From: Jon Hough<[email protected]>
To:"[email protected]"  <[email protected]>
Subject: [Jprogramming] Shorten a verb
Message-ID:<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

My attempt at making a verb that finds the total of all proper divisors of an 
integer seems to work.
e.g. if n = 12, the sum of proper divisors is 1 +2 +3+4+6 = 16 (note 12 is not 
included)
This verb is actually equal to the "sigma function" minus n.Wikipedia 
explanation:http://en.wikipedia.org/wiki/Divisor_function
(I essentially used the equation for sigma_x(n) where x = 1)
my verb:

sum =.(((*/@:-&1@:{.)%~(*/@:-&1@:({.^(1&+@:,@:}.))))@:(2&p:))-]
This seems ok, but is not aesthetically pleasing, and seems to be very 
bracketty, and given that the mathematical equation is pretty concise I am 
surprised the J verb is so long. If anyone knows a nicer way of doing this I 
would be grateful to see it.
Regards.                                        

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to