Smallest Zero One Number of multiples of n

ZeroOne is the number, whose digits are either 0 or 1. You have given a number n and need to find the smallest multiplier which is ZeroOne Number

For example for 4 smallest possible ZeroOne Multipler is 100

Solution

I have tried to solve this but really could not find the optimize solution.

#include <stdio.h>
int getZeroOneMultipler(int number) {
  long multiplier = 1;
  while(1) {
    long product = multiplier++ * number;
    long temp = product;
    while ( temp != 0 ) {
      int digit = temp % 10;
      if ( digit != 0 && digit != 1 ) {
        break;
      }
      temp /= 10;
    }
    if ( temp == 0 ) {
      return product;
    }
  }
}

int main(int argc, char** argv) {
  int i = 0;
  for ( i = 0; i < 100; i++) {
      printf("%d = %d\n", i, getZeroOneMultipler(i));
  }
  return 0;
}
Advertisements

5 thoughts on “Smallest Zero One Number of multiples of n

  1. Check this out:

    public class ZeroOne {
    
    	public static void main(String[] args) {
    
    		for (int input = 0; input < 100; input++) {
    		
    			if (input == 0) {
    				System.out.println("If n = " + input + " then ZeroOne = 0");
    				continue;
    			}
    			
    			int step = 1;
    			int zeroOne = 1;
    			boolean unknown = false;
    			
    			while (zeroOne % input != 0) {
    				try {
    					zeroOne = Integer.parseInt(Integer.toBinaryString(step++));
    				} catch (NumberFormatException nfe) {
    					System.out.println("If n = " + input + " then ZeroOne unknown");
    					unknown = true;
    					break;
    				}
    			}
    			
    			if (unknown) {
    				unknown = false;
    				continue;
    			}
    			
    			System.out.println("If n = " + input + " then ZeroOne = " + zeroOne);
    		}
    	}
    }
    
    • Vishal, thanks, this one is interesting, it cannot find answer for some inputs. just changing variable step and zeroOne to long will give me answer for 99 also.
      Thanks Again.

  2. Forgot to mention that the reason for the following line of code:

    zeroOne = Integer.parseInt(Integer.toBinaryString(step++));
    

    reason is that a “zero one” number is a “sum of powers of 10” or number in binary form used as a decimal.

  3. Here’s a function to calculate a ZeroOne instead of using parseInt() and toBinaryString()

    private static long getZeroOne(int number) {
    		
    	long result = 0;
    	double position = 0;
    		
    	while (number > 0) {
    			
    		boolean add = (number & 0x1) == 0x1;
    			
    		if (add) {
    			result += (long) Math.pow(10.0, position);
    		}
    			
    		position += 1.0;
    		number >>= 1;
    	}
    		
    	return result;
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s