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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:13:24+00:00 2026-06-02T01:13:24+00:00

public class EulerProblem14 { int chainLength=1; public int findChainLength(int number){ System.out.println(number +number); System.out.println(Chainlength +chainLength);

  • 0
public class EulerProblem14 {

 int chainLength=1;
public int findChainLength(int number){
    System.out.println("number "+number);
    System.out.println("Chainlength  "+chainLength);

    if(number==1){
       System.out.println("the number is finally 1 return chain length");
       return chainLength;
    }
   if(number%2==0){
       chainLength++;
     return findChainLength(number/2);
   }
   else {
       chainLength++;
       findChainLength(number*3+1);
   }
   System.out.println("THIS SHOULD NOT BE EXECUTED");
   return -1;
}
public static void main(String args[]){

    System.out.println(new EulerProblem14().findChainLength(13));

}

While solving Project Euler Problem 14, I came across a weird problem in method return in java I never faced before . In the above method when the number is finally 1 it should return the count of a chain . But this is the output for input 13 .

number 13 Chainlength 1

number 40 Chainlength 2

number 20 Chainlength 3

number 10 Chainlength 4

number 5 Chainlength 5

number 16 Chainlength 6

number 8 Chainlength 7

number 4 Chainlength 8

number 2 Chainlength 9

number 1 Chainlength 10

the number is finally 1 return chain length

THIS SHOULD NOT BE EXECUTED

THIS SHOULD NOT BE EXECUTED

-1

The problem is in the last part when the number becomes 1 instead of returning chainlength = 10 it somehow skips it and executes code which should never be executed and returns -1 . It runs fine for all powers of 2 like 1,2,4,8 but fails for others .

This is probably a stupid mistake on my part . Nonetheless it is a problem for me .

  • 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-06-02T01:13:26+00:00Added an answer on June 2, 2026 at 1:13 am

    I haven’t looked in detail, but I suspect this:

    else {
        chainLength++;
        findChainLength(number*3+1);
    }
    

    should actually be:

    else {
        chainLength++;
        return findChainLength(number*3+1);
    }
    

    You should then be able to remove the last two lines of the method entirely, as they’re unreachable.

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

Sidebar

Related Questions

public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
Public Class FooBar { private int _foo; public int Foo { get { return
public class Divers { public static void main(String args[]){ String format = |%1$-10s|%2$-10s|%3$-20s|\n; System.out.format(format,
public class Ex7 { private int fld; private void meth(int val) { fld =
public class Arrys { private int[] nums; //Step 3 public Arrys (int arrySize) {
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class Bird { private static int id = 0; private String kind; public
public class Account { public int pin; public int accountnumber; public string accounttype; public
public class upload extends Activity { private static final int CAMERA_REQUEST = 1888; private

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.