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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:38:29+00:00 2026-06-16T03:38:29+00:00

I need to work out a very large power modulo (2^32), i.e. I want

  • 0

I need to work out a very large power modulo (2^32), i.e. I want the result of:

y = (p^n) mod (2^32)

p is a prime number
n is a large integer

Is there a trick to doing this efficiently in Java?

Or am I stuck with doing it in a loop with n iterations?

  • 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-16T03:38:30+00:00Added an answer on June 16, 2026 at 3:38 am

    The simple way to mod 2^32 is to use & 0xFFFFFFFFL. Also, there happens to be a type which naturally keeps the lowest 32-bit called int 😉 If you use that you don’t even need to perform the & until you have the result (so the answer is unsigned) For this reason you only need to keep the last 32 bit of the answer. To speed up the ^n you can calculate the square, it’s square and it’s square etc, e.g if n is 0b11111 then you need to multiply p^16 * p^8 * p^4 * p^2 * p.

    In short, you can use plain int as you only need 32-bit of accuracy and values with a cost of O(ln n) where n is the power.

    int prime = 2106945901;
    for (int i = 0; i < 10; i++) {
        long start = System.nanoTime();
        long answer1 = BigInteger.valueOf(prime)
                                 .modPow(
                                     BigInteger.valueOf(prime), 
                                     BigInteger.valueOf(2).pow(32)).longValue();
    
        long mid = System.nanoTime();
        int answer2 = 1;
        int p = prime;
        for (int n = prime; n > 0; n >>>= 1) {
            if ((n & 1) != 0)
                answer2 *= p;
            p *= p;
        }
        long end = System.nanoTime();
        System.out.printf("True answer %,d took %.3f ms, quick answer %,d took %.3f ms%n",
                answer1, (mid - start) / 1e6, answer2 & 0xFFFFFFFFL, (end - mid) / 1e6);
    }
    

    prints finally

    True answer 4,169,684,317 took 0.233 ms, quick answer 4,169,684,317 took 0.002 ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The factorial function could return a very large number as a result. How could
I have a situation where I need to place very large number of jars
I need to work out the best way to read data that is being
I need to be able to work out what page is hosting my silverlight
I'm trying to work out what regular expression I would need to change this
I need to work with large files and must find differences between two. And
When editing javascript, if I need to comment out a large section of code
I need to have very high-performance loop going over large datasets. I need to
I am attempting to work with a very large dataset that has some non-standard
I'm using GNU Emacs 23.3 on Windows. I work in a very large codebase

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.