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 7657391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:01:14+00:00 2026-05-31T13:01:14+00:00

I’m currently working on project euler problem 14 . I solved it using a

  • 0

I’m currently working on project euler problem 14.

I solved it using a poorly coded program, without memoization, that took 386 5 seconds to run (see edit).

Here it is:

step :: (Integer, Int) -> Integer -> (Integer, Int)
step (i, m) n   | nextValue > m         = (n, nextValue)
                | otherwise             = (i, m)
                where nextValue = syr n 1

syr :: Integer -> Int -> Int
syr 1 acc   = acc
syr x acc   | even x    = syr (x `div` 2) (acc + 1)
            | otherwise = syr (3 * x + 1) (acc + 1)

p14 = foldl step (0, 0) [500000..999999]

My question is about several comments in the thread related to this problem, where were mentionned execution times of <1 s for programs as follow (C code, credits to the project euler forum user ix for the code — note: I didn’t check that the execution time is in fact as mentionned):

#include <stdio.h>


int main(int argc, char **argv) {
    int longest = 0;
    int terms = 0;
    int i;
    unsigned long j;
    for (i = 1; i <= 1000000; i++) {
        j = i;
        int this_terms = 1;
        while (j != 1) {
            this_terms++;
            if (this_terms > terms) {
                terms = this_terms;
                longest = i;
            }
            if (j % 2 == 0) {
                j = j / 2;
            } else {
                j = 3 * j + 1;
            }
        }
    }
    printf("longest: %d (%d)\n", longest, terms);
    return 0;
}

To me, those programs are kind of the same, when talking about the algorithm.

So I wonder why there is such a big difference? Or is there any fondamental difference between our two algorithms that can justify a x6 factor in performance?

BTW, I’m currently trying to implement this algorithm with memoization, but am kind of lost as to me, it’s way easier to implement in an imperative language (and I don’t manipulate monads yet so I can’t use this paradigm). So if you have any good tutorial that fits a beginner to learn memoization, I’ll be glad (the ones I encountered were not detailed enough or out of my league).

Note: I came to declarative paradigm through Prolog and am still in the very early process of discovering Haskell, so I might miss important things.

Note2: any general advice about my code is welcome.

EDIT: thanks to delnan’s help, I compiled the program and it now runs in 5 seconds, so I mainly look for hints on memoization now (even if ideas about the existing x6 gap are still welcome).

  • 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-05-31T13:01:15+00:00Added an answer on May 31, 2026 at 1:01 pm

    After having compiled it with optimisations, there are still several differences to the C programme

    • you use div, while the C programme uses machine division (which truncates) [but any self-respecting C compiler transforms that into a shift, so that makes it yet faster], that would be quot in Haskell; that reduced the run time by some 15% here.
    • the C programme uses fixed-width 64-bit (or even 32-bit, but then it’s just luck that it gets the correct answer, since some intermediate values exceed 32-bit range) integers, the Haskell programme uses arbitrary precision Integers. If you have 64-bit Ints in your GHC (64-bit OS other than Windows), replace Integer with Int. That reduced the run time by a factor of about 3 here. If you’re on a 32-bit system, you’re out of luck, GHC doesn’t use native 64-bit instructions there, these operations are implemented as C calls, that’s still rather slow.

    For the memoisation, you can outsource it to one of the memoisation packages on hackage, the only one that I remember is data-memocombinators, but there are others. Or you can do it yourself, for example keeping a map of previously calculated values – that would work best in the State monad,

    import Control.Monad.State.Strict
    import qualified Data.Map as Map
    import Data.Map (Map, singleton)
    
    type Memo = Map Integer Int
    
    syr :: Integer -> State Memo Int
    syr n = do
        mb <- gets (Map.lookup n)
        case mb of
          Just l -> return l
          Nothing -> do
              let m = if even n then n `quot` 2 else 3*n+1
              l <- syr m
              let l' = l+1
              modify (Map.insert n l')
              return l'
    
    solve :: Integer -> Int -> Integer -> State Memo (Integer,Int)
    solve maxi len start
        | len > 1000000 = return (maxi,len)
        | otherwise = do
             l <- syr start
             if len < l
                 then solve start l (start+1)
                 else solve maxi len (start+1)
    
    p14 :: (Integer,Int)
    p14 = evalState (solve 0 0 500000) (singleton 1 1)
    

    but that will probably not gain too much (not even when you’ve added the necessary strictness). The trouble is that a lookup in a Map is not too cheap and an insertion is relatively expensive.

    Another method is to keep a mutable array for the lookup. The code becomes more complicated, since you have to choose a reasonable upper bound for the values to cache (should be not much larger than the bound for the starting values) and deal with the parts of the sequences falling outside the memoised range. But an array lookup and write are fast. If you have 64-bit Ints, the below code runs pretty fast, here it takes 0.03s for a limit of 1 million, and 0.33s for a limit of 10 million, the corresponding (as closely as I reasonably could) C code runs in 0.018 resp. 0.2s.

    module Main (main) where
    
    import System.Environment (getArgs)
    import Data.Array.ST
    import Data.Array.Base
    import Control.Monad.ST
    import Data.Bits
    import Data.Int
    
    main :: IO ()
    main = do
        args <- getArgs
        let bd = case args of
                   a:_ -> read a
                   _   -> 100000
        print $ collMax bd
    
    next :: Int -> Int
    next n
        | n .&. 1 == 0  = n `unsafeShiftR` 1
        | otherwise     = 3*n + 1
    
    collMax :: Int -> (Int,Int16)
    collMax upper = runST $ do
        arr <- newArray (0,upper) 0 :: ST s (STUArray s Int Int16)
        let go l m
                | upper < m = go (l+1) $ next m
                | otherwise = do
                    l' <- unsafeRead arr m
                    case l' of
                      0 -> do
                          l'' <- go 1 $ next m
                          unsafeWrite arr m (l'' + 1)
                          return (l+l'')
                      _ -> return (l+l'-1)
            collect mi ml i
                | upper < i = return (mi, ml)
                | otherwise = do
                    l <- go 1 i
                    if l > ml
                      then collect i l (i+1)
                      else collect mi ml (i+1)
        unsafeWrite arr 1 1
        collect 1 1 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

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.