Skip to main content
Commonmark migration
Source Link

#JavaScript, 93 91 87 bytes

JavaScript, 93 91 87 bytes

Throws an overflow error for 0 or 1, outputs 0 for 2.

n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)

Try it online

#JavaScript, 93 91 87 bytes

Throws an overflow error for 0 or 1, outputs 0 for 2.

n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)

Try it online

JavaScript, 93 91 87 bytes

Throws an overflow error for 0 or 1, outputs 0 for 2.

n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)

Try it online

deleted 1701 characters in body
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94

#JavaScript, 93 9191 87 bytes

n=>(g=x=>n-i?a[++x]|g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(a[c=x]=++ig[c=x]=++i,x>n)+g(2):0)(a=[i=c=2]i=c=2)

Try it onlineTry it online

n=>                                 :Anonymous function taking the input as an argument via parameter n
(g=x=>                              :Main function g which takes the number we want to test as an argument via parameter x
  n-i?                              :If n is greater than the iteration counter i then
    a[++x]                          :Increment x and check if the array a has an element at that index
                                    :(On the first iteration x will be the singleton array [2] which gets coerced to a number before being incremented)
    |                               :Or
    (h=(y,z)=>z?h(z,y%z):y)(x,c)    :Helper function which returns the GCD of x and c, the last number in he sequence so far
    <2                              :If that's less than 2 then c & x are co-prime
    ?                               :If either of those checks are truthy then
      g(x)                          :Call g again with the incremented value of x
    :(                              :Else
      a[c=x]=++i,                   :Assign x to c for the next iteration, increment the iteration counter and drop that value into the array at index x
      x>n                           :Test if x is greater than the original input
      )+g(2)                        :Add the result of running g again, with an initial value of 2
  :0                                :Else if n==i then return 0, add that to the final value and return the result
)(a=[i=c=2])                        :Immediately call g with an initial value of [2] which also gets assigned to a and initiate i & c with a value of 2

#JavaScript, 93 91 bytes

n=>(g=x=>n-i?a[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(a[c=x]=++i,x>n)+g(2):0)(a=[i=c=2])

Try it online

n=>                                 :Anonymous function taking the input as an argument via parameter n
(g=x=>                              :Main function g which takes the number we want to test as an argument via parameter x
  n-i?                              :If n is greater than the iteration counter i then
    a[++x]                          :Increment x and check if the array a has an element at that index
                                    :(On the first iteration x will be the singleton array [2] which gets coerced to a number before being incremented)
    |                               :Or
    (h=(y,z)=>z?h(z,y%z):y)(x,c)    :Helper function which returns the GCD of x and c, the last number in he sequence so far
    <2                              :If that's less than 2 then c & x are co-prime
    ?                               :If either of those checks are truthy then
      g(x)                          :Call g again with the incremented value of x
    :(                              :Else
      a[c=x]=++i,                   :Assign x to c for the next iteration, increment the iteration counter and drop that value into the array at index x
      x>n                           :Test if x is greater than the original input
      )+g(2)                        :Add the result of running g again, with an initial value of 2
  :0                                :Else if n==i then return 0, add that to the final value and return the result
)(a=[i=c=2])                        :Immediately call g with an initial value of [2] which also gets assigned to a and initiate i & c with a value of 2

#JavaScript, 93 91 87 bytes

n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)

Try it online

added 1703 characters in body
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94
n=>                                 :Anonymous function taking the input as an argument via parameter n
(g=x=>                              :Main function g which takes the number we want to test as an argument via parameter x
  n-i?                              :If n is greater than the iteration counter i then
    a[++x]                          :Increment x and check if the array a has an element at that index
                                    :(On the first iteration x will be the singleton array [2] which gets coerced to a number before being incremented)
    |                               :Or
    (h=(y,z)=>z?h(z,y%z):y)(x,c)    :Helper function which returns the GCD of x and c, the last number in he sequence so far
    <2                              :If that's less than 2 then c & x are co-prime
    ?                               :If either of those checks are truthy then
      g(x)                          :Call g again with the incremented value of x
    :(                              :Else
      a[c=x]=++i,                   :Assign x to c for the next iteration, increment the iteration counter and drop that value into the array at index x
      x>n                           :Test if x is greater than the original input
      )+g(2)                        :Add the result of running g again, with an initial value of 2
  :0                                :Else if n==i then return 0, add that to the final value and return the result
)(a=[i=c=2])                        :Immediately call g with an initial value of [2] which also gets assigned to a and initiate i & c with a value of 2
n=>                                 :Anonymous function taking the input as an argument via parameter n
(g=x=>                              :Main function g which takes the number we want to test as an argument via parameter x
  n-i?                              :If n is greater than the iteration counter i then
    a[++x]                          :Increment x and check if the array a has an element at that index
                                    :(On the first iteration x will be the singleton array [2] which gets coerced to a number before being incremented)
    |                               :Or
    (h=(y,z)=>z?h(z,y%z):y)(x,c)    :Helper function which returns the GCD of x and c, the last number in he sequence so far
    <2                              :If that's less than 2 then c & x are co-prime
    ?                               :If either of those checks are truthy then
      g(x)                          :Call g again with the incremented value of x
    :(                              :Else
      a[c=x]=++i,                   :Assign x to c for the next iteration, increment the iteration counter and drop that value into the array at index x
      x>n                           :Test if x is greater than the original input
      )+g(2)                        :Add the result of running g again, with an initial value of 2
  :0                                :Else if n==i then return 0, add that to the final value and return the result
)(a=[i=c=2])                        :Immediately call g with an initial value of [2] which also gets assigned to a and initiate i & c with a value of 2
added 69 characters in body
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94
Loading
added 52 characters in body
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94
Loading
added 1 character in body; edited body
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94
Loading
deleted 1 character in body
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94
Loading
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94
Loading