#!/usr/bin/env node

/**

* Algorithm:

* Initially the number to find (say n) is 0, so the remainder is also zero

*

* While (rem doesn't have all the ones)

* Assuming the multiplication by hand

* When we multiply 333 with some 'x'

* We need to add the remainder, When we add the remainder the result should end

* with '1', so choose x such that, when multiplied and added with remainder the

* result should end with '1'.

*

* For example if remainder is 10, the last digit is 0. To

* get '1' after adding, we need to multiply 3 with 7

*/

function allOnes(n){

if(n == 0) return false;

while(n){

if(n % 10 != 1) return false;

n = parseInt(n / 10);

}

return true;

}

var num = 333

, rem = 0

, res = ''

, n = 0;

while(!allOnes(rem)){

switch(rem % 10){

case 0 :

n = num * 7 + rem;

break;

case 1 :

n = num * 10 + rem;

break;

case 2 :

n = num * 3 + rem;

break;

case 3 :

n = num * 6 + rem;

break;

case 4 :

n = num * 9 + rem;

break;

case 5 :

n = num * 2 + rem;

break;

case 6 :

n = num * 5 + rem;

break;

case 7 :

n = num * 8 + rem;

break;

case 8 :

n = num * 1 + rem;

break;

case 9 :

n = num * 4 + rem;

break;

}

res += '1';

rem = parseInt(n / 10);

}

console.log(rem + res);

### Like this:

Like Loading...

*Related*

can you please explain your logic ?

plz write your logic….

Dude, there is no point in pasting your code, unless the algorithm is not explained.So please give importance to above comments and write the logic.

Algo: Step 1: int cnt = 0, num = 1, r = 1; here cnt is for couting how many 1's will be there in the final ans and num is for initial number that we will divide with the given number. r stands for remainder which will be zero once we find our answer.say n is the given inputI'm assuming out require ans is infinite stream with all 1Step 2: while (r) { r = num%n; num = r*10 + 1; // This is what we usually do for dividing. cnt++; }step 3: for all i 0-cnt print 1