Skip to main content
Added test cases
Source Link

#JavaScript ES6, 140140 142 bytes

f=s=>{t=sf=s=>s>9?eval("t=s.replace(/(.*)(.)$/,'$1'-$2*2'$&*2');for(i=-1;0>(n=eval(t[c='slice']u=t[c='slice'](i-4)));i&&u!=t;i--);return;n<0?n:f(s=t[c]t[c](0,i-4)+('0'.repeat(-i)+n)[c](i))>9?f(s"):+s}s

This is true arbitrary-precision math, even works on the largest test-case.

This function recursively removes the last digit from the string, then subtracts 2 * the last digit from the remaining numerical string by iteratively incrementing the amount of digits to apply to the minuend until the difference is positive. Then it appends that difference to the end of the string with appropriately padded 0s and calls itself recursively until its numerical value is less than or equal to 9.

  • Golfed 7 bytes thanks to @Neil (yes I know I gained 2 bytes but I fixed a few bugs that caused the function to freeze or return wrong output for some cases).

Would love some golfing help.

f=s=>s>9?eval("t=s.replace(/.$/,'-$&*2');for(i=-1;0>(n=eval(u=t[c='slice'](i-4)))&&u!=t;i--);n<0?n:f(t[c](0,i-4)+('0'.repeat(-i)+n)[c](i))"):s;[['1',1],['10',1],['100',1],['13',-5],['42',0],['2016',0],['9',9],['99',-9],['9999',-3],['12345',3],['700168844221',7],['36893488147419103232',-1],['231584178474632390847141970017375815706539969331281128078915168015826259279872',8]].map(a=>document.write(`<pre>${f(a[0])==a[1]?'PASS':'FAIL'} ${a[0]}=>${a[1]}</pre>`))

#JavaScript ES6, 140 bytes

f=s=>{t=s.replace(/(.*)(.)/,'$1-$2*2');for(i=-1;0>(n=eval(t[c='slice'](i-4)));i--);return(s=t[c](0,i-4)+('0'.repeat(-i)+n)[c](i))>9?f(s):+s}

This is true arbitrary-precision math, even works on the largest test-case.

This function recursively removes the last digit from the string, then subtracts 2 * the last digit from the remaining numerical string by iteratively incrementing the amount of digits to apply to the minuend until the difference is positive. Then it appends that difference to the end of the string with appropriately padded 0s and calls itself recursively until its numerical value is less than or equal to 9.

Would love some golfing help.

#JavaScript ES6, 140 142 bytes

f=s=>s>9?eval("t=s.replace(/.$/,'-$&*2');for(i=-1;0>(n=eval(u=t[c='slice'](i-4)))&&u!=t;i--);n<0?n:f(t[c](0,i-4)+('0'.repeat(-i)+n)[c](i))"):s

This is true arbitrary-precision math, even works on the largest test-case.

This function recursively removes the last digit from the string, then subtracts 2 * the last digit from the remaining numerical string by iteratively incrementing the amount of digits to apply to the minuend until the difference is positive. Then it appends that difference to the end of the string with appropriately padded 0s and calls itself recursively until its numerical value is less than or equal to 9.

  • Golfed 7 bytes thanks to @Neil (yes I know I gained 2 bytes but I fixed a few bugs that caused the function to freeze or return wrong output for some cases).

f=s=>s>9?eval("t=s.replace(/.$/,'-$&*2');for(i=-1;0>(n=eval(u=t[c='slice'](i-4)))&&u!=t;i--);n<0?n:f(t[c](0,i-4)+('0'.repeat(-i)+n)[c](i))"):s;[['1',1],['10',1],['100',1],['13',-5],['42',0],['2016',0],['9',9],['99',-9],['9999',-3],['12345',3],['700168844221',7],['36893488147419103232',-1],['231584178474632390847141970017375815706539969331281128078915168015826259279872',8]].map(a=>document.write(`<pre>${f(a[0])==a[1]?'PASS':'FAIL'} ${a[0]}=>${a[1]}</pre>`))

Post Undeleted by Patrick Roberts
Post Deleted by Patrick Roberts
Added explanation and golfed 2 bytes
Source Link

#JavaScript ES6, 142140 bytes

f=s=>eval("t=sf=s=>{t=s.replace(/(.*)(.)/,'$1-$2*2');for(i=-1;0>(n=eval(t.slicet[c='slice'](i-4)));i--);;return(s=t.slices=t[c](0,i-4)+('0'.repeat(-i)+n).slice[c](i))>70>9?f(s):+s")+s}

This is true arbitrary-precision math, even works on the largest test-case.

This function recursively removes the last digit from the string, then subtracts 2 * the last digit from the remaining numerical string by iteratively incrementing the amount of digits to apply to the minuend until the difference is positive. Then it appends that difference to the end of the string with appropriately padded 0s and calls itself recursively until its numerical value is less than or equal to 9.

Would love some golfing help.

#JavaScript ES6, 142 bytes

f=s=>eval("t=s.replace(/(.*)(.)/,'$1-$2*2');for(i=-1;0>(n=eval(t.slice(i-4)));i--);(s=t.slice(0,i-4)+('0'.repeat(-i)+n).slice(i))>70?f(s):+s")

This is true arbitrary-precision math, even works on the largest test-case.

#JavaScript ES6, 140 bytes

f=s=>{t=s.replace(/(.*)(.)/,'$1-$2*2');for(i=-1;0>(n=eval(t[c='slice'](i-4)));i--);return(s=t[c](0,i-4)+('0'.repeat(-i)+n)[c](i))>9?f(s):+s}

This is true arbitrary-precision math, even works on the largest test-case.

This function recursively removes the last digit from the string, then subtracts 2 * the last digit from the remaining numerical string by iteratively incrementing the amount of digits to apply to the minuend until the difference is positive. Then it appends that difference to the end of the string with appropriately padded 0s and calls itself recursively until its numerical value is less than or equal to 9.

Would love some golfing help.

Source Link

#JavaScript ES6, 142 bytes

f=s=>eval("t=s.replace(/(.*)(.)/,'$1-$2*2');for(i=-1;0>(n=eval(t.slice(i-4)));i--);(s=t.slice(0,i-4)+('0'.repeat(-i)+n).slice(i))>70?f(s):+s")

This is true arbitrary-precision math, even works on the largest test-case.