Jump to content

out of range????

- - - - -

  • Please log in to reply
9 replies to this topic

#1
enigmas

enigmas

    Newbie

  • Members
  • PipPip
  • 20 posts
So im working on a piece of recursive code that is supposed to find the GCD of 2 huge numbers. I was instructed to use BigInteger and as far as I understand BigInteger is only limited by your memory. However in my Program I get an error telling me that my value is out of range for an int. Could someone help me out please.

import java.math.BigInteger;
public class GCD {

public static void main(String[] args) {
GCD testGCD = new GCD();

BigInteger n = BigInteger.valueOf(9384041234832094823094898230);//<---this is giving me a problem
//BigInteger second = BigInteger.valueOf(3033092482498243802498); //<---and this

testGCD.gcdTest(5,6);
}
public static long gcdTest (int n, int m)
{
if (m==0)
{
System.out.println("n: "+n);
System.out.println("m: "+m);

return (n);
}
else
System.out.println("n: "+n);
System.out.println("m: "+m);
return gcdTest(m,n%m);
}

}

Attached Files

  • Attached File  GCD.java   564bytes   26 downloads


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Don't use int at all, just BigInteger.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
enigmas

enigmas

    Newbie

  • Members
  • PipPip
  • 20 posts
That is not the problem. Those ints are only getting the small numbers (5,6). The error happens when I declare the BigInteger n. It says that it is out of range.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
BigInteger's constructor takes a String as parameter.

#5
enigmas

enigmas

    Newbie

  • Members
  • PipPip
  • 20 posts
are you sure? I tried giving it quotations to turn it into a string and it just gave me more errors.

This is the error I get with the code I posted first


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The literal 9384041234832094823094898230 of type int is out of range

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
BigInteger.valueOf("9384041234832094823094898230");
That's a String.

#7
enigmas

enigmas

    Newbie

  • Members
  • PipPip
  • 20 posts
Yes I realize that. But when I do change it to that it gives me this error


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
BigIngeter cannot be resolved

#8
enigmas

enigmas

    Newbie

  • Members
  • PipPip
  • 20 posts
There we go I fixed it, it did need a string but I had to change it around a bit. I replaced it with this

BigInteger n = new BigInteger("9384041234832094823094898230");

#9
enigmas

enigmas

    Newbie

  • Members
  • PipPip
  • 20 posts
I got another question. I have another task to do and it is asking me: instead of using bigInteger I am to store the number in a linkedlist. Each number is supposed to be in a node. So I have set up my Linked list in a different class and I have the majority of the code ready to go. However I am stuck at one part. How am I supposed to input his large number into the linked list. I'm not supposed to use user input I believe but I will get clarification on that soon. I was thinking of storing the number in an array and then using a for loop to put it into the linked list. Im not sure if Im allowed to do that but I cant think of anything else. Does anyone else have any better ideas?

#10
enigmas

enigmas

    Newbie

  • Members
  • PipPip
  • 20 posts
NVM i figured out how im gonna do it. Store it in a string and then put it into the linked list.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users