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 3338248

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:21:10+00:00 2026-05-18T00:21:10+00:00

I have made a program in Java that calculates powers of two, but it

  • 0

I have made a program in Java that calculates powers of two, but it seems very inefficient. For smaller powers (2^4000, say), it does it in less than a second. However, I am looking at calculating 2^43112609, which is one greater than the largest known prime number. With over 12 million digits, it will take a very long time to run. Here’s my code so far:

import java.io.*;

public class Power
{
 private static byte x = 2;
 private static int y = 43112609;
 private static byte[] a = {x};
 private static byte[] b = {1};
 private static byte[] product;
 private static int size = 2;
 private static int prev = 1;
 private static int count = 0;
 private static int delay = 0;
 public static void main(String[] args) throws IOException
 {
  File f = new File("number.txt");
  FileOutputStream output = new FileOutputStream(f);
  for (int z = 0; z < y; z++)
  {
   product = new byte[size];
   for (int i = 0; i < a.length; i++)
   {
    for (int j = 0; j < b.length; j++)
    {
     product[i+j] += (byte) (a[i] * b[j]);
     checkPlaceValue(i + j);
    }
   }
   b = product;
   for (int i = product.length - 1; i > product.length - 2; i--)
   {
    if (product[i] != 0)
    {
     size++;
     if (delay >= 500) 
     {
      delay = 0;
      System.out.print(".");
     }
     delay++;
    }
   }
  }
  String str = "";
  for (int i = (product[product.length-1] == 0) ? 
   product.length - 2 : product.length - 1; i >= 0; i--)
  {
   System.out.print(product[i]);
   str += product[i];
  }
  output.write(str.getBytes());
  output.flush();
  output.close();
  System.out.println();
 }

 public static void checkPlaceValue(int placeValue)
 {
  if (product[placeValue] > 9)
  {
   byte remainder = (byte) (product[placeValue] / 10);
   product[placeValue] -= 10 * remainder;
   product[placeValue + 1] += remainder;
   checkPlaceValue(placeValue + 1);
  }
 }  
}

This isn’t for a school project or anything; just for the fun of it. Any help as to how to make this more efficient would be appreciated! Thanks!

Kyle

P.S. I failed to mention that the output should be in base-10, not binary.

  • 0 0 Answers
  • 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-18T00:21:11+00:00Added an answer on May 18, 2026 at 12:21 am

    The key here is to notice that:

    2^2 = 4
    2^4 = (2^2)*(2^2)
    2^8 = (2^4)*(2^4)
    2^16 = (2^8)*(2^8)
    2^32 = (2^16)*(2^16)
    2^64 = (2^32)*(2^32)
    2^128 = (2^64)*(2^64)
    ... and in total of 25 steps ...
    2^33554432 = (2^16777216)*(16777216)
    

    Then since:

    2^43112609 = (2^33554432) * (2^9558177)
    

    you can find the remaining (2^9558177) using the same method, and since (2^9558177 = 2^8388608 * 2^1169569), you can find 2^1169569 using the same method, and since (2^1169569 = 2^1048576 * 2^120993), you can find 2^120993 using the same method, and so on…

    EDIT: previously there was a mistake in this section, now it’s fixed:

    Also, further simplification and optimization by noticing that:

    2^43112609 = 2^(0b10100100011101100010100001)
    2^43112609 = 
          (2^(1*33554432))
        * (2^(0*16777216))
        * (2^(1*8388608))
        * (2^(0*4194304))
        * (2^(0*2097152))
        * (2^(1*1048576))
        * (2^(0*524288))
        * (2^(0*262144))
        * (2^(0*131072))
        * (2^(1*65536))
        * (2^(1*32768))
        * (2^(1*16384))
        * (2^(0*8192))
        * (2^(1*4096))
        * (2^(1*2048))
        * (2^(0*1024))
        * (2^(0*512))
        * (2^(0*256))
        * (2^(1*128))
        * (2^(0*64))
        * (2^(1*32))
        * (2^(0*16))
        * (2^(0*8))
        * (2^(0*4))
        * (2^(0*2))
        * (2^(1*1))
    

    Also note that 2^(0*n) = 2^0 = 1

    Using this algorithm, you can calculate the table of 2^1, 2^2, 2^4, 2^8, 2^16 … 2^33554432 in 25 multiplications. Then you can convert 43112609 into its binary representation, and easily find 2^43112609 using less than 25 multiplications. In total, you need to use less than 50 multiplications to find any 2^n where n is between 0 and 67108864.

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

Sidebar

Related Questions

I have made a java GUI program and have added a jList on that
I have made a java application that stores data from a .csv file to
I have made a java program with GUI. Now I want to add a
I have made a java program with GUI and I want a stop button
I have made a custom java program to output a license and am trying
I have made a program in c and wanted to see, how much memory
So I have made a webservice that interfaces with a set of data contained
I have a JavaScript class that I have made and put it into its
I've been asked (as part of homework) to design a Java program that does
I have made a SVG image, or more like mini application, for viewing graphs

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.