Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:23:50+00:00 2026-06-12T10:23:50+00:00

I am learning java myself these days. This function is to calculate the Combination.

  • 0

I am learning java myself these days. This function is to calculate the Combination. However, I found that there is a very small limit of the number n and k in this function. Each time if I type a large n or k, for instance, 100, it gives me

Exception in thread "main" java.lang.ArithmeticException: / by zero
at Combination.combination(Combination.java:29)
at Combination.main(Combination.java:47)

Or gives me a negative number…

Is there a way to make it work for large number like 10000?

Thanks!

import java.util.HashMap; import java.util.Map;

public class Combination {

private Map<Long,Long> factorialMap = new HashMap<Long,Long>();

public Long getFactorial(int number) {
    Long val = factorialMap.get(number);
    if(val != null) {
        return val;
    } else {
        val = getFactorialRecursive(number);
        factorialMap.put((long) number, val);
        return val;
    }
}

public Long getFactorialRecursive(int number) {
    if(number == 1 || number == 0) {
        return 1L;
    } else {
        return number * getFactorialRecursive(number-1);
    }
}

public Long combination(int fromVal, int chooseVal) {
    return getFactorial(fromVal)/(getFactorial(chooseVal)*getFactorial(fromVal-chooseVal));
}


public static void main(String[] args) {
    int   n, k;
    Combination comb = new Combination();
    java.util.Scanner console = new java.util.Scanner(System.in);

    while (true)  // will break with k > n or illegal k or n
    {  System.out.print ("Value for n:  ");
       n = console.nextInt();
       if ( n < 0 ) break;
       System.out.print ("Value for k:  ");
       k = console.nextInt();;
       if ( k > n || k < 0 )
          break;
       System.out.print(n +" choose " + k + " = ");
       System.out.println(comb.combination(n,k));
    }
    console.nextLine();    // Set up for "Press ENTER...


} }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:23:51+00:00Added an answer on June 12, 2026 at 10:23 am

    You should use the BigInteger object: http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

    In particular, your problem is that 21! is too large for even a long and therefore overflows. Another option would be to use a double, but that will lose precision, so if you need integer accuracy BigInteger is the way to go.

    Using BigInteger you will need to convert your integer to BigInteger:

    BigInteger bi = new BigInteger(intVal+"");
    

    Then use the add, multiply, divide and subtract (amongst others) to manipulate your values (like):

    bi = bi.add(bi2);
    

    Then you can use the method longValue() to get the value back (assuming it fits in a long):

    return bi.longValue();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Java and find myself sending methods around while asking for help but
I'm learning Java on my own; and therefore the code below has no function
I am learning Java and have been given these options: How can you implement
I'm learning C++ by myself, creating a RPG. However, I'm stuck at point of
I'm new to Java, and have set myself a project to write so that
I'm really new to java and slowly learning so not sure if there is
I know that there have been plenty of topics describing this topic but I
I want to teach myself enough machine learning so that I can, to begin
I am learning JAVA myself and I am learning about memoization now. But i
I'm learning myself Play 2.0 (Java API used) and would like to have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.