1
0
Fork 0

Problem 64

This commit is contained in:
Jonathan Chan 2017-06-04 21:43:00 -07:00
parent 0f93c61e91
commit facd95edfd
4 changed files with 16 additions and 0 deletions

BIN
64 Executable file

Binary file not shown.

BIN
64.hi Normal file

Binary file not shown.

16
64.hs Normal file
View File

@ -0,0 +1,16 @@
next :: (Integer, Integer, Integer) -> (Integer, Integer, Integer)
next (a, b, n) = let nextA = (n - b * b) `div` a
nextB = nextA * ceiling((sqrt (fromIntegral n) + fromIntegral b) / fromIntegral nextA - 1) - b
in (nextA, nextB, n)
hasOddPeriod :: Integer -> Bool
hasOddPeriod n = let sqrtn = floor . sqrt . fromIntegral $ n
initial = (1, sqrtn, n)
in if sqrtn * sqrtn == n
then False
else odd . (+1) . length $ takeWhile (initial /=) (tail $ iterate next initial)
numOfOddPeriods :: Integer -> Int
numOfOddPeriods n = length $ filter (== True) $ map hasOddPeriod [2..n]
main = print $ numOfOddPeriods 10000

BIN
64.o Normal file

Binary file not shown.