Square of the sum minus sum of the squares in J (or how to take the train?)

半城伤御伤魂 提交于 2019-12-23 01:16:04

问题


Still in the learning process of J... The problem to solve is now to express the square of the sum minus the sum of the squares of natural integers.

The naive solution is

(*:+/>:i.100) - (+/*:>:i.100)

Now, I want to use a fork to be able to write the list >:i.100 only one time. My fork should like to:

  h
/   \
f   g
|   |
x   x

where f is the square of the sum, g is the sum of the squares, and h is minus. So, naively, I wrote:

((*:+/) - (+/*:)) >:i.100

but it gives me a domain error. Why? I also tried:

(+/ (*: - +/) :*) >: i.100

and this time, it gives me a long list of numbers... I guess it has something to do with the @ conjunction, but I still don't figure out what the At does... Continuing my quest, I finally got

((+/*+/) - +/@:*:) >:i.100

but I don't like the fact I manually compute the squares instead of using the *: operator, and I don't really understand why I need the @: conjunction. Could somebody gives me some light about this problem?


回答1:


(+/*:) and (*:+/) don't do what you think they do.

Actually, your f is Q (S x) (square of sum of x) and your g is S (Q x) (sum of square of x). You can see that for any f,g it is f (g y) = (f @: g) y.

So, you can write

(Q (S x)) h (S (Q x))

as

((Q @: S) x) h ((S @: Q) X)

which is now equivalent to

(f x) h (g x)

or

(f h g) x

Thus,

((*: @: (+/)) - (+/ @: *:)) >: i.1000

Note also that *: @: (+/) is not the same as *: @: +/, since +/ is not one verb (like *:) but a composite verb from a verb (+) and an adverb (/).



来源:https://stackoverflow.com/questions/15160502/square-of-the-sum-minus-sum-of-the-squares-in-j-or-how-to-take-the-train

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!