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

Edit Simplified. Correct bug of 0 missing

Defined as a function, then counting 10 more byte for output using alert()

Important Really this one is not valid according to the rules, because javascript can not handle big numbers. For instance with parameters 2,5 it can't find 2^2^2^2^2 (ie 2^65536). A number this big is 'Infinity' in javascript.

Edit Simplified. Correct bug of 0 missing

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

F=(n, k, g={})=>{ k += k - 1; // Count in base 6 - '0' means Value, '1'..'5' means operators // A valid expression starts with Value twice, then n operators intermixed with n-1 Values // x: Coded expression, l: Expression length, k Expression maxlimit length from param for (l = i = 1; x = i.toString(6), l += !!x[l], l<k; i++) if (x.split(0).length*2 == l+1) // check balancing values/operators { for (s = [n,n], p = l; o = x[--p];) // Calc stack starts with N,N if (-o) // If not '0' b=s.pop(),a=s.pop(), // Do calc s.push(o<2 ? a+b : o<3 ? a-b : o<4 ? a*b : o<5 ? a/b : Math.pow(a,b)) else // Push value s.push(n) // r = Math.floor(s.pop()); // Last result in stack r >= 0 & isFinite(r) & !isNaN(r) & !g[r] && (g[r]=x) // Put in hashtable avoiding duplicates } // Uncomment this to see the postfix expression list // for (i in g) console.log(i, [n,n,...[...g[i]].reverse().map(v=>v>0?'x+-*/^'[v]:n)]) return Object.keys(g) // Retust list of hashtable keys } 

Defined as a function, counting 10 more byte for output using alert()

Edit Simplified. Correct bug of 0 missing

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

F=(n, k, g={})=>{ k += k - 1; // Count in base 6 - '0' means Value, '1'..'5' means operators // A valid expression starts with Value twice, then n operators intermixed with n-1 Values // x: Coded expression, l: Expression length, k Expression max length for (l = i = 1; x = i.toString(6), l += !!x[l], l<k; i++) if (x.split(0).length*2 == l+1) // check balancing values/operators { for (s = [n,n], p = l; o = x[--p];) // Calc stack starts with N,N if (-o) // If not '0' b=s.pop(),a=s.pop(), // Do calc s.push(o<2 ? a+b : o<3 ? a-b : o<4 ? a*b : o<5 ? a/b : Math.pow(a,b)) else // Push value s.push(n) // r = Math.floor(s.pop()); // Last result in stack r >= 0 & isFinite(r) & !isNaN(r) & !g[r] && (g[r]=x) // Put in hashtable avoiding duplicates } // Uncomment this to see the postfix expression list // for (i in g) console.log(i, [n,n,...[...g[i]].reverse().map(v=>v>0?'x+-*/^'[v]:n)]) return Object.keys(g) // Retust list of hashtable keys } 

Edit Simplified. Correct bug of 0 missing

Defined as a function, then counting 10 more byte for output using alert()

Important Really this one is not valid according to the rules, because javascript can not handle big numbers. For instance with parameters 2,5 it can't find 2^2^2^2^2 (ie 2^65536). A number this big is 'Infinity' in javascript.

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

F=(n, k, g={})=>{ k += k - 1; // Count in base 6 - '0' means Value, '1'..'5' means operators // A valid expression starts with Value twice, then n operators intermixed with n-1 Values // x: Coded expression, l: Expression length, k Expression limit length from param for (l = i = 1; x = i.toString(6), l += !!x[l], l<k; i++) if (x.split(0).length*2 == l+1) // check balancing values/operators { for (s = [n,n], p = l; o = x[--p];) // Calc stack starts with N,N if (-o) // If not '0' b=s.pop(),a=s.pop(), // Do calc s.push(o<2 ? a+b : o<3 ? a-b : o<4 ? a*b : o<5 ? a/b : Math.pow(a,b)) else // Push value s.push(n) // r = Math.floor(s.pop()); // Last result in stack r >= 0 & isFinite(r) & !isNaN(r) & !g[r] && (g[r]=x) // Put in hashtable avoiding duplicates } // Uncomment this to see the postfix expression list // for (i in g) console.log(i, [n,n,...[...g[i]].reverse().map(v=>v>0?'x+-*/^'[v]:n)]) return Object.keys(g) // Retust list of hashtable keys } 
added 1218 characters in body
Source Link
edc65
  • 32.3k
  • 3
  • 37
  • 90

Javascript (E6) 252 => 352215 (315 - 2*50 bonus) 252

Defined as a function, counting 710 more byte for output using alert()

Golfed

F=(n,k,g={})=>{k+=k-1;for (l=i=1;x=i.toString(6),l+=!!x[l],l<k;i++)if(x.split(0).length*2==l+1){for(s=[n,n],p=l;o=x[--p];) if(-o)b=s.pop(),a=s.pop(),s.push(o<2?a+b:o<3?a-b:o<4?a*b:o<5?a/b:Math.pow(a,b));else s.push(x[p]=n); r=Math.floor(s.pop());r>=0&isFinite(r)&!isNaN(r)&!g[r]&&(g[r]=x)}return Object.keys(g)}; 

Edit Simplified. Correct bug of 0 missing

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

GolfedUsage

alert(F(4,3)) 

Output 1,2,3,4,5,8,12,16,20,32,64,252,256,260,1024,4096,65536,4294967296,1.3407807929942597e+154

Ungolfed

F=(n, k, g={})=>{ C=( k += k - 1; // Count in base 6 - '0' means Value, '1'..'5' means operators // A valid expression starts with Value twice, then n operators intermixed with n-1 Values // x: Coded expression,p l: Expression length,s)=>{ k Expression max length for (;o=x[p];p++l = i = 1; x = i.toString(6), l += !!x[l], l<k; i++) if (o==0x.split(0)s.pushlength*2 == l+1) // check balancing values/operators { for (x[p]=ns = [n,n], p = l; o = x[--p];);else // Calc stack starts with N,N if (-o) // If not '0'  b=s.pop(),a=s.pop(), // Do calc s.push(o<2 ? a+b : o<3 ? a-b : o<4 ? a*b : o<5 ? a/b : Math.pow(a,b)) r=Math else // Push value s.push(n) // r = Math.floor(s.pop());r>0&isFinite; // Last result in stack r >= 0 & isFinite(r) & !isNaN(r) & !g[r]&&g[r] && (g[r]=x) // Put in hashtable avoiding duplicates } k+=k-3;for (i=1;s=i.toString // Uncomment this to see the postfix expression list // for (6i in g),l=s console.splitlog(0).lengthi,!s[k];i++) if (l+l-1==s[n,n,.length)C(..[...g[i]].reverse(s+'00')].reversemap(v=>v>0?'x+-*/^'[v]:n),0,[]]);return return Object.keys(g) // Retust list of hashtable keys } 

Usage

alert(F(4,3)) 

Output 1,2,3,4,5,8,12,16,20,32,64,252,256,260,1024,4096,65536,4294967296,1.3407807929942597e+154

Original richFirst version this is more complicated but maybe easier to follow

Javascript (E6) 252 => 352 - 2*50 bonus

Defined as a function, counting 7 more byte for output using alert()

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

Golfed

F=(n,k,g={})=>{ C=(x,p,s)=>{for(;o=x[p];p++)if(o==0)s.push(x[p]=n);else b=s.pop(),a=s.pop(),s.push(o<2?a+b:o<3?a-b:o<4?a*b:o<5?a/b:Math.pow(a,b)) r=Math.floor(s.pop());r>0&isFinite(r)&!isNaN(r)&!g[r]&&(g[r]=x)} k+=k-3;for (i=1;s=i.toString(6),l=s.split(0).length,!s[k];i++) if (l+l-1==s.length)C([...(s+'00')].reverse(),0,[]);return Object.keys(g)} 

Usage

alert(F(4,3)) 

Output 1,2,3,4,5,8,12,16,20,32,64,252,256,260,1024,4096,65536,4294967296,1.3407807929942597e+154

Original rich version

Javascript (E6) 215 (315 - 2*50 bonus) 252

Defined as a function, counting 10 more byte for output using alert()

Golfed

F=(n,k,g={})=>{k+=k-1;for (l=i=1;x=i.toString(6),l+=!!x[l],l<k;i++)if(x.split(0).length*2==l+1){for(s=[n,n],p=l;o=x[--p];) if(-o)b=s.pop(),a=s.pop(),s.push(o<2?a+b:o<3?a-b:o<4?a*b:o<5?a/b:Math.pow(a,b));else s.push(x[p]=n); r=Math.floor(s.pop());r>=0&isFinite(r)&!isNaN(r)&!g[r]&&(g[r]=x)}return Object.keys(g)}; 

Edit Simplified. Correct bug of 0 missing

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

Usage

alert(F(4,3)) 

Output 1,2,3,4,5,8,12,16,20,32,64,252,256,260,1024,4096,65536,4294967296,1.3407807929942597e+154

Ungolfed

F=(n, k, g={})=>{  k += k - 1; // Count in base 6 - '0' means Value, '1'..'5' means operators // A valid expression starts with Value twice, then n operators intermixed with n-1 Values // x: Coded expression, l: Expression length, k Expression max length for (l = i = 1; x = i.toString(6), l += !!x[l], l<k; i++) if (x.split(0).length*2 == l+1) // check balancing values/operators { for (s = [n,n], p = l; o = x[--p];) // Calc stack starts with N,N if (-o) // If not '0'  b=s.pop(),a=s.pop(), // Do calc s.push(o<2 ? a+b : o<3 ? a-b : o<4 ? a*b : o<5 ? a/b : Math.pow(a,b))  else // Push value s.push(n) // r = Math.floor(s.pop()); // Last result in stack r >= 0 & isFinite(r) & !isNaN(r) & !g[r] && (g[r]=x) // Put in hashtable avoiding duplicates }  // Uncomment this to see the postfix expression list // for (i in g) console.log(i, [n,n,...[...g[i]].reverse().map(v=>v>0?'x+-*/^'[v]:n)]) return Object.keys(g) // Retust list of hashtable keys } 

First version this is more complicated but maybe easier to follow

added 697 characters in body
Source Link
edc65
  • 32.3k
  • 3
  • 37
  • 90

Javascript (E6) ~750252 => 352 - 1002*50 bonus

To be golfed. IDefined as a function, counting 7 more byte for output using alert()

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

Golfed

F=(n,k,g={})=>{ C=(x,p,s)=>{for(;o=x[p];p++)if(o==0)s.push(x[p]=n);else b=s.pop(),a=s.pop(),s.push(o<2?a+b:o<3?a-b:o<4?a*b:o<5?a/b:Math.pow(a,b)) r=Math.floor(s.pop());r>0&isFinite(r)&!isNaN(r)&!g[r]&&(g[r]=x)} k+=k-3;for (i=1;s=i.toString(6),l=s.split(0).length,!s[k];i++) if (l+l-1==s.length)C([...(s+'00')].reverse(),0,[]);return Object.keys(g)} 

Usage

alert(F(4,3)) 

Output 1,2,3,4,5,8,12,16,20,32,64,252,256,260,1024,4096,65536,4294967296,1.3407807929942597e+154

Original rich version

Display the postfix expression for each number

Javascript (E6) ~750 - 100 bonus

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

Javascript (E6) 252 => 352 - 2*50 bonus

Defined as a function, counting 7 more byte for output using alert()

To be golfed. I have an idea I want to share: use a postfix notation. In postfix any type of expression is made just of values and operators - no parentheses needed. The postfix notation can be translated to usual algebraic infix notation - but that's out of scope in this challenge.

Golfed

F=(n,k,g={})=>{ C=(x,p,s)=>{for(;o=x[p];p++)if(o==0)s.push(x[p]=n);else b=s.pop(),a=s.pop(),s.push(o<2?a+b:o<3?a-b:o<4?a*b:o<5?a/b:Math.pow(a,b)) r=Math.floor(s.pop());r>0&isFinite(r)&!isNaN(r)&!g[r]&&(g[r]=x)} k+=k-3;for (i=1;s=i.toString(6),l=s.split(0).length,!s[k];i++) if (l+l-1==s.length)C([...(s+'00')].reverse(),0,[]);return Object.keys(g)} 

Usage

alert(F(4,3)) 

Output 1,2,3,4,5,8,12,16,20,32,64,252,256,260,1024,4096,65536,4294967296,1.3407807929942597e+154

Original rich version

Display the postfix expression for each number

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