Skip to content

Commit

Permalink
final code
Browse files Browse the repository at this point in the history
  • Loading branch information
mwotton committed Jun 7, 2011
0 parents commit 3fce0d3
Show file tree
Hide file tree
Showing 7 changed files with 1,127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Setup.hs
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
2 changes: 2 additions & 0 deletions Tests/test_true.hs
@@ -0,0 +1,2 @@
module T where
test_true = True
14 changes: 14 additions & 0 deletions helper.hs
@@ -0,0 +1,14 @@
import Data.Numbers.Primes
import Data.Word
import Data.Binary
import Data.Vector.Binary
import qualified Data.Vector.Unboxed as U
biggest::Integer
biggest = 10^12

smallprimes :: [Word]
smallprimes = {-# SCC "smallprimes" #-} takeWhile (<= (ceiling . sqrt $ fromIntegral biggest)) primes

small_vector = U.fromList smallprimes

main = encodeFile "primes.dat" small_vector
15 changes: 15 additions & 0 deletions logs.hs
@@ -0,0 +1,15 @@
{-# LANGUAGE BangPatterns #-}
module Logs where
import Data.Word
import Debug.Trace


intlog :: Word -> Word -> Word
--intlog 1 _ = error "don't be an idiot"
--intlog _ 1 = 0
intlog !my_number !my_base = gobble my_number 0
where
gobble !internal_number !accumulator
| internal_number < my_base = accumulator
| otherwise = gobble (div internal_number my_base) (succ accumulator)

37 changes: 37 additions & 0 deletions main.hs
@@ -0,0 +1,37 @@
{-# LANGUAGE ScopedTypeVariables, BangPatterns #-}
-- highest is pretty easy to get - we're always going to end up at the LCM of 1..n
-- if you go in in order, then everyone's unhappy up to n
-- but then all bets are off. damn.

-- so at that point, we need all the common multiples of all our numbers, sorted and uniqed.

-- can't get to primes > n, because no-one would go there anyway.
-- so: all products of the power set of primes < n, raised to an arbitrary power.
import Data.Numbers.Primes
import Data.List
import Logs
import Debug.Trace
import Data.Array
import Control.Parallel
import Control.Parallel.Strategies
import Data.Word
import qualified Data.Vector.Unboxed as U
import Data.Vector.Binary
import Data.Binary

solve :: U.Vector Word -> Word -> Word
solve !smallprimes !no = diff small_arr
where diff (!x) = {-# SCC "diff" #-} 1 + U.foldl' (\(!acc) (!x) -> acc + (diff_i x)) 0 x
small_arr = {-# SCC "arr" #-} U.takeWhile (<= (ceiling . sqrt $ fromIntegral no)) smallprimes
diff_i (!x) = {-# SCC "diff_i" #-} (intlog no x ) - 1

-- repa stuff
-- small_array = fromFunction (Z :. len) (\(Z :. ix) -> arr ! (Z :. ix))
-- arr_primes = {-# SCC "arr_primes" #-} fromList (Z :. len :: DIM1) smallerprimes
-- diff = {-# SCC "diff" #-} (+1) . flip (Repa.!) Z . Repa.fold (\acc -> (+acc) . diff_i) 0
main = do
primes <- {-# SCC "decode" #-} decodeFile "primes.dat" :: IO (U.Vector Word)
l <- getContents
putStr . unlines . format . parMap rseq (solve primes .read) . tail $ lines l

format n = map (\(num, s) -> "Case #" ++ show num ++ ": " ++ show s ) $ zip [1..] n

0 comments on commit 3fce0d3

Please sign in to comment.