Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:10:35+00:00 2026-06-14T06:10:35+00:00

Consider the modified Euler problem #4 — Find the maximum palindromic number which is

  • 0

Consider the modified Euler problem #4 — “Find the maximum palindromic number which is a product of two numbers between 100 and 9999.”

rev :: Int -> Int
rev x = rev' x 0

rev' :: Int -> Int -> Int
rev' n r
    | n == 0 = r
    | otherwise = rev' (n `div` 10) (r * 10 + n `mod` 10)

pali :: Int -> Bool
pali x = x == rev x

main :: IO ()
main = print . maximum $ [ x*y | x <- nums, y <- nums, pali (x*y)]
    where
        nums = [9999,9998..100]
  • This Haskell solution using -O2 and ghc 7.4.1 takes about 18
    seconds
    .
  • The similar C solution takes 0.1 second.

So Haskell is 180 times
slower. What’s wrong with my solution? I assume that this type of
problems Haskell solves pretty well.

Appendix – analogue C solution:

#define A   100
#define B   9999
int ispali(int n)
{
    int n0=n, k=0;
    while (n>0) {
        k = 10*k + n%10;
        n /= 10;
    }
    return n0 == k;
}
int main(void)
{
    int max = 0;
    for (int i=B; i>=A; i--)
        for (int j=B; j>=A; j--) {
            if (i*j > max && ispali(i*j))
                max = i*j;      }
    printf("%d\n", max);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:10:36+00:00Added an answer on June 14, 2026 at 6:10 am

    The similar C solution

    That is a common misconception.

    Lists are not loops!

    And using lists to emulate loops has performance implications unless the compiler is able to eliminate the list from the code.

    If you want to compare apples to apples, write the Haskell structure more or less equivalent to a loop, a tail recursive worker (with strict accumulator, though often the compiler is smart enough to figure out the strictness by itself).

    Now let’s take a more detailed look. For comparison, the C, compiled with gcc -O3, takes ~0.08 seconds here, the original Haskell, compiled with ghc -O2 takes ~20.3 seconds, with ghc -O2 -fllvm ~19.9 seconds. Pretty terrible.

    One mistake in the original code is to use div and mod. The C code uses the equivalent of quot and rem, which map to the machine division instructions and are faster than div and mod. For positive arguments, the semantics are the same, so whenever you know that the arguments are always non-negative, never use div and mod.

    Changing that, the running time becomes ~15.4 seconds when compiling with the native code generator, and ~2.9 seconds when compiling with the LLVM backend.

    The difference is due to the fact that even the machine division operations are quite slow, and LLVM replaces the division/remainder with a multiply-and-shift operation. Doing the same by hand for the native backend (actually, a slightly better replacement taking advantage of the fact that I know the arguments will always be non-negative) brings its time down to ~2.2 seconds.

    We’re getting closer, but are still a far cry from the C.

    That is due to the lists. The code still builds a list of palindromes (and traverses a list of Ints for the two factors).

    Since lists cannot contain unboxed elements, that means there is a lot of boxing and unboxing going on in the code, that takes time.

    So let us eliminate the lists, and take a look at the result of translating the C to Haskell:

    module Main (main) where
    
    a :: Int
    a = 100
    
    b :: Int
    b = 9999
    
    ispali :: Int -> Bool
    ispali n = go n 0
      where
        go 0 acc = acc == n
        go m acc = go (m `quot` 10) (acc * 10 + (m `rem` 10))
    
    maxpal :: Int
    maxpal = go 0 b
      where
        go mx i
            | i < a = mx
            | otherwise = go (inner mx b) (i-1)
              where
                inner m j
                    | j < a = m
                    | p > m && ispali p = inner p (j-1)
                    | otherwise = inner m (j-1)
                      where
                        p = i*j
    
    main :: IO ()
    main = print maxpal
    

    The nested loop is translated to two nested worker functions, we use an accumulator to store the largest palindrome found so far. Compiled with ghc -O2, that runs in ~0.18 seconds, with ghc -O2 -fllvm it runs in ~0.14 seconds (yes, LLVM is better at optimising loops than the native code generator).

    Still not quite there, but a factor of about 2 isn’t too bad.

    Maybe some find the following where the loop is abstracted out more readable, the generated core is for all intents and purposes identical (modulo a switch of argument order), and the performance of course the same:

    module Main (main) where
    
    a :: Int
    a = 100
    
    b :: Int
    b = 9999
    
    ispali :: Int -> Bool
    ispali n = go n 0
      where
        go 0 acc = acc == n
        go m acc = go (m `quot` 10) (acc * 10 + (m `rem` 10))
    
    downto :: Int -> Int -> a -> (a -> Int -> a) -> a
    downto high low acc fun = go high acc
      where
        go i acc
            | i < low   = acc
            | otherwise = go (i-1) (fun acc i)
    
    maxpal :: Int
    maxpal = downto b a 0 $ \m i ->
                downto b a m $ \mx j ->
                    let p = i*j
                    in if mx < p && ispali p then p else mx
    
    main :: IO ()
    main = print maxpal
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following simple example class, which has a property that exposes a modified
Consider the following VBScript which, when run, lists all the files in the current
Possible Duplicate: How do I find the last modified file in a directory in
i need to know which part of the text is been modified by the
Consider the following example : $ls base.txt base-modified.txt $diff base.txt base-modified.txt > diff.txt $rm
Consider a huge CSV with the following structure (modified for simplicity): ID, NAME, ADDRESS,
Consider the following code, which takes place in a background thread (thread B): List<T>
Consider a loop in C which declares a character array in the loop's body.
Problem Description Consider the case maven is being used on hudson. Now someone took
Consider the following situation. We have a database which stores writers and books in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.