Skip to content
Browse files

Euler3. Also BigNum implementation of factoring / prime functions

  • Loading branch information...
1 parent abac5d4 commit cd2034e32074e1d43ca2e234b25af7a989c3b022 @thomasnokleby thomasnokleby committed Feb 20, 2012
Showing with 49 additions and 0 deletions.
  1. +3 −0 src/main/haskell/Euler3.hs
  2. +46 −0 src/main/haskell/bigmath.hs
View
3 src/main/haskell/Euler3.hs
@@ -0,0 +1,3 @@
+import BigMath
+
+euler3 = primefactorsBig 600851475143
View
46 src/main/haskell/bigmath.hs
@@ -0,0 +1,46 @@
+module BigMath where
+
+isprimeBig :: Integer -> Bool
+isprimeBig n = let maxCand = preparefactorsBig n
+ in isprimeloopBig n maxCand 2
+
+isprimeloopBig :: Integer -> Integer -> Integer -> Bool
+isprimeloopBig n maxCandidate iter = if iter <= maxCandidate
+ then
+ if n `mod` (fromInteger iter) < 1
+ then --factor found!
+ False
+ else
+ let tmp = iter + 1
+ in
+ isprimeloopBig n maxCandidate tmp
+ else True
+
+
+primefactorsBig :: Integer -> [Integer]
+primefactorsBig n = filter isprimeBig (properfactorsBig n)
+
+preparefactorsBig :: Integer -> Integer
+preparefactorsBig n = floor (sqrt (fromInteger n))
+
+properfactorsBig :: Integer -> [Integer]
+properfactorsBig n = let maxCand = preparefactorsBig n
+ in factorsLoopBig n maxCand 2 [1]
+
+factorsLoopBig :: Integer -> Integer -> Integer -> [Integer] -> [Integer]
+factorsLoopBig n maxCandidate iter acc = if iter <= maxCandidate
+then
+ if n `mod` (fromIntegral) iter < 1
+ then --factor found
+ let tmp = iter + 1
+ accC = if iter /= n `div` iter
+ then
+ [iter] ++ [ n `div` iter] ++ acc
+ else
+ [iter] ++ acc
+ in factorsLoopBig n maxCandidate tmp accC
+ else
+ let tmp = iter + 1
+ in factorsLoopBig n maxCandidate tmp acc
+ else
+ acc

0 comments on commit cd2034e

Please sign in to comment.
Something went wrong with that request. Please try again.