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
import Data.List | |
possibleTriangles :: Integer -> Int | |
possibleTriangles n = length . filter (\x -> length x == 1) . group . sort . concat $ [ [s,2*s..n] | i <- [1,3..(upperBound n)] | |
, j <- [2,4..(upperBound n)-i] | |
, gcd i j == 1 | |
, let s = abs (j*j - i*i) + 2*i*j + i*i + j*j ] | |
upperBound :: Integer -> Integer | |
upperBound = truncate . sqrt . fromIntegral |
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
import Control.Arrow (second) | |
class Fluffy f where | |
furry :: (a -> b) -> f a -> f b | |
-- Exercise 1 | |
-- Relative Difficulty: 1 | |
instance Fluffy [] where | |
furry _ [] = [] | |
furry f xs = foldr ((:) . f) [] xs |
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
import Data.Array | |
pentagonals :: [Int] | |
pentagonals = [ n*(3*n-1) `div` 2 | n <- pentagonalIndices ] | |
where | |
pentagonalIndices :: [Int] | |
pentagonalIndices = concatMap (\x -> [x,-x]) [1..] | |
partition :: Array Int Integer | |
partition = listArray (0, 100000) $ 1 : 1 : map partition' [2..] |
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
import Data.Numbers.Primes | |
import Data.List (group, sort) | |
import Control.Arrow ((&&&)) | |
sumOfDivisors :: Integer -> Integer | |
sumOfDivisors n = (-n +) . product . map ( productOfPowers . (&&&) head length ) . group . primeFactors $ n | |
where | |
productOfPowers :: (Integer, Int) -> Integer | |
productOfPowers (base, power) = sum [ base ^ powers | powers <- [0..power] ] | |
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
import PECore (eulerTotient) | |
import Data.Numbers.Primes | |
import Control.Arrow ((&&&)) | |
eulerChains :: Integer -> Integer | |
eulerChains 1 = 1 | |
eulerChains n = 1 + eulerChains (eulerTotient n) | |
main = print . sum . map fst . filter ( (== 25) . snd ) . map ((&&&) id eulerChains) . takeWhile (<40000000) $ primes |
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
import PECore (fastPow) | |
import Data.Numbers.Primes (primes) | |
import Control.Arrow ((&&&)) | |
main = print . sum . take 40 . map fst . filter ( (== 1) . snd) . map ( (&&&) id (fastPow 10 (10^9)) ) . dropWhile (<= 3) $ primes |
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
import PECore (numToList, listToNum) | |
import Control.Monad (ap) | |
import Data.List (sort) | |
bouncy :: (Num a, Ord a) => [a] -> Bool | |
bouncy = not . (\x -> all (>=0) x || all (<=0) x) . map (uncurry (-)) . ap zip tail | |
bouncyNumber :: Integer -> Bool | |
bouncyNumber = bouncy . numToList |
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
import Data.Numbers.Primes (primeFactors) | |
import Data.List (group, nub, sort) | |
import Control.Arrow ((&&&)) | |
import PECore | |
squarefree :: Integer -> Bool | |
squarefree = all ((< 2) . snd) . map ((&&&) head length) . group . primeFactors | |
numFromPascalTriangles :: Integer -> [Integer] | |
numFromPascalTriangles n = nub . sort . concat . map (\x -> map (combinations x) $ [0..x]) $ [0..n-1] |
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
import Control.Applicative ((<$>)) | |
import Control.Monad ((<=<), forever, liftM, liftM2) | |
import Data.Function (on) | |
import Data.Maybe (fromMaybe) | |
import Data.Time.Format (formatTime) | |
import Data.Time.LocalTime (getZonedTime) | |
import GHC.Conc.IO (threadDelay) | |
import Network.Lastfm.Types | |
import Network.Lastfm.API.User | |
import System.Directory (getHomeDirectory) |
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
import Control.Arrow ((***), (&&&)) | |
import Control.Monad ((<=<), forM_, join, liftM, liftM2, when) | |
import Data.Maybe (fromMaybe) | |
import Kludges | |
import Network.Lastfm.API.Artist (getInfo) | |
import Network.Lastfm.Types | |
import Text.Printf | |
import Text.XML.Light | |
import qualified Network.Lastfm.API.Library as L |