• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java BigInteger Factorial  RSS feed

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using BigInteger I must create a program that calculates the factorials of numbers 1-30. I have to format it so that is displays as a table. Example as follows:


The code I have created is as follows:


I am not sure what is wrong with it. My compiler error is as follows:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
I am not sure what is wrong with it. My compiler error is as follows:


You actually have a few compiler errors ... but ... the one that the compiler is complaining about here is that you have mismatched parenthesis. You have one extra open parenthesis, so the compiler wasn't expecting the end of the source file.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that is a great example why code formatting and indentation matters.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

You actually have a few compiler errors ... but ... the one that the compiler is complaining about here is that you have mismatched parenthesis. You have one extra open parenthesis, so the compiler wasn't expecting the end of the source file.

Henry


It seems to only be I needed a parenthesis at the end of the code. Updated code:


Updated compiler issue:


 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you should be able to understand error messages like this.  "cannot find symbol" isn't terribly clear, so go to the next line.  This is the line of code where the error occurs.  The next line tells you which symbol it couldn't find: the variable x.  Now look at your code:

Is the variable x defined at line 6?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After these edits I have all the code correct except for one line (line 17):


I understand the compiler says:

But I don't understand where I need a parenthesis because I have them opening and closing everything, and I also don't understand where I would put ";" because I tried it in many places and even taking it out and it still said it was incorrect.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:After these edits I have all the code correct except for one line (line 17):


I understand the compiler says:



Well, that line of code, is simply *NOT* valid Java syntax. What are you trying to do with line 17?

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With line 17 I am trying to do the calculations for the factorial.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the api of BigInteger, you'll see that it says

That means that if a and b are BI's and you want the product, the syntax should be

Do you see why line 17 is wrong, and how you can repair it?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:If you look at the api of BigInteger, you'll see that it says

That means that if a and b are BI's and you want the product, the syntax should be

Do you see why line 17 is wrong, and how you can repair it?


I did this but it says it is incorrect. Everything has been defined above and I have proper parenthesis and everything.

 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this line of code:
Lexi Turgeon wrote:

n is not a BigInteger (it is an int, namely the parameter). So, this is still wrong.

Since we must calculate: n * (n-1) * ... 1 (with each factor being a BigInteger), we must start with the BigIntegerversion of n. But you have that, and called it 'big'.
So, we can use that to improve that line of code:

The question is: what to fill in for these '???'. It must be a BigInteger, so (n-1) won't do. Can we turn this (n-1) perhaps into a BigInteger?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:


Lexi Turgeon wrote:

Concentrate. Read once again what Piet suggested yesterday and look to what you're doing here.
x is a primitive data type in your case, while there should be BigInteger data type within the parenthesis.
You should be able by now at least to know how to use Java API, but you didn't need that here as Piet already gave you an answer.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I would set it up like this:



My problem is that "x" is not a BigInteger but I do not understand how to define it as one. I know I defined "n" as one but how would I do that for "x" and have it be in the "for" line.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:My problem is that "x" is not a BigInteger but I do not understand how to define it as one. I know I defined "n" as one but how would I do that for "x" and have it be in the "for" line.

Although you could modify your for() loop I might suggest overloading your factorial() method. Example:

Also, you have (x + 1) which is not what you had in your original post. And, of course it should now use 'n' and not 'x'. You'll also have to use BigInteger's add() or subtract() methods as appropriate, '+' and '-' don't work with BigInteger.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just going to call out the elephant in the room: your code formatting is horrible. If you were coloring in a coloring book, you would not only be coloring outside the lines, you'd be coloring all over the table as well.

Improperly aligned/formatted code is the inexcusable, IMO. It is the easiest thing to fix if you are using any modern IDE like NetBeans or Eclipse; just hit Ctrl+Shift+F and you can make it so much easier for you and others to see the structure and organization of the logic in your code.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Although you could modify your for() loop I might suggest overloading your factorial() method. Example:

Overloading: Having more than one method with the same name but taking different parameters. The compiler will examine the parameter types when calling a method and determine which (if any) is the best match. By overloading factorial() as I've shown here, your for() loop will call the version that takes an "int". This method will in turn call the one that takes a "BigInteger". This keeps your for() loop nice and clean.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, BigInteger doesn't have a constructor that takes an int. Try this:
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest overloading factorial() to keep your for() loop clean. Here's how an overloaded loop compares to a non-overloaded one.

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I suggest overloading factorial() to keep your for() loop clean. Here's how an overloaded loop compares to a non-overloaded one.



So should I put this in my factorial method?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
Carey Brown wrote:I suggest overloading factorial() to keep your for() loop clean. Here's how an overloaded loop compares to a non-overloaded one.
So should I put this in my factorial method?

One or the other of these loops should go into main(). I recommend using the first (which I believe is what you already have) but this would require adding the overloaded factorial(int) method as I posted previously.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, why going the overload way, I wonder.

In 'factorial', OP is already calculating 'big' as BigInteger of n, and since 'factorial' returns a BigInteger, you could simply end with:

and get a nice recursion into the bargain. But what o what do we need to fill in for these '???' ?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, he's converting 'n' to 'big' but doing so over and over rather than converting once then staying in the world of BigInteger.

Either way...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!