Factorial Calculation for integers 1-5

Any way to make my program more flexible?

Page 1 of 1

13 Replies - 7976 Views - Last Post: 03 March 2009 - 08:44 PM Rate Topic: -----

#1 rekijitsu89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-April 08

Factorial Calculation for integers 1-5

Posted 03 March 2009 - 06:57 PM

public class FindFactorial
{
	public static void main (String args[])
	{
		int value = 1;
				System.out.printf("Value   Factorial\n");
		while (value <= 5)
		{
			switch (value)
			{
			case 1:
				{
				System.out.printf("1:	  %d\n", 1);
				value++; continue;
				}
			case 2:
				{
				System.out.printf("2:	  %d\n", 2*1);
				value++; continue;
				}
			case 3:
				{
				System.out.printf("3:	  %d\n", 3*2*1);
				value++; continue;
				}
			case 4:
				{
				System.out.printf("4:	  %d\n", 4*3*2*1);
				value++; continue;
				}
			case 5:
				{
				System.out.printf("5:	  %d\n", 5*4*3*2*1);
				value++; continue;
				}
			}
		}
	}
}

/* The obvious problem with evaluating something like 20 is two things, for one, it is in tabular format,
which in the simple manner I did my table in would very likely lose formatting and the larger numbers like
the factorial of 20 would move the number further to the right, and from an appearance perspective wouldn't look
very clean, even if it *appears* like I made it left justified for any value that is single digit. The second and 
more noticable problem is that this program is not very flexible. It does what the example asks it to do, but 
with my switch statement to evaluate each integer, I would also have to change the value of the loop control
variable "value" to 20, if I kept consistant and used value++, I would have to do 6-19 as well in the display.
Also, I have to edit the source code in my program to do anything like 20, it isn't input by the user,
but the example didn't ask for it to be. */ 


This is what I have as my program, basically the assignment reads as such:
"Write an app that evaluates the factorials of the integers from 1-5. Display the results in tabular format. What difficulty might prevent you from calculating the factorial of 20?"

Our teacher normally doesn't take off for flexibility... but as you can see I didn't even really use an algorithm here, I basically calculated the value through each case... I hate to leave it so inflexible, but should I Keep it Simple Stupid because the book says to only evaluate 1-5?

Is This A Good Question/Topic? 0
  • +

Replies To: Factorial Calculation for integers 1-5

#2 rekijitsu89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-April 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 07:09 PM

I know this sounds completely noobish but...

public class FindFactorial
{
	public static void main (String args[])
	{
		int value = 1;
				System.out.printf("Value   Factorial\n");

			switch (value)
			{
			case 1:
				{
				System.out.printf("1:	  %d\n", 1);
				value++;
				}
			case 2:
				{
				System.out.printf("2:	  %d\n", 2*1);
				value++;
				}
			case 3:
				{
				System.out.printf("3:	  %d\n", 3*2*1);
				value++;
				}
			case 4:
				{
				System.out.printf("4:	  %d\n", 4*3*2*1);
				value++;
				}
			case 5:
				{
				System.out.printf("5:	  %d\n", 5*4*3*2*1);
				value++;
				}
			}
		
	}
}

/* The obvious problem with evaluating something like 20 is two things, for one, it is in tabular format,
which in the simple manner I did my table in would very likely lose formatting and the larger numbers like
the factorial of 20 would move the number further to the right, and from an appearance perspective wouldn't look
very clean, even if it *appears* like I made it left justified for any value that is single digit. The second and 
more noticable problem is that this program is not very flexible. It does what the example asks it to do, but 
with my switch statement to evaluate each integer, I would also have to change the value of the loop control
variable "value" to 20, if I kept consistant and used value++, I would have to do 6-19 as well in the display.
Also, I have to edit the source code in my program to do anything like 20, it isn't input by the user,
but the example didn't ask for it to be. */


I was able to get rid of the while loop entirely, dunno why I needed it honestly, maybe I incorporated it just because we have been learning about them... lol.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 07:19 PM

KISS is a nice idea! :)

You are working too hard doing it in a straight line approach. :)

Instead of a while() loop, think how you would calculate a factorial using a for() loop. For each case in your switch statement, you are doing by hand (so to speak) what a for() loop could be doing for you.

As for the difficulty question, just how big is 20!?
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 07:48 PM

You are not making a factorial but a sumation

int x = 10;
int sum = 0;
for(int i = 1; i <= 10; i++)
	sum += i;



factorial would be

int x = 10;
int facto = 1;
for(int i = 1; i <= 10; i++)
	facto *= i;


Was This Post Helpful? 0
  • +
  • -

#5 rekijitsu89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-April 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:07 PM

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:21 PM

View Postrekijitsu89, on 3 Mar, 2009 - 07:07 PM, said:

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...

value++; looks like an addition to me :D
Was This Post Helpful? 0
  • +
  • -

#7 rekijitsu89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-April 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:26 PM

Yeah value actually isn't the value of a factorial for me, in fact I have no variable to account for the factorial value of a number. The value is more or less for each case so that my program continues to evaluate the factorial of the next case (1, 2, 3 +), and running the multiplication within each print statement. It isn't fancy at all really, and not very flexible since it just does simple math in the print statement itself.
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:31 PM

View Postpbl, on 3 Mar, 2009 - 09:21 PM, said:

View Postrekijitsu89, on 3 Mar, 2009 - 07:07 PM, said:

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...

value++; looks like an addition to me :D


Look in the printf(...) statements. The calculation(s) are there.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:34 PM

View PostLocke, on 3 Mar, 2009 - 07:31 PM, said:

View Postpbl, on 3 Mar, 2009 - 09:21 PM, said:

View Postrekijitsu89, on 3 Mar, 2009 - 07:07 PM, said:

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...

value++; looks like an addition to me :D


Look in the printf(...) statements. The calculation(s) are there.


Locke: 1 PBL: 0
So what is value used for ?
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:35 PM

View Postpbl, on 3 Mar, 2009 - 09:34 PM, said:

Locke: 1 PBL: 0


;):D

View Postpbl, on 3 Mar, 2009 - 09:34 PM, said:

So what is value used for ?


No idea. Right now...useless computation.

This post has been edited by Locke: 03 March 2009 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#11 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:36 PM

View PostLocke, on 3 Mar, 2009 - 07:35 PM, said:

View Postpbl, on 3 Mar, 2009 - 09:34 PM, said:

Locke: 1 PBL: 0


;):D

View Postpbl, on 3 Mar, 2009 - 09:34 PM, said:

So what is value used for ?


No idea. Right now...useless computation.

It originally was the loop counter.
Was This Post Helpful? 0
  • +
  • -

#12 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:39 PM

View Postn8wxs, on 3 Mar, 2009 - 09:36 PM, said:

It originally was the loop counter.


n8wxs: 1
Locke: 1
pbl: 0

:D
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:42 PM

View Postn8wxs, on 3 Mar, 2009 - 07:36 PM, said:

It originally was the loop counter.

So why removing to loop ? Because it looked too much like the code I wrote ? Which will work for any number as the first request was.
Was This Post Helpful? 0
  • +
  • -

#14 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:44 PM

View Postpbl, on 3 Mar, 2009 - 09:42 PM, said:

So why removing to loop ? Because it looked too much like the code I wrote ? Which will work for any number as the first request was.


I don't know...the standard approach to this problem would be a loop...at least, in everyone I know's minds.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1