6 Replies - 739 Views - Last Post: 21 March 2012 - 09:33 AM Rate Topic: -----

#1 hambonie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 12

Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:28 PM

I was given this assignment last week in an intro to java course:
Print out the factorials from 1-100. First use integers. What happens? and why? Then use BigInteger... I had it working using the following code but my prof is being picky and wants me to do it another way :{
import javax.swing.*;

public class FactorialMethod {
  public static void main(String[] args) { 
	 long start = 0;
	 System.out.println("The factorials from 1-100 are:");
  		factorial(start);														
		}

  public static void factorial(long start) {
  	int counter = 1;
	double factorial = 0;
	int x;
	while (counter<=100){
		System.out.print("Factorial of " + counter + " is");
			factorial = counter/counter;
				for (x=counter; x>0; x--){
				 factorial = factorial*x;
}
		System.out.print(" " + Math.abs(factorial) + "\n\n");	
		counter++;					
}
  	 	  												}
}


as i said the above logic was not acceptable; so, i started from scratch and have come up with the following. unfortunately there is an error in my factorial method relating to a missing return statement. Does anyone have any ideas?
import java.util.*;
import java.math.*;
public class FactorialMethod2 {

    
    public static void main(String[] args) {
                
        int counter = 1;
        int start = 0;
	System.out.println("Factorials from 1-100 are: "); 
        factorial(counter);
  	factorial(start);
        
    }
    
    //return factorials for 1-100
    public static int factorial(int counter){
        
        counter = 1;
	int factorial = 0;
	int x;
	while (counter<=100){
		System.out.print("Factorial of " + counter + " is");
			factorial = counter/counter;
				for (x=counter; x>0; x--){
				 factorial = factorial*x;
        
        
        if (counter == 0) 
            return 1;
        else
            return counter * factorial( counter-1 ); //recursive call
        
        
        
    }
}
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Missing Return Statement in code to display factorials from 1-100

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:53 PM

Quote

as i said the above logic was not acceptable; so, i started from scratch and have come up with the following. unfortunately there is an error in my factorial method relating to a missing return statement. Does anyone have any ideas?


There's a more elegant way to write a recursive factorial function. Two parts:

  • A base case : The condition which stops the recursion
  • A recursive call : Do the same operation but with a different input.


1 + 2 + 3 + 4 ... : That pattern is similar to a factorial. f(n) = n + f(n-1), for n > 0

You're currently using an iterative approach. Why don't you work on that.

Factorial growth is greater than exponential growth, so for increasing inputs the output will grow very large quickly and you'll overflow the output storage. The BigInteger is an arbitrarily large data type that accommodates large numbers.

This post has been edited by blackcompe: 20 March 2012 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:58 PM

    while (counter<=100)
    {
        System.out.print("Factorial of " + counter + " is");
            factorial = counter/counter;
                for (x=counter; x>0; x--)
    {
                 factorial = factorial*x;
        
        if (counter == 0)
            return 1;
        else
            return counter * factorial( counter-1 ); //recursive call
    }
}



From the compiler's perspective there is the possibility that counter starts out more than 100. In that case the while loop never executes and you have no return statement in that eventuality.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:59 PM

A don't think recursion is involved here the only thing the teacher wants to see is that by using int you will overflow which won't be the case with BigInteger
and he wants the result of each factorial to see where it will slip :)... around 16 I guess not really need to go to 100

	public static void main(String[] args) {
		int facto = 1;
		for(int i = 2; i <= 100; ++i) {
			facto *= i;
			System.out.printf("Facto of %3d is %12d\n", i, facto);
		}
	}



and with a long you can go further
	public static void main(String[] args) {
		int facto = 1;
		for(int i = 2; i <= 100; ++i) {
			facto *= i;
			System.out.printf("Facto of %3d is %12d\n", i, facto);
		}
		long lFacto = 1;
		for(long i = 2; i <= 100L; ++i) {
			lFacto *= i;
			System.out.printf("Facto of %3d is %20d\n", i, lFacto);
		}
	}


This post has been edited by pbl: 20 March 2012 - 02:02 PM
Reason for edit:: added the long version

Was This Post Helpful? 1
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 02:09 PM

Quote

A don't think recursion is involved here the only thing the teacher wants to see is that by using int you will overflow which won't be the case with BigInteger
and he wants the result of each factorial to see where it will slip :)... around 16 I guess not really need to go to 100


Actually I agree completely. Perhaps the submission was rejected because you failed to implement the BigInteger requirement.
Was This Post Helpful? 0
  • +
  • -

#6 hambonie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 12

Re: Missing Return Statement in code to display factorials from 1-100

Posted 21 March 2012 - 06:37 AM

Thanks for the help. i almost have it. but am having trouble getting the BigInteger portion of the code to compile. i don't know how to properly declare j. any ideas?
//csc 151
//small extra 
//factorial from 1-100
//by hambonie

   import javax.swing.*;
   import java.math.*;

   public class FactorialExtra3 {
      public static void main(String[] args) { 
         System.out.println("The factorials from 1-100 are:");
         factorial();														
      }
   
      public static void factorial() {
         int facto = 1;
	    for(int i = 2; i <= 100; ++i) {
	        facto *= i;
	        System.out.printf("Facto of %3d is %12d\n", i, facto);
	    }
            System.out.println("BigInteger factorials from 1-100 are:");
            BigFactorial();
      }
            public static void BigFactorial() {
         BigInteger factor = new BigInteger("1");
         //BigInteger j = new BigInteger("2");
	    for(BigInteger j = 2; j <= 100; ++j) {
	        factor *= j;
	        System.out.printf("Facto of %3d is %12d\n", j, factor);
	}
            
         }}
      

Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Missing Return Statement in code to display factorials from 1-100

Posted 21 March 2012 - 09:33 AM

1. Don't call BigFactorial from factorial. It's bad design. Call BigFactorial from main right after calling factorial.

2. You can only use binary arithmetic operators with primitive data types and wrapper classes, i.e. int, float, Integer, Byte. BigInteger is an object, which requires calls to its methods to change its internal state. Look at the BigInteger docs to see what methods are available for doing arithmetic.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1