Pascal's Triangle in Java

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Ok, I'm supposed to make a code that will give me the number in Pascal's triangle based on the row and column values I give it.

I have everything working well up until something like (50, 4), which gives me a "divide by zero" error. I understand this is because int values can only go up to 2 billion (or something) before things get weird.

Changing it to doubles fixed it...but apparently I'm not allowed to use doubles for this code (needs to be int).

I'm also not supposed to use division in the code...even though the super easy formula to find a value if given the row and column is row!/(col!(row-col)!). I had to make a factorial helper for this.

I'm not exactly sure what else to do. Obviously I'm just missing some sort of method or algorithm to go about doing this problem some other way.

So, I guess this isn't a total programming question...but I didn't know where else to put it.

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
do you actually try to calculate "row!"? you don't need to because you can cancel out some of the calculation in the denominator.
 
Sep 29, 2004
18,665
67
91
You are welcome unless you actually need to use the int primitive type.

OK, this is homework. If you need to supoprt this:
"I have everything working well up until something like (50, 4)"
how high do you have to go? Eventually the program will die if you get into numbers that are to high.

What class is this for? Intro to Java? Algorithms or some senior level class?
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Originally posted by: oog
do you actually try to calculate "row!"? you don't need to because you can cancel out some of the calculation in the denominator.

Even then (which I don't think you actually can), you still have to divide something, which I'm apparently not supposed to have to do.


Originally posted by: IHateMyJob2004
You are welcome unless you actually need to use the int primitive type.

OK, this is homework. If you need to supoprt this:
"I have everything working well up until something like (50, 4)"
how high do you have to go? Eventually the program will die if you get into numbers that are to high.

What class is this for? Intro to Java? Algorithms or some senior level class?

I have to use int. The highest test case we were given was (50, 4), but there should be no technical limit.

This is for C212, which is basically "Computer Science 2". 2nd CS class I've taken (I'm a freshman).

Edit: Name of the course is "Intro to Software Systems"
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Update: Here's what my AI told me:

"You don't need to use the division operator to solve this problem. Your method should also not be returning a double. You may want to change how you visualize the triangle. Another way of looking at things may be

1
1 1
1 2 1
1 3 3 1
...

Now think about the following from the assignment: "The other values in the triangle come from summing the two values directly above it in the previous row.""

And here's what my professor said:

"You should implement this with recursive calls to pascalTriangle. If you use division, then you will probably overflow an int in the test case pascalTriangle(50, 4). "

I'm having trouble thinking up another way to go about this...

Edit: Nevermind! Found the solution oh Yahoo answers...haha.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |