title: Cutting of the first 9 digits
body: <p>Alright I am trying to solve a challenge one of my friends gave me to do, well I've manged to cut the last 9 digits out of a <code>BigInteger</code> well I had a way to cut-off the first 9 but it was so slow, it was taking too long.</p>
  
  <p>The reason I need the first 9 and the last 9 is because I am looking for a <code>BigInteger</code> where the first and last are pandigital.</p>
  
  <p>If you do not understand what I mean say we have <code>n = new BigInteger("123456789987654321")</code> well I need to get the "123456789" and the "987654321" seperately, and I do <strong>NOT</strong> want to convert the BigInteger to a string because that's a VERY slow process.</p>
  
  <p>I am going for speed here, I am just stumped on this solution. I've heard something about using the Golden Ratio? Here is my code if you're interested.</p>
  
  <pre><code>import java.math.BigInteger;
  
  public class Main {
  
      public static void main(String...strings)
      {       
          long timeStart = System.currentTimeMillis();
          fib(350_000);
          long timeEnd = System.currentTimeMillis();
          System.out.println("Finished processing, time: " + (timeEnd - timeStart) + " milliseconds.");
      }
  
      public static BigInteger fib(int n)
      {
          BigInteger prev1 = BigInteger.valueOf(0), prev2 = BigInteger.valueOf(1);        
          for (int i = 0; i &lt; n; i++)
          {   
  
              // TODO: Check if the head is pandigital as well.
              BigInteger tailing9Digits = tailing9Digits(prev1);
              boolean tailPandigital = isPanDigital(tailing9Digits);
  
              if (tailPandigital)
              {                   
                  System.out.println("Solved at index: " + i);
                  break;
              }
              BigInteger savePrev1 = prev1;
              prev1 = prev2;
              prev2 = savePrev1.add(prev2);
          }
          return prev1;
      }
  
      public static BigInteger leading9Digits(BigInteger x)
      {
          // STUCK HERE.
          return null;
      }
  
      public static BigInteger tailing9Digits(BigInteger x) 
      {
          return x.remainder(BigInteger.TEN.pow(9));
      }
  
      static BigInteger[] pows = new BigInteger[16];
  
      static 
      {
          for (int i = 0; i &lt; 16; i++) 
          {
              pows[i] = BigInteger.TEN.pow(i);
          }
      }
  
      static boolean isPanDigital(BigInteger n) 
      {
          if (!n.remainder(BigInteger.valueOf(9)).equals(BigInteger.ZERO)) 
          {
              return false;
          }
          boolean[] foundDigits = new boolean[9];
  
  
          boolean isPanDigital = true;
          for (int i = 1; i &lt;= 9; i++) 
          {
              BigInteger digit = n.remainder(pows[i]).divide(pows[i - 1]);
              for (int j = 0; j &lt; foundDigits.length; j++) {
                  if (digit.equals(BigInteger.valueOf(j + 1)) &amp;&amp; !foundDigits[j])
                  {
                      foundDigits[j] = true;
                  }
              }
          }
  
          for (int i = 0; i &lt; 9; i++)
          {
              isPanDigital = isPanDigital &amp;&amp; foundDigits[i];
          }
  
          return isPanDigital;
      }
  }
  </code></pre>