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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:46:53+00:00 2026-05-24T01:46:53+00:00

I am working on a projecteuler problem, and I have run into an OutOfMemoryError

  • 0

I am working on a projecteuler problem, and I have run into an OutOfMemoryError.
And I don’t understand why because my code was working beautifully (to my novice eyes at least :P).
Everything works just fine until the loop reaches 113383.
If someone could help me debug this it would be greatly appreciated because I don’t understand at all why it is failing.

My Code

import java.util.Map;
import java.util.HashMap;
import java.util.Stack;

/*
* The following iterative sequence is defined for the set of positive integers:
n = n/2 (n is even)
n = 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13  40  20  10  5  16  8  4  2  1
It can be seen that this sequence (starting at 13 and finishing at 1) contains
10 terms. Although it has not been proved yet (Collatz Problem), it is thought 
that all starting numbers finish at 1. Which starting number, under one million,
produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
*/
public class Problem14 {

    static Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    final static int NUMBER = 1000000;

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        map.put(1, 1);      
        for (int loop = 2; loop < NUMBER; loop++) {
            compute(loop);
            //System.out.println(map);
            System.out.println(loop);
        }       
        long end = System.currentTimeMillis();
        System.out.println("Time: " + ((end - start) / 1000.) + " seconds" );
    }

    private static void compute(int currentNumber) {
        Stack<Integer> temp = new Stack<Integer>();
        /**
         * checks to see if current value is already 
         * part of the map. if it isn't, add to temporary
         * stack. also if current value exceeds 1 million,
         * don't check map or add to stack.
         */
        while (!map.containsKey(currentNumber)){
            temp.add(currentNumber);
            currentNumber = realCompute(currentNumber);
            while (currentNumber > NUMBER){
                currentNumber = realCompute(currentNumber);
            }
        }
        System.out.println(temp);
        /**
         * adds members of the temporary stack to the map
         * based on when they were placed in the stack
         */
        int initial = map.get(currentNumber) + 1;
        int size = temp.size();

        for (int loop = 1; loop <= size; loop++){
            map.put(temp.pop(), initial);
            initial++;
            //key is the top of stack
            //value is initially 1 greater than the current number that was
            //found at the map, then incremented by 1 afterwards;
        }
    }

    private static int realCompute(int currentNumber) {

        if (currentNumber % 2 == 0) {
            currentNumber /= 2;
        } else {
            currentNumber = (currentNumber * 3) + 1;
        }
        return currentNumber;
    }
}
  • 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-24T01:46:54+00:00Added an answer on May 24, 2026 at 1:46 am

    Seems to me it’s just what the error says: you’re running out of memory. Increase the heap size with -Xmx (eg java -Xmx512m).

    If you want to reduce the memory footprint, take a look at GNU Trove for a more compact (and faster) implementation of primitive maps (instead of using HashMap<Integer,Integer>).

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

Sidebar

Related Questions

I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I've run into something odd, here, and I'm at a loss -- I have
Working on a project at the moment and we have to implement soft deletion
I'm trying to work through the problems on projecteuler.net but I keep running into
I have been receiving errors when trying to save and run this Python 3.1
I'm working on a Project Euler problem which requires factorization of an integer. I
I'm working on Project Euler problem #2 : Each new term in the Fibonacci
Working on trying out the market licensing service, and I'm running into a few
Working with spring, below, the code of applicationContext-service.xml: <bean id=mediaObjectService class=path.MediaObjectServiceImpl> <property name=mediaObjectDao >
I've been slowly working my way through the list of Project Euler problems, 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.