Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java BigInteger Factorial

 
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:

 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Sheriff
Posts: 7708
534
Mac OS X VI Editor BSD Linux
  • 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:


 
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Bartender
Posts: 4067
156
  • 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
Bartender
Posts: 4067
156
  • 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: 7708
534
Mac OS X VI Editor BSD Linux
  • 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
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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.
 
Marshal
Posts: 15884
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
Bartender
Posts: 4067
156
  • 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
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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...
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic