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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:08:36+00:00 2026-05-18T02:08:36+00:00

This is actually a solution to Project Euler Problem 14 in F#. However, I’m

  • 0

This is actually a solution to Project Euler Problem 14 in F#. However, I’m running into a System.OutOfMemory exception when attempting to calculate an iterative sequence for larger numbers. As you can see, I’m writing my recursive function with tail calls.

I was running into a problem with StackOverFlowException because I was debugging in visual studio (which disables the tail calls). I’ve documented that in another question. Here, I’m running in release mode–but I’m getting out of memory exceptions when I run this as a console app (on windows xp with 4gb ram).

I’m really at a loss to understand how I coded myself into this memory overflow & hoping someone can show my the error in my ways.

let E14_interativeSequence x =

  let rec calc acc startNum =
    match startNum with
    | d when d = 1      -> List.rev (d::acc)
    | e when e%2 = 0    -> calc (e::acc) (e/2)
    | _                 -> calc (startNum::acc) (startNum * 3 + 1)

  let maxNum pl=

    let rec maxPairInternal acc pairList =
        match pairList with
        | []        ->  acc
        | x::xs     ->  if (snd x) > (snd acc) then maxPairInternal x xs
                        else maxPairInternal acc xs

    maxPairInternal (0,0) pl
    |> fst

  // if I lower this to like [2..99999] it will work.
  [2..99999] 
  |> List.map (fun n -> (n,(calc [] n)))
  |> List.map (fun pair -> ((fst pair), (List.length (snd pair))))
  |> maxNum
  |> (fun x-> Console.WriteLine(x))

EDIT

Given the suggestions via the answers, I rewrote to use a lazy list and also to use Int64’s.

#r "FSharp.PowerPack.dll"

let E14_interativeSequence =

  let rec calc acc startNum =
    match startNum with
    | d when d = 1L         -> List.rev (d::acc) |> List.toSeq
    | e when e%2L = 0L      -> calc (e::acc) (e/2L)
    | _                     -> calc (startNum::acc) (startNum * 3L + 1L)

  let maxNum (lazyPairs:LazyList<System.Int64*System.Int64>) =

    let rec maxPairInternal acc (pairs:seq<System.Int64*System.Int64>) =
        match pairs with
        | :? LazyList<System.Int64*System.Int64> as p ->
            match p with
            | LazyList.Cons(x,xs)->  if (snd x) > (snd acc) then maxPairInternal x xs
                                     else maxPairInternal acc xs
            | _                         ->  acc
        | _ -> failwith("not a lazylist of pairs")

    maxPairInternal (0L,0L) lazyPairs
    |> fst

  {2L..999999L}
  |> Seq.map (fun n -> (n,(calc [] n)))
  |> Seq.map (fun pair -> ((fst pair), (Convert.ToInt64(Seq.length (snd pair)))))
  |> LazyList.ofSeq
  |> maxNum

which solves the problem. I’d also look at Yin Zhu’s solution which is better, though.

  • 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-18T02:08:36+00:00Added an answer on May 18, 2026 at 2:08 am

    As mentioned by Brian, List.* operations are not appropriate here. They cost too much memory.

    The stackoverflow problem comes from another place. There are two possible for you to have stackoverflow: calc and maxPairInternal. It must be the first as the second has the same depth as the first. Then the problem comes to the numbers, the number in 3n+1 problem could easily go to very large. So you first get a int32 overflow, then you get a stackoverflow. That’s the reason. After changing the numbers to 64bit, the program works.

    Here is my solution page, where you can see a memoization trick.

    open System
    let E14_interativeSequence x =
    
      let rec calc acc startNum =
        match startNum with
        | d when d = 1L      -> List.rev (d::acc)
        | e when e%2L = 0L    -> calc (e::acc) (e/2L)
        | _                 -> calc (startNum::acc) (startNum * 3L + 1L)
    
      let maxNum pl=
    
        let rec maxPairInternal acc pairList =
            match pairList with
            | []        ->  acc
            | x::xs     ->  if (snd x) > (snd acc) then maxPairInternal x xs
                            else maxPairInternal acc xs
    
        maxPairInternal (0L,0) pl
        |> fst
    
      // if I lower this to like [2..99999] it will work.
      [2L..1000000L] 
      |> Seq.map (fun n -> (n,(calc [] n)))
      |> Seq.maxBy (fun (n, lst) -> List.length lst)
      |> (fun x-> Console.WriteLine(x))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Premise This problem has a known solution (shown below actually), I'm just wondering if
I use CMake to generate a Visual Studio 2010 project and solution file. Actually
I am doing problem 68 at project euler and came up with the following
I have two projects in my solution. First project is web site (actually web
I have a C# solution for a web project (actually it's an Orchard CMS
I'm trying to work through Project Euler and I'm hitting a barrier on problem
I have a C# solution with this structure: Project 1: Contains a Dao (can
I've been trying to work my way through Problem 27 of Project Euler, but
Simple question - what does this actually do? var oq = (ObjectQuery<TEntity>)L2EQuery; return ExecuteFirstorDefault<TEntity>(oq,
I have seen code like this (actually seeing another person type it up): catch

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.