
Using bang patterns didn't help almost anything here. Using rem instead of mod made the time go from 45s to 40s. Now, using -fvia-C really helped (when I used rem but not using mod). It went down to 10s.
Bang patterns should have helped tons - it isn't GHC thats at fault here and yes it does tco. I attached a version w/ excessive bangs below. Did you compile with "ghc --make -O3 -fforce-recomp"? Cheers, Thomas main = print $ rangeI 0 0 rangeK :: Int -> Int -> Int -> Int -> Int rangeK !i !j !k !acc = if k < 1000 then if i * i + j * j + k * k `rem` 7 == 0 then rangeK i j (k+1) (acc+1) else rangeK i j (k+1) acc else acc rangeJ :: Int -> Int -> Int -> Int rangeJ !i !j !acc = if j < 1000 then rangeJ i (j+1) (acc + rangeK i j 0 0) else acc rangeI :: Int -> Int -> Int rangeI !i !acc = if i < 1000 then rangeI (i+1) (acc + (rangeJ i 0 0)) else acc