简体   繁体   English

隐式函数将列表中的五个连续数字相乘:J,j701

[英]Tacit function to multiply five consecutive number in a list: J, j701

I'm working on Project Euler, I'm on problem 8 , and I'm trying a simple brute force: Multiply each consecutive 5 digit of the number, make a list with the results, and find the higher. 我正在研究Euler项目,正在研究问题8 ,并且正在尝试一种简单的蛮力方法:将数字的每个连续5位数字相乘,列出结果,然后找出更高的值。

This is the code I'm currently trying to write in J: 这是我目前正在尝试用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

How I can use takeFive for all the index of N? 如何对所有N索引使用takeFive? I tried: 我试过了:

  (i.#N) takeFive N
|length error: takeFive
|   (i.#N)    takeFive N 

but it doesn't work and I don't know why. 但它不起作用,我也不知道为什么。 Thank you all. 谢谢你们。

1. The reason that (i.#N) takeFive N is not working is that you are essentially trying to run 5{. ((i.#N)-1) }. N 1. (i.#N) takeFive N不起作用的原因是您实际上是在尝试运行5{. ((i.#N)-1) }. N 5{. ((i.#N)-1) }. N 5{. ((i.#N)-1) }. N but you have to use x not as a list but as an atom. 5{. ((i.#N)-1) }. N但您不必将x用作列表而是原子。 You can do that by setting the appropriate left-right rank " of the verb: 你可以做,通过设置适当的左右秩"的动词:

 (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

2. One other way is to bind ( & ) your list ( N ) to takeFive and then run the binded-verb through every i.#N . 2.另一种方法是将列表( N )绑定( & )到takeFive ,然后通过每个i.#N运行绑定动词。 To do this, it's better to use the reverse version of takeFive: takeFive~ : 为此,最好使用反向版本的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

or (N&(takeFive~)) each i.#N . (N&(takeFive~)) each i.#N

3. I think, though, that the infix dyad \\ might serve you better: 3.不过,我认为infix dyad \\可能会为您提供更好的服务:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM