Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

rewrite or write

  • Loading branch information...
commit c6eb3bdc86b7720f0d2dc41c8920429f4ae826d7 1 parent ed2db6a
@youknowone authored
View
15 Prime.hs
@@ -1,7 +1,18 @@
module Prime where
-primes = primes_inner [] [2 ..]
+primes = primes_inner [2 ..]
where
- primes_inner ps (x:xs) = x:(primes_inner (x:ps) [n | n<-xs, n `mod` x /= 0])
+ primes_inner (x:xs) = x:(primes_inner [n | n<-xs, n `mod` x /= 0])
+
+divisors n
+ | n < 2 = [n]
+ | otherwise = divisors_inner n primes
+ where
+ divisors_inner remain px@(p:ps)
+ | remain == 1 = []
+ | p^2 > n = [remain]
+ | remain `mod` p == 0 = p : divisors_inner (remain `div` p) px
+ | otherwise = divisors_inner remain ps
+
View
13 euler003.hs
@@ -1,16 +1,5 @@
import Prime
-factors n
- | n < 2 = [n]
- | otherwise = factors_inner n [] primes
- where
- factors_inner remain facts px@(p:ps)
- | remain == 1 = facts
- | p * p > n = remain:facts
- | remain `mod` p == 0 = factors_inner (remain `div` p) (p:facts) px
- | otherwise = factors_inner remain facts ps
-
-
main = do
- print $ head $ factors 600851475143
+ print $ head $ reverse $ divisors 600851475143
View
3  euler008.hs
@@ -29,8 +29,7 @@ max_mult cs = max_mult_inner cs 0
| 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))
+ mult = product (map digitToInt (take 5 s))
new_max = if mult > old_max then mult else old_max
main = do
View
16 euler009.hs
@@ -0,0 +1,16 @@
+
+find_pythagorean_triplet n = fpt_inner 1 1
+ where
+ fpt_inner a b
+ | a > c = (-1,-1,-1)
+ | a^2 + b^2 == c^2 = (a, b, c)
+ | b > c = fpt_inner (a+1) (a+1)
+ | otherwise = fpt_inner a (b+1)
+ where
+ c = n - (a + b)
+
+mult_triplet (a,b,c) = a*b*c
+
+main = do
+ print $ find_pythagorean_triplet 1000
+ print $ mult_triplet( find_pythagorean_triplet 1000 )
View
5 euler010.hs
@@ -0,0 +1,5 @@
+
+import Prime
+
+main = do
+ print $! sum ( takeWhile (<=2000000) primes )
View
BIN  euler010.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
13 run
@@ -1,13 +1,2 @@
-#!/bin/bash
-if [ $1 = 'clean' ]; then
- rm *.hi
- rm *.o
- rm euler???
- exit
-fi
-
-ghc euler$1 > /dev/null
-if [ $? == 0 ]; then
- ./euler$1
-fi
+time runghc ~/Projects/euler-haskell/euler$1.hs
Please sign in to comment.
Something went wrong with that request. Please try again.