1
我試圖解決26th project Euler problem和我寫了一些東西,編譯罰款,但是當我啓動它,我得到一個錯誤。26日歐拉項目:「過程停止」
程序是:
module Main where
import Data.List (maximumBy, unfoldr)
liste :: Int -> [(Int,Int)]
liste i = unfoldr étape (1,i)
where étape (n,m) = if mod (n*10) m == 0
then Nothing
else Just ((mod (n*10) m, div (n*10) m), (mod (n*10) m, i))
longueur :: Int -> Maybe Int
longueur i =
let maxRecherche = 5000
laListe = liste i
assignAttribute :: Int -> Maybe Int
assignAttribute j = if ((laListe !! j) == (laListe !! maxRecherche))
then Just (maxRecherche - j)
else if (j==1) then Nothing
else assignAttribute(j-1)
in if (length laListe < maxRecherche)
then Nothing
else assignAttribute(maxRecherche-1)
-- éléments comparés à maxRacherche, dv dc etre avant (a=a)
main :: IO()
main = do
putStrLn "hello"
let longueur370 = longueur 370
putStrLn $ "370 : " ++ show longueur370
let longueurs = Data.List.maximumBy tri $ map (\i -> (i,longueur i)) [1..1000]
putStrLn $ "resultats : " ++ (show longueurs)
where tri (_,Nothing)(_,Nothing) = EQ
tri (_,Nothing)(_,_) = LT
tri (_,_)(_,Nothing) = GT
tri (_,Just u)(_,Just v) = compare u v
下面是一些短的解釋:
- 功能
liste
創建對無限列表中,含有(休息,數字),其中其餘和數字是一對的1/n的第i個位數的計算結果(參見爲什麼我的計算項目歐拉1/N) 這個功能似乎運作良好,與370 - 的證明被測試本功能提取5000個第一對並嘗試找到任何週期的長度。我認爲第5000對被包含在一個可能的週期內,即週期已經開始。請注意,它是完全經驗的,但計算的1/n限於[1..1000],這並不是很大。 最後,這一切都是一個小的事實,如果沒有周期對的列表可以停止複雜。
錯誤是由操作系統給出:有些分鐘後,我得到的消息,「突未停止」 ......
你能幫助我嗎?
感謝,
奧利維爾
編輯: 好吧,我發現如何使它:我測試,如果列表中包含元素的給定數量,通過降低這個數量的元素,並檢查列表其餘不爲空。
你好,你是對的,但我如何測試如果列表是無窮的,不是嗎? (我會看看在接下來的幾個小時,這個問題,但如果你知道答案,你可以分享吧!)一個想法:標記列表用布爾作爲有限。你怎麼看待這件事?有更漂亮的答案嗎? – lolveley
無法檢查列表是否無限。 – ErikR
1/d的小數位總是會重複,並且週期長度始終<= d。並且循環開始之前的前綴也具有長度<= d。所以不需要考慮無限列表。 – ErikR