How to refactor this in J?

不想你离开。 提交于 2019-12-24 20:32:47

问题


Here is a different approach for the Project Euler #1 solution:

+/~.(3*i.>.1000%3),5*i.>.1000%5

How to refactor it?


回答1:


[:+/@~.@,3 5([*i.@>.@%~)]

usage example:

f =: [:+/@~.@,3 5([*i.@>.@%~)]
f 1000

or

+/~.,3 5([*i.@>.@%~)1000

%~                        = 4 : 'y % x'
i.@>.@%~                  = 4 : 'i. >. y % x'
[*i.@>.@%~                = 4 : 'x * i. >. y % x'
3 5([*i.@>.@%~)]          = 3 : '3 5 * i. >. y % 3 5'
[:+/@~.@,3 5([*i.@>.@%~)] = 3 : '+/ ~. , 3 5 * i. >. y % 3 5'



回答2:


+/(#~ ( (0= 3| ]) +. (0 = 5 |]) )) 1+i.999

0 = ( 3 | ]) uses (twice) the trick of verb train (fork) with n u v (discussed at the end of http://www.jsoftware.com/help/learning/09.htm)

A different way of writing it:

+/(#~ ( ((0&=) @ (3&|)) +. ((0&=) @ (5&|)))) 1+i.999



回答3:


Here is another approach, using a simple, generic verb

multiplesbelow =: 4 : 'I. 0 = x | i.y'
+/ ~. ,3 5 multiplesbelow"0 [ 1000


来源:https://stackoverflow.com/questions/1557522/how-to-refactor-this-in-j

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