#!/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);

Advertisements