A comparison of Haskell, Julia and Picat on a Euler Project problem

88 views
Skip to first unread message

Neng-Fa Zhou

unread,
Dec 4, 2023, 10:57:07 PM12/4/23
to Picat
There have been several comparisons of Picat with other languages. Today,  my class on PL did a comparison of Haskell, Julia and  Picat on the 206th Euler Project problem, which asks for the unique positive integer whose square has the form  1_2_3_4_5_6_7_8_9_0, where each "_" is a single digit.  The programs are attached below. As can be seen,  thanks to be built-in backtracking and unification features, the Picat program is much shorter than their counterparts in Haskell and Julia. In terms of execution CPU time,  which includes both compile and execution times, Picat is also competitive on this problem.

Julia : 0.136s
Haskell: 0.111s
Picat: 0.019

This is surprising, because I expected that failure-driven backtracking search would be slower than a loop.
 
Cheers,
NF

===== Julia =====

function euler()

    x = floor(Int, sqrt(19293949596979899.0))

    while !check(x)

x -= 1

    end

    return x*10

end


function check(x)

    str = string(x*x)

    if length(str) != 17

        return false

    end

    d1,_,d2,_,d3,_,d4,_,d5,_,d6,_,d7,_,d8,_,d9 = str

    return d1 == '1' && d2 == '2' && d3 == '3' &&

           d4 == '4' && d5 == '5' && d6 == '6' &&

           d7 == '7' && d8 == '8' && d9 == '9'

end


println(euler())


===== Haskell =====


main = putStrLn (show euler)


euler =

    search (floor (sqrt 19293949596979899))


search x

    | check (show (x * x)) = (x * 10)

    | otherwise = search (x - 1)


check ['1',_,'2',_,'3',_,'4',_,'5',_,'6',_,'7',_,'8',_,'9'] = True

check _ = False


===== Picat =====


main =>

    Start = floor(sqrt(19293949596979899)),

    between(-Start, 0, X),

    to_string(X*X) = ['1',_,'2',_,'3',_,'4',_,'5',_,'6',_,'7',_,'8',_,'9'],

    println(-X*10).


Neng-Fa Zhou

unread,
Dec 7, 2023, 5:46:57 PM12/7/23
to Picat
After my post, Hakan wrote several programs in Picat, including ones that use constraints.

http://hakank.org/picat/euler_206.pi

The programs are very illustrative of Picat's features. Thank you Hakan.

Cheers,
NF

Reply all
Reply to author
Forward
0 new messages