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 6956155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:49:53+00:00 2026-05-27T14:49:53+00:00

I am doing calculations with BigIntegers that uses a loop that calls multiply() about

  • 0

I am doing calculations with BigIntegers that uses a loop that calls multiply() about 100 billion times, and the new object creation from the BigInteger is making it very slow. I was hoping somebody had written or found a MutableBigInteger class. I found the MutableBigInteger in the java.math package, but it is private and when I copy the code into a new class, many errors come up, most of which I don’t know how to fix.

What implementations exist of a Java class like MutableBigInteger that allows modifying the value in place?

  • 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-05-27T14:49:54+00:00Added an answer on May 27, 2026 at 2:49 pm

    Is their any particular reason you cannot use reflection to gain access to the class?

    I was able to do so without any problems, here is the code:

    public static void main(String[] args) throws Exception {       
        Constructor<?> constructor = Class.forName("java.math.MutableBigInteger").getDeclaredConstructor(int.class);
        constructor.setAccessible(true);
        Object x = constructor.newInstance(new Integer(17));
        Object y = constructor.newInstance(new Integer(19));
        Constructor<?> constructor2 = Class.forName("java.math.MutableBigInteger").getDeclaredConstructor(x.getClass());
        constructor2.setAccessible(true);
        Object z = constructor.newInstance(new Integer(0));
        Object w = constructor.newInstance(new Integer(0));
    
        Method m = x.getClass().getDeclaredMethod("multiply", new Class[] { x.getClass(), x.getClass()});
        Method m2 = x.getClass().getDeclaredMethod("mul", new Class[] { int.class, x.getClass()});
        m.setAccessible(true);
        m2.setAccessible(true);
    
        // Slightly faster than BigInteger
        for (int i = 0; i < 200000; i++) {
            m.invoke(x, y, z);
            w = z;
            z = x;
            x = w;
        }
    
        // Significantly faster than BigInteger and the above loop
        for (int i = 0; i < 200000; i++) {
            m2.invoke(x, 19, x);
        }
    
        BigInteger n17 = new BigInteger("17");
        BigInteger n19 = new BigInteger("19");
        BigInteger bigX = n17;
    
        // Slowest
        for (int i = 0; i < 200000; i++) {
            bigX = bigX.multiply(n19);
        }
    }
    

    Edit:
    I decided to play around with a bit more, it does appear that java.math.MutableBigInteger doesn’t behave exactly as you would expect.

    It operates differently when you multiply and it will throw a nice exception when it has to increase the size of the internal array when assigning to itself. Something I guess is fairly expected. Instead I have to swap around the objects so that it is always placing the result into a different MutableBigInteger. After a couple thousand calculations the overhead from reflection becomes negligible. MutableBigInteger does end up pulling ahead and offers increasingly better performance as the number of operations increases. If you use the ‘mul’ function with an integer primitive as the value to multiply with, the MutableBigInteger runs almost 10 times faster than using BigInteger. I guess it really boils down to what value you need to multiply with. Either way if you ran this calculation “100 billion times” using reflection with MutableBigInteger, it would run faster than BigInteger because there would be “less” memory allocation and it would cache the reflective operations, removing overhead from reflection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing some calculations that require a large array to be initialized. The
I'm doing some calculations that result in a value like {-0.707107, 9.61481e-017, 0.707107} when
How would I go about doing calculations with extremely large numbers in Java? I
I know that UPS has some APIs they make available for doing shipping calculations.
in my C#-Silverlight-3-Application, I have created a class, that is doing some calculations that
Python 3.1 I am doing some calculations on a data that has missing values.
In my mxml file, I'm doing some calculations in the script tag, and binding
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
When doing calculations on very large numbers where integral data types such as double
I'm doing some calculations, and the results are being save in a file. I

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.