Commits

WANG Yanjin committed f931274

solve p28

  • Participants
  • Parent commits 496a636

Comments (0)

Files changed (2)

         pCnt x y = (length (takeWhile (\n -> isPrime (n*n+x*n+y)) [0..]), (x, y))
         tt = snd . maximum $ [pCnt x y | x <- [-999..999], y <- [2..999]]
 
+-- surround is recursive, with no tail call optimize, so it's a little slow
+p28 :: Int
+p28 = sumdig s1001 + sumdig (reverse s1001) - 1
+    where
+        surround :: Int -> [[Int]]
+        surround 1 = [[1]]
+        surround n
+            | even n || n < 1 = []
+            | otherwise = preIns:((map (\x -> (n*n-n+1-(fst x)):((snd x) ++ [(n-2)^2+(fst x)])) (zip [1..] xa)) ++ aftIns)
+                where
+                    xa = surround (n-2)
+                    preIns = [(n*n-n+1)..n*n]
+                    aftIns = [reverse [(n-2)^2+n-1..(n-2)^2+2*n-2]]
+        sumdig :: [[Int]] -> Int
+        sumdig x = sum $ map head (zipWith drop [0..] x)
+        s1001 = surround 1001
 
 -- solutions end
 
 
     args <- getArgs
     let n = read (args !! 0) :: Int
-        xxx = p27
+        xxx = p28
     print $ xxx
 
     t2 <- getCPUTime
     print('Sum: ', _sum)
 
 if __name__ == '__main__':
-	print(p26())
+	print(p28())
 	print("Seconds:", time.clock() - start)