title: Calculating Extremely Large Powers of 2
body: <p>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:</p>
  
  <pre><code>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 &lt; y; z++)
    {
     product = new byte[size];
     for (int i = 0; i &lt; a.length; i++)
     {
      for (int j = 0; j &lt; b.length; j++)
      {
       product[i+j] += (byte) (a[i] * b[j]);
       checkPlaceValue(i + j);
      }
     }
     b = product;
     for (int i = product.length - 1; i &gt; product.length - 2; i--)
     {
      if (product[i] != 0)
      {
       size++;
       if (delay &gt;= 500) 
       {
        delay = 0;
        System.out.print(".");
       }
       delay++;
      }
     }
    }
    String str = "";
    for (int i = (product[product.length-1] == 0) ? 
     product.length - 2 : product.length - 1; i &gt;= 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] &gt; 9)
    {
     byte remainder = (byte) (product[placeValue] / 10);
     product[placeValue] -= 10 * remainder;
     product[placeValue + 1] += remainder;
     checkPlaceValue(placeValue + 1);
    }
   }  
  }
  </code></pre>
  
  <p>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!</p>
  
  <p>Kyle</p>
  
  <p>P.S. I failed to mention that the output should be in base-10, not binary.</p>