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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:06:11+00:00 2026-05-26T11:06:11+00:00

works:meaning no precision error::Just array out of bounds long a[] =new long[1000000]; int no=2,n;

  • 0

works:meaning no precision error::Just array out of bounds

long a[] =new long[1000000];
int no=2,n;
long i;
a[no]=i+a[n];
if(a[no]>longChain)
{
    longChain = a[no];
    startNo = no;
}

and when I do

long a[] =new long[1000000];
long no=2,n;
long i,longChain=1,startNo;
a[no]=i+a[n];
if(a[no]>longChain)
{
    longChain = a[no];
    startNo = no;
}

then loss of precision

found:long

required: int

what is the problem?

My Code for above problem, its ProjectEuler Problem No. 14

class P14
{
    public static void main(String args[])
    {
        long a[] =new long[1000000];
        long no=2,n;
        long i,longChain=1,startNo;
        a[1]=1;
        while(no<1000000)
        {
            n=no;
            i=0;
            while(n>no-1)
            {
                if(n%2==0)
                    n=n/2;
                else
                    n=3*n+1;
                i++;
                //System.out.println(n);
            }
            a[no]=i+a[n];
            if (a[no] > longChain)
            {
                longChain=a[no];
                startNo=no;
            }
            no++;
            //System.out.println(no);
        }
    }
}

This is my code for where above problem is occurring

Answer:: Replace a[no] by a[(int)n]

a[n],a[(int)n]

  • 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-26T11:06:12+00:00Added an answer on May 26, 2026 at 11:06 am

    your no and n variables need to be int, not long. Arrays can’t be indexed by long. Changing the code to:

    int no=2,n;
    

    makes the code compile.

    The ArrayIndexOutOfBoundsException is because you wrote the algorithm assuming it was longs.

    This code will eventually cause n to become negative:

    n=3*n+1;
    

    It’s hard to see why when you’re doing integer arithmetic. A slight change to make the code use long arithmetic and print the interim result reveals exactly when it becomes negative and how:

    while(n>no-1)
    {
        long newN = n;
    
        if (n % 2 == 0) newN = newN / 2L;
        else newN = 3L * newN + 1L;
    
        if (newN > Integer.MAX_VALUE) System.out.println("about to fail");
    
        //this is where the magic happens
        n = (int)newN;
        System.out.println("converted " + newN + " to " + n);
    
        i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out the exact meaning of the words Covariance and
This works just fine: protected void txtTest_Load(object sender, EventArgs e) { if (sender is
I'm trying to make a table that works like Excel. Meaning, when a user
Someone who understands how rails caching works can really help me out here. Here's
So, i'm trying to understand how the SVM algorithm works but i just cannot
I am really struggling to understand the meaning of the error below. I develop
I'm just getting started in OO javascript so please bear with me. This works:
I'm using a simple line of jquery to load a new song meaning when
But it doesn't work -meaning the java code is not executed. Although the batch
What does this meaning mean in words? (SomeVariable * 330UL >> 10) Is it:

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.