gpt4 book ai didi

haskell - 随机 haskell 错误。计算永远不会结束

转载 作者:行者123 更新时间:2023-12-02 21:11:59 26 4
gpt4 key购买 nike

我写了一个小程序来执行广义凯撒密码。我不明白为什么我的暴力破解功能不起作用。我想做的就是尝试乘数和偏移量小于 26 的所有可能组合。此外,如果有人能为“tryallcombinations”函数提出更好的方法,我们将不胜感激。

import Data.Char

caesarencipher::Int->Int->String->String
caesarencipher r s p = map chr $ map encipher plaintext
where
plaintext = map (\x->(ord x) - 97) p
encipher p = (mod (r*p + s) 26) + 97

caesardecipher::Int->Int->String->String
caesardecipher r s c = map chr $ map decipher ciphertext
where
ciphertext = map (\x->(ord x) - 97) c
inverser x | mod (r * x) 26 == 1 = x
| otherwise = inverser (x + 1)
decipher c = (mod ((inverser 1) * (c - s)) 26) + 97

tryallcombinations::String->[String]
tryallcombinations ciphertext = map (\x->x ciphertext) possibilities
where
rs = map caesardecipher [0..25]
possibilities = concat $ map (\x-> map x [0..25]) rs

最佳答案

26 不是素数,因此并非所有数字都有模逆 mod 26。这意味着逆函数有时永远不会返回,但会永远递归。

关于haskell - 随机 haskell 错误。计算永远不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9937021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com