Skip to content

Instantly share code, notes, and snippets.

View cmoore's full-sized avatar
🆑

Clint Moore cmoore

🆑
View GitHub Profile
p1 :: Int
p1 = sum $ nub $
filter (\x -> x `mod` 3 == 0) [1..999] ++
filter (\x -> x `mod` 5 == 0) [ 1..999 ]
fib :: ( Num t, Num t1 ) => t -> t1
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
cnl_clink :: (Num t, Num a, Ord a) => t -> [a]
cnl_clink x = let lx = fib x
in
if lx > 4000000 then
[]
gpfsearch :: ( Integral a ) => a -> a -> a
gpfsearch f x | x == f = x
| divides x f = gpfsearch f ( div x f )
| otherwise = gpfsearch ( f + 1 ) x
where divides x y = mod x y == 0
import Data.List (sort)
problem_four :: Int
problem_four =
head . reverse . sort $
filter (>0) $ concat $ map (\x ->
map( \y -> fix x y ) nx
) nx
where
-- Ahh, that's better!
p7 = ( filter isprime [ 1.. ] ) !! 10001
{-
1. [1..] - An infinite list, starting with 1, of integers. [1,2,3.. and so on]
2. (filter isprime x ) - Filter out of that list the numbers for which isprime is true.
3. !! 10001 - Take the 10001th element from that list.
-}
-- The Haskell Wiki solution to this still seems like witchcraft.
-- foldr1 lcm [1..20]
c_s :: Int -> [ Int ]
c_s x = nub $ map ( x `mod` ) [ 1..20 ]
p5_iter :: Int -> Int
p5_iter x = case length $ nub $ map ( x `mod` ) [ 1..20 ] of
1 ->
x
problem_six =
let sum_of_squares = sum $ map (\x -> x * x ) [1..100]
sum_of_numbers = sum [1..100]
in
( sum_of_numbers * sum_of_numbers ) - sum_of_squares
-- Check the actual problem for this 1000 digit number.
p8 :: String
p8 = "73167176531330624..."
p8_five [] = []
p8_five x = take 5 x : p8_five ( tail x )
-- Take a string, break it into seperate integers, and then get a product of those integers.
prod_char x = product $ map digitToInt x
p9_is_correct :: Int -> Int -> Int -> Bool
p9_is_correct x y z =
x^2 + y^2 == z^2 &&
x + y + z == 1000 &&
x < y && y < z
p9_fix :: Int -> Int -> ( Int, Int )
p9_fix x y =
case ( x > y || x == y ) of
True ->
let y' = x + 1
in
(x,y')
False ->
(x,y)