# Arrays ?

• 03-08-2014, 06:38 PM
earthkid123
Arrays ?
I have to write a program that computes factorials up to 50
public static void main(String[] args)
{
int [] a= new int [100];
for(int n=0; n<=50; n++)
{
double factorial = 1;
for (int multiplier=1; multiplier<=50; multiplier++)
{
factorial = factorial * multiplier;
System.out.println(multiplier + "!" + factorial);
}
}
}
}
// I have this as my code so far I just don't know how to incorporate the array in there which i need to do as well as I dont get why all the output comes out for every single thing . IT will write 1 factorial and display all the factorials up to 50 and do the same pattern over and over again
• 03-08-2014, 06:43 PM
jim829
Re: Arrays ?
I'm still not certain what you want to do. You have an int array but are calculating double factorials (which will soon be very inaccurate). The reason it is repeating is because of your outer loop. Just use the multiplier to save the value in the array (but change the array to doubles).

And you may want to investigate the BigInteger class for this problem.

Regards,
Jim
• 03-08-2014, 06:44 PM
earthkid123
Re: Arrays ?
we were not yet taught the BigInteger Im confused on where to put the int array I was going to change my double to an int factorial
• 03-08-2014, 06:45 PM
earthkid123
Re: Arrays ?
No i need to keep it as a double actually
• 03-08-2014, 06:53 PM
jim829
Re: Arrays ?
Get rid of the outer loop. You don't need it. Save each factorial in the 'a' array indexed by 'multiplier'. I also recommend you size the array properly and the use a.length as the upper bounds in your for loop (instead of a discrete value of 50).

You can then use another loop below the first one to verify that the values are stored properly by printing them out.

Edit: and don't forget about 0! which is equal to 1 by definition. So you can just preset the a[0] to 1.

Regards,
Jim
• 03-08-2014, 07:00 PM
JosAH
Re: Arrays ?
Forget about numerical overflow for the moment (it'll hit you later and it'll hit hard and mean ;-) If you want to calculate the fifth entry in your array, you compute 1*2*3*4*5, if you want to calculate the sixth entry, you compute 1*2*3*4*5*6 etc. etc. You're doing most of the work you've done already again! A small observation n! == n*(n-1)!, i.e. if you want to compute n! and you have computed (n-1)! already,all you have to do is multiply the previous result by n. So, if you have to compute the n-th entry in your array (n!), all you do is take the previous entry in your array (n-1)! and multiply it by n. Only the zeroth entry doesn't have a previous entry, but you know what it is: 0! == 1. A bit of clever looping does the rest.

kind regards,

Jos
• 03-08-2014, 08:52 PM
earthkid123
Re: Arrays ?
I now have this but I don't know how to get the algorithm working for factorials

public static void main(String[] args)
{
double [] a= new double [51];

double factorial = 1.0;
for (int multiplier=0; multiplier<=50; multiplier++)
{
a[multiplier] = factorial * multiplier;
}
for(int i=0; i<=50;i++)
{

a[i]=a[i]*(i*(i-1));
a[0]=1;
System.out.println(a[i]);

}
}
}
• 03-08-2014, 09:41 PM
JosAH
Re: Arrays ?
Suppose you know that the i-1-th element of your array contains (i-1)!; it is easy then to compute the i-th element:

Code:

`a[i]= i*a[i-1];`
You also know that 0! == 1, so:

Code:

`a[0]= 1;`
Combining those two notions and adding a loop makes this:

Code:

```a[0]= 1; for (int i= 1; i <= n; i++)   a[i]= i*a[i-1];```
This technique is called mathematical induction and it's easy when you get the hang of it.

kind regards,

Jos
• 03-08-2014, 10:29 PM
earthkid123
Re: Arrays ?
Thank you so much for helping I really appreciate it