Skip to content

Instantly share code, notes, and snippets.

@sunilnandihalli
Created August 19, 2011 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunilnandihalli/c1ee2d6397cd5b28ade5 to your computer and use it in GitHub Desktop.
Save sunilnandihalli/c1ee2d6397cd5b28ade5 to your computer and use it in GitHub Desktop.
module Main where
import qualified Data.List as L
primes::(Integral a)=>[a]
primesFrom::(Integral a)=>a->[a]->[a]
primesFrom n (f:r) = if (any (\x -> mod n x ==0) (L.takeWhile (\x-> (x*x)<=n) primes))
then primesFrom (n+f) r
else n:(primesFrom (n+f) r)
primes = let wheel = cycle [2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,8,6,4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2,4,2,10,2,10]
in [2,3,5,7] ++ primesFrom 11 wheel
primeFactorsOfNfactorial::(Integral a)=>a->[a]
primeFactorsOfNfactorial n = let numberOfFactors = \p -> sum (takeWhile (0/=) $ map (div n) (iterate (p*) p))
primeFactors = takeWhile (<=n) primes
in map numberOfFactors primeFactors
calcNumberOfWaysToGroup::(Integral a)=>a->[a]->a
calcNumberOfWaysToGroup m multiplicities = L.foldl' (\cur f->((((2|*|cur)-1)|*|f)|+|cur)) 1 multiplicities
where x |*| y = (mod x m) * (mod y m)
x |+| y = mod (x + y) m
numberOfSolutionsToEquation::(Integral a)=>a->a
numberOfSolutionsToEquation n = let frequencies = (primeFactorsOfNfactorial n)
in mod ((2 * (calcNumberOfWaysToGroup (fromIntegral 1000007) frequencies)) - 1) 1000007
main =
do nstr<-getLine
let n = read nstr
print $ numberOfSolutionsToEquation (n::Integer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment