Skip to content

Commit

Permalink
rewrite and write 2~8
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Mar 27, 2011
1 parent 68eb6f5 commit ed2db6a
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Prime.hs
@@ -0,0 +1,7 @@

module Prime where

primes = primes_inner [] [2 ..]
where
primes_inner ps (x:xs) = x:(primes_inner (x:ps) [n | n<-xs, n `mod` x /= 0])

11 changes: 3 additions & 8 deletions euler002.hs
@@ -1,12 +1,7 @@

fib_sum n = fib_sum_inner [1,0]
fibonaccis = fib_inner 0 1
where
fib_sum_inner s@(x1:x2:xs)
| x > n = 0
| otherwise = even_value x + fib_sum_inner (x:s)
where
x = x1 + x2
even_value n = if even n then n else 0
fib_inner a b = a:(fib_inner b (a+b))

main = do
print $ fib_sum 4000000
print $ sum (takeWhile (<= 4000000) ([n|n<-fibonaccis,even n]))
4 changes: 1 addition & 3 deletions euler003.hs
@@ -1,7 +1,5 @@

primes = primes_inner [] [2 ..]
where
primes_inner ps (x:xs) = x:(primes_inner (x:ps) [n | n<-xs, n `mod` x /= 0])
import Prime

factors n
| n < 2 = [n]
Expand Down
16 changes: 16 additions & 0 deletions euler004.hs
@@ -0,0 +1,16 @@

is_palindrome n = n == reverse_num n 0
where
reverse_num 0 tgt = tgt
reverse_num src tgt = reverse_num (src `div` 10) (tgt * 10 + (src `mod` 10))

max_palindrome n = mp_inner n n 0
where
mp_inner a b max
| a * 2 < n = max
| a * b < max = mp_inner (a-1) (a-1) max
| is_palindrome (a*b) = mp_inner (a-1) (a-1) (a*b)
| otherwise = mp_inner a (b-1) max

main = do
print $ max_palindrome 999
10 changes: 10 additions & 0 deletions euler005.bruteforce.hs
@@ -0,0 +1,10 @@

lcm' ns = lcm_inner ns 1
where
lcm_inner [] r = r
lcm_inner ns r
| foldl (&&) True (map ((0 ==).(r `mod`)) ns) = r
| otherwise = lcm_inner ns (r+1)

main = do
print $ lcm' [1 .. 20]
9 changes: 9 additions & 0 deletions euler005.hs
@@ -0,0 +1,9 @@

lcm' [] = 0
lcm' ns = lcm_inner ns 1
where
lcm_inner [] r = r
lcm_inner (n:ns) r = lcm_inner ns (r * (n `div` (gcd n r)))

main = do
print $ lcm' [1 .. 20]
5 changes: 5 additions & 0 deletions euler006.hs
@@ -0,0 +1,5 @@

square_diff n = (sum [1..n]) ^2 - (sum (map (^2) [1..n]))

main = do
print $ square_diff 100
5 changes: 5 additions & 0 deletions euler007.hs
@@ -0,0 +1,5 @@

import Prime

main = do
print $ primes !! (10001 -1)
37 changes: 37 additions & 0 deletions euler008.hs
@@ -0,0 +1,37 @@

import Char

numstring =
"73167176531330624919225119674426574742355349194934" ++
"96983520312774506326239578318016984801869478851843" ++
"85861560789112949495459501737958331952853208805511" ++
"12540698747158523863050715693290963295227443043557" ++
"66896648950445244523161731856403098711121722383113" ++
"62229893423380308135336276614282806444486645238749" ++
"30358907296290491560440772390713810515859307960866" ++
"70172427121883998797908792274921901699720888093776" ++
"65727333001053367881220235421809751254540594752243" ++
"52584907711670556013604839586446706324415722155397" ++
"53697817977846174064955149290862569321978468622482" ++
"83972241375657056057490261407972968652414535100474" ++
"82166370484403199890008895243450658541227588666881" ++
"16427171479924442928230863465674813919123162824586" ++
"17866458359124566529476545682848912883142607690042" ++
"24219022671055626321111109370544217506941658960408" ++
"07198403850962455444362981230987879927244284909188" ++
"84580156166097919133875499200524063689912560717606" ++
"05886116467109405077541002256983155200055935729725" ++
"71636269561882670428252483600823257530420752963450"

max_mult cs = max_mult_inner cs 0
where
max_mult_inner s@(_:cs) old_max
| length s < 5 = old_max
| otherwise = max_mult_inner cs new_max
where
cval c = (ord c) - (ord '0')
mult = foldl (*) 1 (map cval (take 5 s))
new_max = if mult > old_max then mult else old_max

main = do
print $ max_mult numstring

0 comments on commit ed2db6a

Please sign in to comment.