You are not logged in. Please login at www.codechef.com to post your questions!

×

small factorials

how to rectify NZEC error in java for below code which runs well on netbeans 5.0 and gives correct factorial value for 100,99 etc.

/*
* Main.java
*
* Created on May 19, 2012, 12:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication5;

/**
*
* @author Shivran Roy
*/
import java.util.*;
import java.io.*;
public class Main {

    /** Creates a new instance of Main */
    public Main() {
    }

    /**
    * @param args the command line arguments
    */
    public static void   main(String[] args) {

        int k,j,i,t,l;
        double f;
        Scanner input=new Scanner(System.in);
        t=input.nextInt();
        int arr[]=new int[t];
        j=0;
        for(i=0;i<t;i++)
        {
            arr[i]=input.nextInt();
            System.out.print("\n");
        }
        for(k=0;k<=i;k++)
        {

            for(f=1,l=1;l<=arr[k];l++)
            {
                f=f*l;
            }
            System.out.println(f);

            // TODO code application logic here
        }

    }
}

asked 19 May '12, 22:35

shivran352's gravatar image

0★shivran352
20114
accept rate: 0%

edited 22 Jun '12, 14:54

admin's gravatar image

0★admin ♦♦
18.4k348492529


@shivran352 :There are quite a few petty errors and one or two blunders in your Java Code:

(1).The Main Reason Behind NZEC is the line "for(f=1,l=1;l<=arr[k];l++)" .The Array arr index becomes out of bound for large input input value like 100.

Ran your code on IDEONE.COM ,the runtime error detail is :: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Main.main(Main.java:31)

(2).I don't know why the type of f is "Double".Its meaning less and moreover running the loop for(f=1,l=1;l<=arr[k];l++) where the type of f is double,well i have never seen such thing in my life.

(3).Don't use package word in your code if you are submitting it to codechef judge.

(4)what is the purpose of System.out.print("n") after arr[i]=input.nextInt() instruction?Printing blank line is absurd.

link

answered 20 May '12, 19:19

ritesh_gupta's gravatar image

5★ritesh_gupta ♦
3.7k42549
accept rate: 27%

edited 20 May '12, 19:26

1

thanks for ur analysis.... how can i make an integer array in java which takes input in runtime and its index does not go out of bounds for large values? and i have used double so that the value of factorial for large no. like 100,98 etc remains in its range .

(20 May '12, 21:39) shivran3520★
2

Well as the factorial of 100 is quite large so no data type can hold such a large value.

But as you are a java Coder ,so u are quite lucky. There is a data-type called Big Integers (in java.math.*) ,that can holds any large value.

Go through the following page http://wiki.compsci.ca/index.php?title=Java_Big_Integers to learn more about it. Here is the code that will help you to calculate large factorials using BigInteger: BigInteger fact=new BigInteger ("1"); for(int j=1;j<=N;j++) { fact=fact.multiply(BigInteger.valueOf(j)); } System.out.println(fact);

(21 May '12, 10:07) ritesh_gupta ♦5★

Your class cannot be in package. Use so called default package.

edit:

description in detail

When you submit your solution, CodeChef server tries to compile it. If you want to compile your Main class from command line you have to execute:

javac Main.java

Next for correctness checking CodeChef server runs the class

java Main < test.input

That's what CodeChef server tries to do. But if your class is in package (javaapplication5 in your case) CodeChef server have to now, that your submitted code have to be placed to javaapplication5 folder and compiled as

java javaapplication5\Main

and run as

javac javaapplication5.Main

Simple solution (if you are submitting code using editor) is removing package declaration line package javaapplication5 from code (it will work exactly the same way).

PS: I have to warn you that there is similar problem in your code as asked here.

link

answered 20 May '12, 03:01

betlista's gravatar image

3★betlista ♦♦
16.9k49115225
accept rate: 11%

edited 20 May '12, 14:29

thanks for suggestion........

(20 May '12, 21:40) shivran3520★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,091
×362

question asked: 19 May '12, 22:35

question was seen: 3,446 times

last updated: 22 Jun '12, 14:54