Arithmetic derivative: Difference between revisions

→‎{{header|J}}: more direct (non-recursive) implementation
(→‎{{header|J}}: more direct (non-recursive) implementation)
Line 39:
=={{header|J}}==
Implementation:
<lang J>D=: {{ (*y)*+/1*/\.q:1>.|y }}"0</lang>
 
if. 0 > y do. -D-y
In other words: find the prime factors of the absolute value of y as a sequence, find and sum each of the products with exactly one value removed from this sequence and multiply by the sign of y. (And since 0 has an infinite list of prime factors which makes treating it nonsensical, use the empty list of prime factors of 1 for that case.)
elseif. 2>y do. 0
elseif. 1 p: y do. 1
else. g+({:f)*D g=.*/}:f=. q: y
end.
}}M."0</lang>
 
Task example:
6,951

edits