如何使Haskell使用另一个列表,取决于Haskell中的以前的答案(How to make Haskell use another list depending on the previous a

编程入门 行业动态 更新时间:2024-10-23 07:30:23
如何使Haskell使用另一个列表,取决于Haskell中的以前的答案(How to make Haskell use another list depending on the previous answer in Haskell)

我正在做项目欧拉问题63,在那里我必须找到存在的数字的数量:

x^(n) == y

其中n是y的长度。

很快就会发现,这种情况的结果在奇数和偶数之间交替出现,所以我在Haskell中提出了这个问题:

prob63 = [ n | n <- nums n , i <-[1..10], i^(length $ show n) == n] nums n | odd (n) == True = filter odd [n..] | otherwise = filter even [n..]

如果n < - [1 ..],prob63会产生如下所示的流:

[1,2,3,4,5,6,7,8,9,16,25,36,49,64,81,125,216,343,512,729,1296,2401,4096,6561,16807,32768,59049,117649,262144,531441]

但是这很慢,我之后想到的不起作用。 我需要的是,根据以前的答案,它将开始测试n的奇数或偶数。 我如何从现有的东西中解脱出来?

I am doing project euler question 63 where I must find the amount of numbers that exist where:

x^(n) == y

Where n is the length of y.

It soon emerges that the results for this condition alternate between odd and even and so I came up with this in Haskell:

prob63 = [ n | n <- nums n , i <-[1..10], i^(length $ show n) == n] nums n | odd (n) == True = filter odd [n..] | otherwise = filter even [n..]

If n <- [1..], prob63 produces a stream that looks like this:

[1,2,3,4,5,6,7,8,9,16,25,36,49,64,81,125,216,343,512,729,1296,2401,4096,6561,16807,32768,59049,117649,262144,531441]

But this is slow and what I came up with after doesn't work. What I need is, depending on the previous answer, it will start testing the odd or even integers from n. How would I go about this from what I already have?

最满意答案

再看看输出。 元素不会在奇数和偶数之间交替出现!

1,2,3,4,5,6,7,8,9,16,25,36,49,64,81,125,216,343,512,729,1296,2401,4096,6561,16807,32768,59049,117649,262144,531441]

在实现您要求的代码后,我注意到了这一点。 它的输出与您的代码的输出不匹配。 无论如何,我会在这里发布代码:

prob63 :: [Integer] prob63 = test 1 where test s = next : test (next + 1) where next = head [n | n <- [s,s+2..], i <-[1..10], i^(length $ show n) == n]

Look again at the output. The elements do not alternate between odd and even!

1,2,3,4,5,6,7,8,9,16,25,36,49,64,81,125,216,343,512,729,1296,2401,4096,6561,16807,32768,59049,117649,262144,531441]

I noticed this after implementing the code that you ask for. Its output does not match the output of your code. I'll post the code here anyway:

prob63 :: [Integer] prob63 = test 1 where test s = next : test (next + 1) where next = head [n | n <- [s,s+2..], i <-[1..10], i^(length $ show n) == n]

更多推荐

本文发布于:2023-08-05 21:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:答案   列表   Haskell   list   answer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!