473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help -My Pascal Triangle program breaks down at row 13- I've spent hours!

Hi, my name is Stephan. I've been working on this program off and on over the past few days. I have yet to perfect it. I would really appreciate any help! For some reason the output begins to deteriorate at around row 13...I can't figure out why.
Thank you in advance,
Stephan

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import gpdraw.*;
  4. import java.lang.*;
  5. public class pascal{
  6. int[] array= new int[1000];
  7. public void generate(int input){
  8.     int i=0;
  9.     for(int n=0;n<=input;n++){
  10.        for(int r=0 ; r<=n;r++){
  11.            array[i] = fact(n)/(fact(n-r)*fact(r));
  12.            i++;
  13.     }}
  14.     int d=0;
  15.     for (int row = 0; row <=input; row++){
  16.         for(int pos=0; pos<=row; pos++){
  17.             System.out.print(" " + array[d]);
  18.             d++;
  19. System.out.println();
  20.  
  21. }}
  22. public static int fact(int x){
  23.         if( x <= 1 )     // base case
  24.             return 1;
  25.         else
  26.             return x * fact( x - 1 );
  27.     }
  28. }
DRIVER
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class pascaldriver{
  3. public static void main(String[] args){
  4.     int input;
  5.     Scanner nub= new Scanner(System.in);
  6.     System.out.print("Enter the row you want the pascal triangle to go to: ");
  7.     input = nub.nextInt();
  8.  
  9. pascal tri = new pascal();
  10.     tri.generate(input);}
OUTPUT
Expand|Select|Wrap|Line Numbers
  1. Enter the row you want the pascal triangle to go to: 13
  2.  1
  3.  1 1
  4.  1 2 1
  5.  1 3 3 1
  6.  1 4 6 4 1
  7.  1 5 10 10 5 1
  8.  1 6 15 20 15 6 1
  9.  1 7 21 35 35 21 7 1
  10.  1 8 28 56 70 56 28 8 1
  11.  1 9 36 84 126 126 84 36 9 1
  12.  1 10 45 120 210 252 210 120 45 10 1
  13.  1 11 55 165 330 462 462 330 165 55 11 1
  14.  1 12 66 220 495 792 924 792 495 220 66 12 1
  15.  1 4 24 88 221 399 532 532 399 221 88 24 4 1
Starts at row 0 as well as position 0 on each row.

The more rows I enter the more senseless the output becomes.

Expand|Select|Wrap|Line Numbers
  1. 1
  2.  1 1
  3.  1 2 1
  4.  1 3 3 1
  5.  1 4 6 4 1
  6.  1 5 10 10 5 1
  7.  1 6 15 20 15 6 1
  8.  1 7 21 35 35 21 7 1
  9.  1 8 28 56 70 56 28 8 1
  10.  1 9 36 84 126 126 84 36 9 1
  11.  1 10 45 120 210 252 210 120 45 10 1
  12.  1 11 55 165 330 462 462 330 165 55 11 1
  13.  1 12 66 220 495 792 924 792 495 220 66 12 1
  14.  1 4 24 88 221 399 532 532 399 221 88 24 4 1
  15.  1 0 1 5 14 29 44 50 44 29 14 5 1 0 1
  16.  1 1 -4 -1 2 4 7 9 9 7 4 2 -1 -4 1 1
  17.  1 0 -1 -1 -1 4 -1 1 1 1 -1 4 -1 -1 -1 0 1
  18.  1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
I wouldn't post unless I had spent a few hours on this...
I WOULD REALLY APPRECIATE ANY HELP AT ALL!
Mar 18 '08 #1
3 2334
Ganon11
3,652 Expert 2GB
It probably has to do with the factorial being used. 13! is likely too big to hold in an int. This isn't your fault, so don't blame your code or yourself - that's just how Java was made.

If you have a desperate need to go to 13 rows and beyond, you need to look into alternate data types to use.
Mar 18 '08 #2
Laharl
849 Expert 512MB
Yep, 13! is 6,227,020,800, which is too big for the 4billion or so a 32bit integer (which is all Java has) can handle. Use a long instead, that's 64 bits and will thus get you to 2^63-1, which is 9.22*10^18.
Mar 18 '08 #3
JosAH
11,448 Expert 8TB
I'd say don't use factorials at all. A number at line i, column j is the sum of the
two numbers diagonally above it, i.e (i-1, j-1) and (i-1, j). If you calculate those
numbers from right to left you can use the same array over and over again.
Here's a spoiler so no comments nor an explanation:

Expand|Select|Wrap|Line Numbers
  1. public class Pascal {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         int[] n= new int[20];
  6.  
  7.         for (int i= 0; i < n.length; i++) {
  8.             n[i]= 1;
  9.             for (int j= i; --j > 0; n[j]+= n[j-1]);
  10.             for(int j= 0; j <= i; j++)
  11.                 System.out.print(n[j]+" ");
  12.             System.out.println();
  13.         }
  14.     }
  15. }
kind regards,

Jos
Mar 18 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
7
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. I have spent hours on this.. but I am a total novice and can't seem to figure it out. Here is a .php script. Right now, the minimum amount of time (i.e. duration)...
25
by: GUPTAJI | last post by:
hi all, can u give me the code to create a Pascal's Triangle........... and yes, it should work thankx
5
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
4
by: asif929 | last post by:
I have another program to write, i will appreciate if somebody can help......prompts the user to enter positive integer, and then prints out four triangles For Example: If we enter 4 it should...
4
by: Aaron | last post by:
Hi, I have written a pascals triangle program. However, my program gets a floating point exception somewhere between 60 and 70 and any subsequent larger value. This is no doubt due to dividing...
11
by: Jim Madsen | last post by:
Hi all-- Two years ago I bought a copy of vbdotnet2003 and the book "Learn MS VBDotNet 2003 in 21 Days", published by Sams. I started working through the book, but lost interest. This...
1
by: AlmasKhan01 | last post by:
Hi I have been for sometime now been working on a MineSweeper program and I'm 90% complete on it it involves a window interface and is quite simple but the issue is not. The program is long(to me)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.