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

  • Home
  • SEARCH
  • 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 309831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:46:40+00:00 2026-05-12T07:46:40+00:00

Ok, referring back to my previous question, I am still working on learning haskell

  • 0

Ok, referring back to my previous question, I am still working on learning haskell and solving the current problem of finding the longest chain from the following iteration:

 chain n    | n == 0       = error "What are you on about?"
        | n == 1       = [1]
        | rem n 2 == 0 = n : chain (n `div` 2) 
        | otherwise    = n : chain (3 * n + 1)

I have this bit sorted, but I need to find the longest chain from a starting number below 1,000,000. So how do I make it do each starting number up to 1,000,000 and then print the one with the longest chain length.
I can do it for one example with:

Main> length (chain n)

I assume I need the output as an array and then use the maximum function to find the value largest chain length and then see how far along it is in the array of answers.

Is this a good way to go about finding a solution or is there a better way (perhaps with better efficiency)?

  • 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-12T07:46:40+00:00Added an answer on May 12, 2026 at 7:46 am

    You are right about the maximum part. To get the list (that’s what Haskell’s []s are, arrays are different structures) you need to use the map higher-order function, like this:

    chainLength n = length (chain n)
    
    lengths = map chainLength [1..1000000]
    

    Essentially, map takes as arguments a function and a list. It applies the function to each element in the list and returns the list of the results.

    Since you will be needing the number whose chain has that length, you may want change the chainLength function to return the number as well, like this:

    chainLength n = (n, length (chain n))
    

    That way you will have an array of pairs, with each number and its chain length.

    Now you need to get the pair with the largest second component. That’s where the maximumBy function comes in. It works just like maximum but takes a function as a parameter to select how to compare the values. In this case, the second component of the pair. This comparison function takes two numbers and returns a value of type Ordering. This type has only three possible values: LT, EQ, GT, for less than, equal, and greater than, respectively.

    So, we need a function that given two pairs tells us how the second components compare to each other:

    compareSnd (_, y1) (_, y2) = compare y1 y2
    -- Or, if you import Data.Function, you can write it like this (thanks alexey_r):
    compareSnd = compare `on` snd                  -- reads nicely
    

    I used the default compare function that compares numbers (well, not just numbers).

    Now we only need to get the maximum using this function:

    longestChain = maximumBy compareSnd lengths
    

    That gets you a pair of the number with the longest chain and the corresponding length. Feel free to apply fst and snd as you please.

    Note that this could be more much more concisely using zip and composition, but since you tagged the question as newbie, I thought it better to break it down like this.

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

Sidebar

Related Questions

No related questions found

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.