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

×

Program compiling but not running in Eclipse

I have some trouble with Eclipse I guess. I want to run my factorial program and it compiles but doesn't do anything else. Meaning no error message, no warning, etc... Here is the code maybe you guys can tell me if the mistake is in the code or my eclipse needs some fix. Or it is the big numbers?

import java.math.BigInteger;

public class FactorialTrailingZeros {

public static void main(String[] args) {

    int N = 60;
    System.out.println(factorial(N));
    System.out.println(getZeros(factorial(N)));

}
public static BigInteger factorial(int N){
    BigInteger fact = new BigInteger("1");
    for(BigInteger t = BigInteger.valueOf(1);
            t.compareTo(BigInteger.ZERO) <= N;
            t.add(BigInteger.ONE)){
        //bi1 = bi1.multiply(bi2);
        fact = fact.multiply(t);//multiply BigIntegers
    }
    return fact;
}
public static int getZeros(BigInteger C){
    int count = 0;
    while(C.mod(BigInteger.TEN) == BigInteger.ZERO){ //while the last digit is not different than 0 don't stop

        C = C.divide(BigInteger.TEN);                //divide by 10 each time so we get a 10 times smaller number 100 -> 10

        count++;
    }
    return count;
}

}

asked 16 Feb '15, 20:22

snowpeak's gravatar image

0★snowpeak
195
accept rate: 0%


It is not the problem of eclipse. The for loop calculating factorial is the problem here. Change it to the following.

public static BigInteger factorial(int N){
    BigInteger fact = new BigInteger("1");
    BigInteger big_N=BigInteger.valueOf(N);
    for(BigInteger t = BigInteger.valueOf(1);
            t.compareTo(big_N) <= 0;
            t=t.add(BigInteger.ONE)){
        //bi1 = bi1.multiply(bi2);
        fact = fact.multiply(t);//multiply BigIntegers
    }
    return fact;
}

Your problem is in Comparable.

Given BigInteger x, y, the following are the comparison conditions:

x.compareTo(y) < 0 // x < y
x.compareTo(y) <= 0 // x <= y
x.compareTo(y) != 0 // x != y
x.compareTo(y) == 0 // x == y
x.compareTo(y) > 0 // x > y
x.compareTo(y) >= 0 // x >= y
This is not specific to BigInteger, this is applicable to any Comparable<T> in general.

Note that the above is not the only way.

Read for loop syntax for BigInteger here.

link

answered 16 Feb '15, 21:08

mediocoder's gravatar image

3★mediocoder
1.1k313
accept rate: 36%

edited 16 Feb '15, 21:15

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:

×228
×196
×19
×16
×12

question asked: 16 Feb '15, 20:22

question was seen: 1,759 times

last updated: 16 Feb '15, 21:15