Skip to main content
added 910 characters in body
Source Link
edc65
  • 32.3k
  • 3
  • 37
  • 90

#JavaScript (ES6) 250222 250

This can be golfed a little moreThis can be golfed a little more(done), but I like the fact that it is not limited to JS standard numbers (17 decimal digits of precision) and that it is fast.

Test running the snippet below in an EcmaScript 6 compliant browser (implementing arrow functions and the spread operator. Time is acceptable up to 9998 - IE Firefox)don't try 9999 and be patient with 999.

// functionAs Ma -complete Modulusprogram with arbitrary precision - a is aI/O stringvia ofpopup decimal digits M=for(a,b,q=1,t=0n=+prompt(a=[0],j=a.lengthq=[t=1])=>;t;){whilefor(j--c=1,t=i=0;i<a.length;i++)+a[j]a[i]=a[i]&c?t+=q:0:a[i]|c?(c=0,q=t+=q[i],1):c=0;c&&(q*10a[i]=c,t+=q[i]=q[i-1]*10%n)%b;return t%b;t%=n} // function D - Long division with arbitrary precision - a is a string of decimal digits D=.reverse(a,b,r='',z=0)=>[...a].map(a=>(z+=az+=[a],d=z/b|0n|0,z%=bz%=n,r||d?r+=d:0),r='',z=0)&&r;alert([r,a.join``]) // TestableAs functiona testable function f=n=>{ for(i=0;++i<1e7&&(z=M(v=i.toString(2)a=[0],n)q=[t=1];t;); { for(c=1,t=i=0;i<a.length;i++); return z a[i]=a[i]&c?['big']0:[Da[i]|c?(vc=0,n)t+=q[i],v] }1):c=0  c&&(a[i]=c,t+=q[i]=q[i-1]*10%n); // As a complete programt%=n  with I/O} via popup P=_=>alert a.reverse(f).map(prompta=>()z+=[a],d=z/n|0,z%=n,r||d?r+=d:0)[0],r='',z=0)  return [r,a.join``] }  // Test and timing T=~new Date  out = x => O.innerHTML += x + '\n' console.log=(...x)=>O.innerHTML+=x+'\n' setTimeout(_=>{ ;[2;[1,2,10, 21, 23, 98, 72, 9,99] 99, 999] .forEach(w=>console.log(wtest, f(w))i); => {  var t0 = ~new Date  console.log var result = f('Timetest) out('n='+test+' '+result+' time(ms) '+' + (Tt0-~new Date)) })},100) 
<pre id=O><id=O>Timing test cases ... </pre>

More readable

This is the first version, with modulus and long division as separated functions.

// function M - Modulus with arbitrary precision - a is a string of decimal digits M = (a, b, q = 1, t = 0, j = a.length) => { while (j--) + a[j] ? t += q : 0, q = (q * 10) % b; return t % b } // function D - Long division with arbitrary precision - a is a string of decimal digits D = (a, b, r = '', z = 0) => [...a].map(a => (z += a, d = z / b | 0, z %= b, r || d ? r += d : 0)) && r // Testable function f = n => { for (i = 0; ++i < 1e7 && (z = M(v = i.toString(2), n));); return z ? ['big'] : [D(v, n), v] } 

#JavaScript (ES6) 250

This can be golfed a little more, but I like the fact that it is not limited to JS standard numbers (17 decimal digits of precision) and that it is fast.

Test running the snippet below in an EcmaScript 6 compliant browser (implementing arrow functions and the spread operator - IE Firefox)

// function M - Modulus with arbitrary precision - a is a string of decimal digits M=(a,b,q=1,t=0,j=a.length)=>{while(j--)+a[j]?t+=q:0,q=(q*10)%b;return t%b} // function D - Long division with arbitrary precision - a is a string of decimal digits D=(a,b,r='',z=0)=>[...a].map(a=>(z+=a,d=z/b|0,z%=b,r||d?r+=d:0))&&r // Testable function f=n=>{ for(i=0;++i<1e7&&(z=M(v=i.toString(2),n));); return z?['big']:[D(v,n),v] } // As a complete program with I/O via popup P=_=>alert(f(prompt())[0]) // Test and timing T=~new Date   console.log=(...x)=>O.innerHTML+=x+'\n' ;[2,21,98,72,9,99].forEach(w=>console.log(w, f(w))); console.log('Time(ms) '+ (T-~new Date))
<pre id=O></pre>

#JavaScript (ES6) 222 250

This can be golfed a little more(done), but I like the fact that it is not limited to JS standard numbers (17 decimal digits of precision) and that it is fast.

Test running the snippet below in an EcmaScript 6 compliant browser. Time is acceptable up to 9998 - don't try 9999 and be patient with 999.

// As a complete program with I/O via popup for(n=+prompt(a=[0],q=[t=1]);t;){for(c=1,t=i=0;i<a.length;i++)a[i]=a[i]&c?0:a[i]|c?(c=0,t+=q[i],1):c=0;c&&(a[i]=c,t+=q[i]=q[i-1]*10%n);t%=n}a.reverse().map(a=>(z+=[a],d=z/n|0,z%=n,r||d?r+=d:0),r='',z=0);alert([r,a.join``]) // As a testable function f=n=>{ for(a=[0],q=[t=1];t;) { for(c=1,t=i=0;i<a.length;i++)  a[i]=a[i]&c?0:a[i]|c?(c=0,t+=q[i],1):c=0  c&&(a[i]=c,t+=q[i]=q[i-1]*10%n); t%=n  }  a.reverse().map(a=>(z+=[a],d=z/n|0,z%=n,r||d?r+=d:0),r='',z=0)  return [r,a.join``] }  // Test and timing out = x => O.innerHTML += x + '\n' setTimeout(_=>{ ;[1,2,10, 21, 23, 98, 72, 9, 99, 999] .forEach((test,i) => {  var t0 = ~new Date   var result = f(test) out('n='+test+' '+result+' time(ms) ' + (t0-~new Date)) })},100) 
<pre id=O>Timing test cases ... </pre>

More readable

This is the first version, with modulus and long division as separated functions.

// function M - Modulus with arbitrary precision - a is a string of decimal digits M = (a, b, q = 1, t = 0, j = a.length) => { while (j--) + a[j] ? t += q : 0, q = (q * 10) % b; return t % b } // function D - Long division with arbitrary precision - a is a string of decimal digits D = (a, b, r = '', z = 0) => [...a].map(a => (z += a, d = z / b | 0, z %= b, r || d ? r += d : 0)) && r // Testable function f = n => { for (i = 0; ++i < 1e7 && (z = M(v = i.toString(2), n));); return z ? ['big'] : [D(v, n), v] } 
added 139 characters in body
Source Link
edc65
  • 32.3k
  • 3
  • 37
  • 90

#JavaScript (ES6) 250

Using arbitrary precision math (operating on strings of decimal digits)

This can be golfed a little more, but I like the fact that it is not limited to JS standard numbers (17 decimal digits of precision) and that it is fast.

Test running the snippet below in an EcmaScript 6 compliant browser (implementing arrow functions and the spread operator - IE Firefox)

// function M - Modulus with arbitrary precision - a is a string of decimal digits M=(a,b,q=1,t=0,j=a.length)=>{while(j--)+a[j]?t+=q:0,q=(q*10)%b;return t%b} // function D - Long division with arbitrary precision - a is a string of decimal digits D=(a,b,r='',z=0)=>[...a].map(a=>(z+=a,d=z/b|0,z%=b,r||d?r+=d:0))&&r // Testable function f=n=>{ for(i=0;++i<1e7&&(z=M(v=i.toString(2),n));); return z?['big']:[D(v,n),v] } // As a complete program with I/O via popup P=_=>alert(f(prompt())[0]) // Test and timing T=~new Date console.log=(...x)=>O.innerHTML+=x+'\n' ;[2,21,98,72,9,99].forEach(w=>console.log(w, f(w))); console.log('Time(ms) '+ (T-~new Date))
<pre id=O></pre>

#JavaScript (ES6) 250

Using arbitrary precision math (operating on strings of decimal digits)

This can be golfed a little more, but I like the fact that it is not limited to JS standard numbers (17 decimal digits of precision) and that it is fast.

// function M - Modulus with arbitrary precision - a is a string of decimal digits M=(a,b,q=1,t=0,j=a.length)=>{while(j--)+a[j]?t+=q:0,q=(q*10)%b;return t%b} // function D - Long division with arbitrary precision - a is a string of decimal digits D=(a,b,r='',z=0)=>[...a].map(a=>(z+=a,d=z/b|0,z%=b,r||d?r+=d:0))&&r // Testable function f=n=>{ for(i=0;++i<1e7&&(z=M(v=i.toString(2),n));); return z?['big']:[D(v,n),v] } // As a complete program with I/O via popup P=_=>alert(f(prompt())[0]) // Test and timing T=~new Date console.log=(...x)=>O.innerHTML+=x+'\n' ;[2,21,98,72,9,99].forEach(w=>console.log(w, f(w))); console.log('Time(ms) '+ (T-~new Date))
<pre id=O></pre>

#JavaScript (ES6) 250

Using arbitrary precision math (operating on strings of decimal digits)

This can be golfed a little more, but I like the fact that it is not limited to JS standard numbers (17 decimal digits of precision) and that it is fast.

Test running the snippet below in an EcmaScript 6 compliant browser (implementing arrow functions and the spread operator - IE Firefox)

// function M - Modulus with arbitrary precision - a is a string of decimal digits M=(a,b,q=1,t=0,j=a.length)=>{while(j--)+a[j]?t+=q:0,q=(q*10)%b;return t%b} // function D - Long division with arbitrary precision - a is a string of decimal digits D=(a,b,r='',z=0)=>[...a].map(a=>(z+=a,d=z/b|0,z%=b,r||d?r+=d:0))&&r // Testable function f=n=>{ for(i=0;++i<1e7&&(z=M(v=i.toString(2),n));); return z?['big']:[D(v,n),v] } // As a complete program with I/O via popup P=_=>alert(f(prompt())[0]) // Test and timing T=~new Date console.log=(...x)=>O.innerHTML+=x+'\n' ;[2,21,98,72,9,99].forEach(w=>console.log(w, f(w))); console.log('Time(ms) '+ (T-~new Date))
<pre id=O></pre>

Source Link
edc65
  • 32.3k
  • 3
  • 37
  • 90

#JavaScript (ES6) 250

Using arbitrary precision math (operating on strings of decimal digits)

This can be golfed a little more, but I like the fact that it is not limited to JS standard numbers (17 decimal digits of precision) and that it is fast.

// function M - Modulus with arbitrary precision - a is a string of decimal digits M=(a,b,q=1,t=0,j=a.length)=>{while(j--)+a[j]?t+=q:0,q=(q*10)%b;return t%b} // function D - Long division with arbitrary precision - a is a string of decimal digits D=(a,b,r='',z=0)=>[...a].map(a=>(z+=a,d=z/b|0,z%=b,r||d?r+=d:0))&&r // Testable function f=n=>{ for(i=0;++i<1e7&&(z=M(v=i.toString(2),n));); return z?['big']:[D(v,n),v] } // As a complete program with I/O via popup P=_=>alert(f(prompt())[0]) // Test and timing T=~new Date console.log=(...x)=>O.innerHTML+=x+'\n' ;[2,21,98,72,9,99].forEach(w=>console.log(w, f(w))); console.log('Time(ms) '+ (T-~new Date))
<pre id=O></pre>