Permalink
Browse files

补充完整的第8题的解法,最好通过管道将原有的100位的数据输入给程序。

  • Loading branch information...
1 parent b9b2b81 commit d8959e9606ce378f6c70c392fdc19e58a681bcfc @Liutos committed Mar 21, 2012
View
@@ -1,4 +1,4 @@
pro6 :: Int -> Int
pro6 n =
let tmp = (n * (n + 1)) `div` 2
- in tmp * tmp - sum [ x * x | x <- [1..n]]
+ in tmp ^ 2 - sum [ x * x | x <- [1..n]]
View
Binary file not shown.
View
Binary file not shown.
View
@@ -1,5 +1,24 @@
import qualified Data.Char as Char
+import Data.List
digStrProd :: String -> Int
digStrProd digStr =
- product $ map (\c -> Char.ord c - Char.ord '0') digStr
+ product $ map (\c -> Char.ord c - Char.ord '0') digStr
+
+subseq :: [a] -> Int -> Int -> [a]
+subseq seq start end =
+ snd $ splitAt start (take end seq)
+
+pro8 :: String -> Int
+pro8 digitString =
+ let rec i max =
+ if i > 995 then max
+ else let num = digStrProd $ subseq digitString i $ i + 5
+ in if num > max
+ then rec (i + 1) num
+ else rec (i + 1) max
+ in rec 0 0
+
+main = do
+ digitStr <- getLine
+ print $ pro8 digitStr
View
Binary file not shown.
View
Binary file not shown.
View
@@ -0,0 +1,27 @@
+(defun divisor-count (number)
+ (let ((cnt 0))
+ (dotimes (i (truncate (sqrt number)))
+ (if (zerop (mod number (1+ i)))
+ (incf cnt 2)))
+ cnt))
+
+(defun triangle-number-p (number)
+ (let ((root (truncate (/ (1- (sqrt (1+ (* 8 number)))) 2))))
+ (= number
+ (/ (* root (1+ root)) 2))))
+
+(defun pro12 ()
+ (labels ((rec (n)
+ (if (and (triangle-number-p n)
+ (> (divisor-count n) 500))
+ n
+ (rec (1+ n)))))
+ (rec 28)))
+
+(defun pro12 ()
+ (labels ((rec (test inc)
+ (let ((cnt (divisor-count test)))
+ (if (> cnt 500)
+ test
+ (rec (+ test inc) (1+ inc))))))
+ (rec 1 2)))

0 comments on commit d8959e9

Please sign in to comment.