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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:30:30+00:00 2026-05-23T14:30:30+00:00

I have a working program to compute the longest collatz chain in a given

  • 0

I have a working program to compute the longest collatz chain in a given range (project euler n°14). I think it works correctly, but is very slow. I tried to look for a better solution, but I can only reduce slightly the evaluated domain. Am I doing something wrong?

The implementation use memoization to avoid computing the same result twice. Is Data.Map bad for general performances?

import Data.Map ((!), member, insert, singleton, assocs, Map)

insertSolution::Integer->(Map Integer Integer)->(Map Integer Integer)
insertSolution n syracMap
    | n `member` syracMap = syracMap
    |otherwise = let
        next = if n `mod` 2 == 0 then n `div` 2 else 3 * n + 1
        newMap = insertSolution next syracMap
        solution = newMap ! next + 1
        in insert n solution newMap

bound = 1::Integer
lower = 999999::Integer

test::[Integer]
test = [lower,lower+2..bound]

values = takeWhile (\(k, v) -> k < bound) $ assocs $ foldr insertSolution (singleton 1 1) test

result = foldr (\(k, v) (k', v') -> if v > v' then (k, v) else (k', v')) (1, 1) values

main = putStr $ show $ result

edit

updated function to remove bug. It is still pretty slow on my laptop.

  • 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-23T14:30:31+00:00Added an answer on May 23, 2026 at 2:30 pm

    FWIW, here’s my solution:

    module Main
        where
    
    import Data.List
    import Data.Ord
    
    next_hailstone n | even n = n `div` 2
                     | otherwise = 3*n+1
    
    gen_next_hailstone n
        = if nh == 1
          then Nothing
          else Just (nh, nh)
              where nh = next_hailstone n
    
    hailstone n = unfoldr gen_next_hailstone n
    
    hailstone_seqs = map hailstone [1..1000000]
    
    zip_hailstone = zip [1..1000000] hailstone_seqs
    
    max_hailstone = maximumBy (comparing (length . snd)) zip_hailstone
    
    main = print . fst $ max_hailstone
    

    It’s relatively fast. If you want more speed, consult the Haskell wiki (SPOILER ALERT!!!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on kind of drawing program but I have a problem with
Some explanation: for a project I'm working on I have to write a program
I have a working program in C++ that generates data for a Mandelbrot Set.
I have been working on a childish little program: there are a bunch of
I have a program in Perl I'm working on where I would need multiple
I have a program that I'm working on that register global hotkeys I would
I have a requirement for a program I'm working on to store job numbers
I'm working on a vbscript program and I have got and Expected Statement Error.
I have a simple Delphi program that I'm working on, in which I am
I'm working on a C# program, and right now I have one Form and

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.