4

Here is very simple Haskell code to find all the Pythagoras integers from 1 to 200 that satisfied the Pythagoras Theorem X^2 = Y^2 + Z^2

Haskell:

let l = [1..200]
let pythagoras = [ x | x <- l, y <- l, z <- l, x^2 == y^2 + z^2]

It takes 24.1 seconds to finish it,

Swift: Using standard for loop 0.05 seconds

C: Using standard for loop 0.022 seconds

enter image description here

Aron Lee
  • 607
  • 1
  • 5
  • 8

1 Answers1

8

I measure the Haskell code running in 0.03 seconds, leading me to believe you have ran this in the interpreter (which is for development, not performant execution) instead of compiling the code. Also, you probably let the type default to Integer instead of using machine Int values.

tommd@HalfAndHalf /tmp% ghc -O2 t.hs && time ./t >/dev/null
[1 of 1] Compiling Main             ( t.hs, t.o )
Linking t ...
./t > /dev/null  0.03s user 0.00s system 87% cpu 0.040 total
tommd@HalfAndHalf /tmp% cat t.hs

main :: IO ()
main = do
  let l = [1..200] :: [Int]
  let pythagoras = [ x | x <- l, y <- l, z <- l, x^2 == y^2 + z^2]
  print pythagoras
Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166