Loops 将列表中的五个连续数字相乘的默认函数:J,j701
我在做Euler项目,我在做,我在尝试一个简单的暴力:将数字的每一个连续的5位数相乘,列出结果,然后找出更高的数字 这是我目前试图用J编写的代码:Loops 将列表中的五个连续数字相乘的默认函数:J,j701,loops,j,tacit-programming,Loops,J,Tacit Programming,我在做Euler项目,我在做,我在尝试一个简单的暴力:将数字的每一个连续的5位数相乘,列出结果,然后找出更高的数字 这是我目前试图用J编写的代码: n =: 731671765313x NB. 'n' will be the complete 1000-digits number itl =: (".@;"0@":) NB. 'itl' transform an integer in a list of his digit N =: itl n NB. j
n =: 731671765313x
NB. 'n' will be the complete 1000-digits number
itl =: (".@;"0@":)
NB. 'itl' transform an integer in a list of his digit
N =: itl n
NB. just for short writing
takeFive =: 5 {. ] }.~ 1 -~ [
NB. this is a dyad, I get this code thanks to '13 : '5{.(x-1)}.y'
NB. that take a starting index and it's applied to a list
如何使用takeFive来表示N的所有索引?
我试过:
但它不起作用,我也不知道为什么。
谢谢大家。1。(i.#N)takeFive N
不起作用的原因是您实际上正在尝试运行5{((i.#N)-1)}。N
但您必须使用x
作为原子而不是列表。您可以通过设置动词的适当位置来实现这一点:
(i.#N) (takeFive"0 _) N
7 3 1 6 7
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3
5 3 1 3 0
3 1 3 0 0
1 3 0 0 0
二,。另一种方法是将(&
)列表(N
)绑定到takeFive
,然后在每个i.#N
中运行绑定的动词。为此,最好使用takeFive的反向版本:takeFive~
:
((N&(takeFive~))"0) i.#N
7 3 1 6 7
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3
5 3 1 3 0
3 1 3 0 0
1 3 0 0 0
或(N&(takeFive~)每个i.#N
三,。不过,我认为,这可能会更好地为您服务:
5 >\N
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3
中缀二元是完美的!谢谢调整一个小音符:你的
itl
可以被简单地用基数的倒数(#)代替N=:10#。inv N
5 >\N
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3