Created
October 13, 2017 08:28
-
-
Save Agnishom/c45f90c53eb0e3c2152bc960735fa03e to your computer and use it in GitHub Desktop.
Strange Efficiency Issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Just an utility function to calculate [root(x)] | |
| intSqrt :: Integer -> Integer | |
| intSqrt n = acc 1 n n | |
| where | |
| acc a b n | |
| | a == (b-1) = a | |
| | m*m > n = acc a m n | |
| | m*m <= n = acc m b n | |
| where | |
| m = (a + b) `div` 2 | |
| -- The fast function with list comprehension | |
| isPrime :: Integer -> Bool | |
| isPrime 1 = False | |
| isPrime 2 = True | |
| isPrime n = null [k | k <- 2:[3, 5..(intSqrt n)], n `mod` k == 0] | |
| -- The slower code. Don't understand why it's slow. | |
| isPrime' :: Integer -> Bool | |
| isPrime' 1 = False | |
| isPrime' 2 = True | |
| isPrime' n = (n `mod` 2 /= 0) && checkPrime 3 n (intSqrt n) | |
| where | |
| checkPrime m n r = if m > r then True else n `mod` m /= 0 && checkPrime (m+2) n r | |
| -- Devang's code. Slightly faster, idk why. | |
| isPrime_2 :: Integer -> [Integer] -> Integer -> Bool | |
| isPrime_2 1 _ _ = False | |
| isPrime_2 2 _ _ = True | |
| isPrime_2 n (x:xs) k | |
| | x > k = True | |
| | n `mod` x == 0 = False | |
| | otherwise = isPrime_2 n xs k | |
| isPrime2 n = isPrime_2 n (2:[3,5..]) (intSqrt n) | |
| -- Data for some tests we did at compsci lab | |
| -- Prime : 9831655609 | |
| -- isPrime : 0.05s | |
| -- isPrime' : 0.12s | |
| -- isPrime2 : 0.10s | |
| -- Prime : 999999999999577 | |
| -- isPrime : 8.37s | |
| -- isPrime' : 23.15s | |
| -- isPrime2 : 18.95s | |
| -- Measurements done with :set +s, ghci was restarted between runs of prime to clear cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {- | |
| selectLeader 10000 7: 1s | |
| selectLeader1 10000 7 : 17s | |
| -} | |
| selectLeader :: Int -> Int -> Int | |
| selectLeader n k = head (last (auxSelect [1..n] k 0)) | |
| auxSelect :: [Int] -> Int -> Int -> [[Int]] | |
| auxSelect [x] k j = [[x]] | |
| auxSelect l k j = l:auxSelect (take j l ++ drop (j+1) l) k ((j+k-1) `mod` ((length l)-1)) | |
| selectLeader1 :: Int -> Int -> Int | |
| selectLeader1 n k = head (last (auxSelect1 [1..n] k 0)) | |
| delete :: [Int] -> Int -> Int -> [Int] | |
| delete (x:xs) j i | |
| | i==j = xs | |
| | otherwise = x:delete xs j (i+1) | |
| auxSelect1 :: [Int] -> Int -> Int -> [[Int]] | |
| auxSelect1 [x] k j = [[x]] | |
| auxSelect1 l k j = l:auxSelect1 (delete l j 0) k ((j+k-1) `mod` ((length l)-1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment