Skip to main content
Commonmark migration
Source Link

#JavaScript (ES6), 98 bytes

JavaScript (ES6), 98 bytes

###Test cases

Test cases

#JavaScript (ES6), 98 bytes

###Test cases

JavaScript (ES6), 98 bytes

Test cases

added the test cases generating 2 to 10
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650
let f =

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):k-=d<2)(++n)?g(n,k):n)(1,`)

console.log;[
  "(f)",                             // 2
  "(())",                           // 3
  "()()",                           // 4
  "((()))",                         // 5
  "()(()))",                         // 6
  "(()())",                         // =>7
 100 "()()()",                         // 8
console.log  "(f())(())",                       // 9
  "()((()))",                       // 10
  "()()((()))((()))",               // 100
  "(()(()(())))",                   // => 101
console.log(f(  "(()())(((())))(()(()))")),         // => 1001
console.log(f(  "(((((((())))))))")),               // => 5381
console.log(f(  "(()())((((()))))(()()(())(()))")), // => 32767
console.log(f(  "()()()()()()()()()()()()()()()"))  // 32768
]
.forEach(s => 32768console.log(s, '-->', f(s)))
let f =

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):k-=d<2)(++n)?g(n,k):n)(1,`)

console.log(f("()()((()))((()))"))               // => 100
console.log(f("(()(()(())))"))                   // => 101
console.log(f("(()())(((())))(()(()))"))         // => 1001
console.log(f("(((((((())))))))"))               // => 5381
console.log(f("(()())((((()))))(()()(())(()))")) // => 32767
console.log(f("()()()()()()()()()()()()()()()")) // => 32768
let f =

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):k-=d<2)(++n)?g(n,k):n)(1,`)

;[
  "()",                             // 2
  "(())",                           // 3
  "()()",                           // 4
  "((()))",                         // 5
  "()(())",                         // 6
  "(()())",                         // 7
  "()()()",                         // 8
  "(())(())",                       // 9
  "()((()))",                       // 10
  "()()((()))((()))",               // 100
  "(()(()(())))",                   // 101
  "(()())(((())))(()(()))",         // 1001
  "(((((((())))))))",               // 5381
  "(()())((((()))))(()()(())(()))", // 32767
  "()()()()()()()()()()()()()()()"  // 32768
]
.forEach(s => console.log(s, '-->', f(s)))
saved 2 bytes
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650

#JavaScript (ES6), 10098 bytes

Inspired by notjagan's Python answer. Turns the input expression into a huge and ugly executable string.

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):d>1k-=d<2)(++n)||--k?g(n,k):n)(1,`)

Merging the C and g functions into a single one may save some bytes, but it would require even more recursion.

###Test cases

let f =

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):d>1k-=d<2)(++n)||--k?g(n,k):n)(1,`)

console.log(f("()()((()))((()))"))               // => 100
console.log(f("(()(()(())))"))                   // => 101
console.log(f("(()())(((())))(()(()))"))         // => 1001
console.log(f("(((((((())))))))"))               // => 5381
console.log(f("(()())((((()))))(()()(())(()))")) // => 32767
console.log(f("()()()()()()()()()()()()()()()")) // => 32768

#JavaScript (ES6), 100 bytes

Inspired by notjagan's Python answer. Turns the input expression into a huge and ugly executable string.

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):d>1)(++n)||--k?g(n,k):n)(1,`)

Merging the C and g functions into a single one may save some bytes, but it would require even more recursion.

###Test cases

let f =

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):d>1)(++n)||--k?g(n,k):n)(1,`)

console.log(f("()()((()))((()))"))               // => 100
console.log(f("(()(()(())))"))                   // => 101
console.log(f("(()())(((())))(()(()))"))         // => 1001
console.log(f("(((((((())))))))"))               // => 5381
console.log(f("(()())((((()))))(()()(())(()))")) // => 32767
console.log(f("()()()()()()()()()()()()()()()")) // => 32768

#JavaScript (ES6), 98 bytes

Inspired by notjagan's Python answer. Turns the input expression into a huge and ugly executable string.

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):k-=d<2)(++n)?g(n,k):n)(1,`)

Merging the C and g functions into a single one may save some bytes, but it would require even more recursion.

###Test cases

let f =

s=>eval(s.split`)(`.join`)*(`.split`(`.join`(g=(n,k)=>(C=d=>n%--d?C(d):k-=d<2)(++n)?g(n,k):n)(1,`)

console.log(f("()()((()))((()))"))               // => 100
console.log(f("(()(()(())))"))                   // => 101
console.log(f("(()())(((())))(()(()))"))         // => 1001
console.log(f("(((((((())))))))"))               // => 5381
console.log(f("(()())((((()))))(()()(())(()))")) // => 32767
console.log(f("()()()()()()()()()()()()()()()")) // => 32768

Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650
Loading