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

  • Home
  • SEARCH
  • 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 7786155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:22:14+00:00 2026-06-01T20:22:14+00:00

I am trying to write a function in Java that will return the number

  • 0

I am trying to write a function in Java that will return the number of factors a specific number has.

The following restrictions should be taken into account.

  1. It should be done with BigInteger
  2. Storing the previously generated numbers is not allowed, thus more processing and less memory. (You can not use "Sieve of Atkin" like in this)
  3. Negative numbers can be ignored.

This is what I have so far, but it is extremely slow.

public static int getNumberOfFactors(BigInteger number) {
    // If the number is 1
    int numberOfFactors = 1;
    
    if (number.compareTo(BigInteger.ONE) <= 0)  {
        return numberOfFactors;
    }
    
    BigInteger boundry = number.divide(new BigInteger("2"));
    BigInteger counter = new BigInteger("2");
    
    while (counter.compareTo(boundry) <= 0) {
        if (number.mod(counter).compareTo(BigInteger.ZERO) == 0) {
            numberOfFactors++;
        }
        
        counter = counter.add(BigInteger.ONE);
    }
    
    // For the number it self
    numberOfFactors++;
    
    return numberOfFactors;
}
  • 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-01T20:22:15+00:00Added an answer on June 1, 2026 at 8:22 pm

    I can propose faster solution, though I have a feeling that it will not be fast enough yet. Your solution runs in O(n) and mine will work in O(sqrt(n)).

    I am going to use the fact that if n = xi1p1 * xi2p2 * xi3p3 * … xikpk is the prime factorization of n (i.e. xij are all distinct primes) then n has (p1 + 1) * (p2 + 1) * … * (pk + 1) factors in total.

    Now here goes the solution:

    BigInteger x = new BigInteger("2");
    long totalFactors = 1;
    while (x.multiply(x).compareTo(number) <= 0) {
        int power = 0;
        while (number.mod(x).equals(BigInteger.ZERO)) {
            power++;
            number = number.divide(x);
        }
        totalFactors *= (power + 1);
        x = x.add(BigInteger.ONE);
    }
    if (!number.equals(BigInteger.ONE)) {
        totalFactors *= 2;
    }
    System.out.println("The total number of factors is: " + totalFactors);
    

    This can be further optimized if you consider the case of 2 separately and then have the step for x equal to 2 not 1 (iterating only the odd numbers).

    Also note that in my code I modify number, you might find it more suitable to keep number and have another variable equal to number to iterate over.

    I suppose that this code will run reasonably fast for numbers not greater than 264.

    EDIT I will add the measures of reasonably fast to the answer for completeness. As it can be seen in the comments below I made several measurements on the performance of the proposed algorithm for the test case 1000000072, which was proposed by Betlista:

    • If the algorithm is used as is the time taken is 57 seconds on my machine.
    • If I consider only the odd numbers the time is reduced to 28 seconds
    • If I change the check for the end condition of the while to comparing with the square root of number which I find using binary search the time taken reduces to 22 second.
    • Finally when I tried switching all the BigIntegers with long the time was reduced to 2 seconds. As the proposed algorithm will not run fast enough for number larger than the range of long it might make sense to switch the implementation to long
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a recursive function in Java that prints the numbers
I'm trying to write a function that will remove a query argument from a
I am trying to write a function that will pull the name of a
I am trying to write a general function in F# that would return all
I'm trying to write a simple recursive function that look over list and return
I've been trying to write some very fast Java code that has to do
I'm a C/C++/Java programmer working with JavaScript. I'm trying to write a function that
I'm trying to write a Java function that takes a Class<?> and returns a
I'm trying to write a function in three classes that will compare two lists
I'm trying to write a java function that takes as a parameter an enum

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.