title: Finding numerical substrings mathematically, without string comparison
body: <p>This originally was a problem I ran into at work, but is now something I'm just trying to solve for my own curiosity.</p>
  
  <p>I want to find out if int 'a' contains the int 'b' in the most efficient way possible.  I wrote some code, but it seems no matter what I write, parsing it into a string and then using indexOf is twice as fast as doing it mathematically.</p>
  
  <p>Memory is not an issue (within reason), just sheer processing speed.</p>
  
  <p>This is the code I have written to do it mathematically:</p>
  
  <pre><code>private static int[] exponents = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
  
  private static boolean findMatch(int a, int b) {
      if (b &gt; a) return false;
  
      if (a == b) return true;
  
      int needleLength = getLength(b);
  
      int exponent = exponents[needleLength];
      int subNum;
      while (a &gt;= 1) {
          subNum = a % exponent;
  
          if (subNum == b)
              return true;
  
          a /= 10;
      }
      return false;
  }
  
  private static int getLength(int b) {
  
      int len = 0;
  
      while (b &gt;= 1) {
          len++;
          b /= 10;
      }
  
      return len;
  }
  </code></pre>
  
  <p>Here's the string method I'm using, which seems to trump the mathematical method above:</p>
  
  <pre><code>private static boolean findStringMatch(int a, int b) {      
      return String.valueOf(a).indexOf(String.valueOf(b)) != -1;      
  }
  </code></pre>
  
  <p>So although this isn't really required for me to complete my work, I was just wondering if anyone could think of any way to further optimize my way of doing it mathematically, or an entirely new approach altogether.  Again memory is no problem, I am just shooting for sheer speed.</p>
  
  <p>I'm really interested to see or hear anything anyone has to offer on this.</p>
  
  <p><strong>EDIT:</strong>  When I say contains I mean can be anywhere, so for example, findMatch(1234, 23) == true</p>
  
  <p><strong>EDIT:</strong> For everyone saying that this crap is unreadable and unnecessary: you're missing the point.  The point was to get to geek out on an interesting problem, not come up with an answer to be used in production code.</p>