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

  • Home
  • SEARCH
  • 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 85525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:06:11+00:00 2026-05-10T22:06:11+00:00

This originally was a problem I ran into at work, but is now something

  • 0

This originally was a problem I ran into at work, but is now something I’m just trying to solve for my own curiosity.

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.

Memory is not an issue (within reason), just sheer processing speed.

This is the code I have written to do it mathematically:

private static int[] exponents = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };  private static boolean findMatch(int a, int b) {     if (b > a) return false;      if (a == b) return true;      int needleLength = getLength(b);      int exponent = exponents[needleLength];     int subNum;     while (a >= 1) {         subNum = a % exponent;          if (subNum == b)             return true;          a /= 10;     }     return false; }  private static int getLength(int b) {      int len = 0;      while (b >= 1) {         len++;         b /= 10;     }      return len; } 

Here’s the string method I’m using, which seems to trump the mathematical method above:

private static boolean findStringMatch(int a, int b) {           return String.valueOf(a).indexOf(String.valueOf(b)) != -1;       } 

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.

I’m really interested to see or hear anything anyone has to offer on this.

EDIT: When I say contains I mean can be anywhere, so for example, findMatch(1234, 23) == true

EDIT: 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.

  • 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. 2026-05-10T22:06:12+00:00Added an answer on May 10, 2026 at 10:06 pm

    This is along Kibbee’s line, but I got a little intrigued by this before he posted and worked this out:

    long mask ( long n ) {      long m   = n % 10;     long n_d = n;     long div = 10;     int  shl = 0;     while ( n_d >= 10 ) {          n_d /= 10;         long t = n_d % 10;         m |= ( t << ( shl += 4 ));     }     return m; }  boolean findMatch( int a, int b ) {      if ( b < a  ) return false;     if ( a == b ) return true;      long m_a = mask( a );    // set up mask O(n)     long m_b = mask( b );    // set up mask O(m)      while ( m_a < m_b ) {         if (( m_a & m_b ) == m_a ) return true;         m_a <<= 4;  // shift - fast!         if ( m_a == m_b ) return true;     }  // O(p)     return false; }         void testContains( int a, int b ) {      print( 'findMatch( ' + a + ', ' + b + ' )=' + findMatch( a, b )); }  testContains( 12, 120 ); testContains( 12, 125 ); testContains( 123, 551241238 ); testContains( 131, 1214124 ); testContains( 131, 1314124 ); 

    Since 300 characters is far too little to make an argument in, I’m editing this main post to respond to Pyrolistical.

    Unlike the OP, I wasn’t that surprised that a native compiled indexOf was faster than Java code with primitives. So my goal was not to find something I thought was faster than a native method called zillions of times all over Java code.

    The OP made it clear that this was not a production problem and more along the lines of an idle curiosity, so my answer solves that curiosity. My guess was that speed was an issue, when he was trying to solve it in production, but as an idle curiosity, ‘This method will be called millions and millions of times’ no longer applies. As he had to explain to one poster, it’s no longer pursued as production code, so the complexity no longer matters.

    Plus it provides the only implementation on the page that manages to find the ‘123’ in ‘551241238’, so unless correctness is an extraneous concern, it provides that. Also the solution space of ‘an algorithm that solves the problem mathematically using Java primitives but beats optimized native code’ might be EMPTY.

    Plus, it’s not clear from your comment whether or not you compared apples to apples. The functional spec is f( int, int )-> boolean, not f( String, String )-> boolean (which is kind of the domain of indexOf) . So unless you tested something like this (which could still beat mine, and I wouldn’t be awfully surprised.) the additional overhead might eat up some of that excess 40%.

    boolean findMatch( int a, int b ) {      String s_a = '' + a;     String s_b = '' + b;     return s_a.indexOf( s_b ) > -1; } 

    It does the same basic steps. log10( a ) encoding + log10( b ) encoding + actually finding the match, which is as well O(n) where n is the largest logarithm.

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

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://blog.rebeccamurphey.com/2007/12/04/anchor-based-url-navigation-with-jquery/ Never done this, but this site has pretty straight… May 11, 2026 at 5:42 pm
  • Editorial Team
    Editorial Team added an answer After much head-scratching and divining I suspect what you want… May 11, 2026 at 5:42 pm
  • Editorial Team
    Editorial Team added an answer It adds an indirection. With position independent code you have… May 11, 2026 at 5:42 pm

Related Questions

I'm using Delphi 2007 Pro. I have a runtime package that includes a number
I'm working on an application that runs on Windows Mobile 6 that needs to
For reasons of compatibility with an external product, I need to build a RCP
Edit: This makes alot more sense to me now that i've taken a step

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.