I have a follow-up question here from my previous question on Math SE. I am lazy enough to explain the content again, so I have used a paraphraser to explain it below:

I was considering arbitrary series, springing up as a top priority, when I considered one potential series in my mind. It is as per the following:


The essential thought is, take a line of regular numbers \$\mathbb{N}\$ which goes till boundlessness, and add them. Something apparent here is that the most greatest biggest number \$\mathbb{N}_{max}\$ would be \$\mathbb{N}_{i}\$. In essential words, on the off chance that we go till number 5, \$\mathbb{N}_5\$ the level it comes to by summation is 5.

Further, continuing, we can get:


The essential ramifications here is that we knock the numbers by unambiguous \$\mathbb{N}\$. At start, we take the starting number, for our situation it is 1, we move once up and afterward once down. Then we do it two times, threefold, etc. So 1 3 2 as per my outline is one knock. At the closure \$\mathbb{N}\$ which is 2 here, we will hop it by 2 and make it low by 2. So it gets 2 5 12 7 4. Here, expect \$\mathbb{N}_i\$ as the quantity of incrementation, before it was 1, presently it is 2. We get various sets, with various terms, however absolute number of terms we overcome this would be \$2 \mathbb{N}_i + 1\$. Presently, it will begin from 4, continue taking 3 leaps prior to arriving by three terms. By this, we get series featured by circles in that three-sided exhibit as:

1, 3, 2, 5, 12, 7, 4, 9, 20, 44, 24, 13, 7, 15, 32, 68, 144, 76, 40, 21, 11, 23, 48, 100, 208, 432, 224, 116, 60, 31, 16...

The series appear to be disparate, my particular inquiry this is the way to address this series in Numerical terms.

Challenge: Implement the algorithm which can build this series.

Scoring Criteria: It is ranked by fastest-algorithm so the answer with lowest time complexity is considered (time complexity is loosely allowed to be anywhere) but the program must have been running accurate result.


6 Answers 6


Charcoal, 21 bytes


Try it online! Link is to verbose version of code. Outputs the nth term. Explanation:

Nθ                      First input as a number
      θ                 First input
     ⊕                  Incremented
    ×                   Multiplied by
        ²               Literal integer `2`
       X                Raised to power
           θ            First input
         ↔⁻             Absolute difference with
                 θ      First input
                ₂       Square root
               ⌈        Ceiling
              ⊖         Decremented
             ×          Multiplied by
                    θ   First input
                   ₂    Square root
                  ⌈     Ceiling
   ⊘                    Halved
  I                     Cast to string

30 bytes in integer arithmetic:


Try it online! Link is to verbose version of code.

  • 3
    \$\begingroup\$ @AiraThunberg I derived it myself without looking anything up so for all I know it may only be an independent rediscovery. \$\endgroup\$
    – Neil
    Commented Feb 26, 2023 at 19:55

Jelly, 13 bytes

Port of Neil's answer (I was working towards it myself but he nailed it).

Unsure of the complexity of this closed-form formula, but think it is \$O(M(B(n))B(n))\$ where \$n\$ is the input, \$M(x)\$ is the multiplication of two \$x\$ bit numbers and \$B(x)\$ is the bit-length of \$x\$.


A monadic Link that accepts \$n\$ and yields \$a(n)\$.

Try it online!


½Ċ’×$‘ạ⁸’2*ב - Link: positive integer, n
½             - square-root
 Ċ            - ceiling
    $         - last two links as a monad - f(x=that):
  ’           -   decrement (x)
   ×          -   (that) multiply (x)
     ‘        - increment (that) -> A
       ⁸      - chain's left argument = n
      ạ       - (A) absolute difference (n)
        ’     - decrement (that) -> E
         2    - two
          *   - (2) exponentiate (E)
           ×  - (that) multiply (n)
            ‘ - increment

To avoid floating point arithmetic we could instead do it in  17 16  14 bytes (-2 thanks to Neil!):


Try it online!

This replaces ½Ċ’×$ with ’ƽ‘×$ and moves the decrement out of the exponent, instead halving at the end (H).

’ƽ‘×$ - chain: integer n
’      - decrement -> n-1
 ƽ    - integer-square-root (n-1) (uses only integer arithmetic)
     $ - last two links as a monad - f(x=that):
   ‘   -   increment (x)
    ×  -   (that) multiply (x)
  • \$\begingroup\$ square-root ceiling decrement is the same as decrement square-root floor for positive integer n which doesn't make a difference in floating point but in integer it should save you some bytes. \$\endgroup\$
    – Neil
    Commented Feb 26, 2023 at 22:58
  • \$\begingroup\$ (just tried it and ’ƽ‘×$‘ạ⁸2*בH seems to work for 14 bytes) \$\endgroup\$
    – Neil
    Commented Feb 26, 2023 at 23:01
  • \$\begingroup\$ Thanks @Neil! Yeah, that is a valid simplification. \$\endgroup\$ Commented Feb 27, 2023 at 20:25

C (gcc), 97 + 3 bytes

+3 bytes from -lm. Runs in O(n) time, where n is the index.

f(M,n,v,l,c){l=0;for(n=c=1;M--;l+=v&&v--?:-1)n=((v=l?v:c++)?2*n:n/2+.5)+pow(2,v?l:l-2);return n;}

Try it online!

  • \$\begingroup\$ Flags don't count for extra bytes anymore, so this is just 96 \$\endgroup\$ Commented Mar 5, 2023 at 20:05
  • \$\begingroup\$ Suggest l-!v*2 instead of v?l:l-2 \$\endgroup\$
    – ceilingcat
    Commented Mar 19, 2023 at 8:01

05AB1E, 13 bytes


Another port of @Neil's Charcoal answer, so make sure to upvote him as well!

Try it online or verify all test cases.
The TIO-links use the legacy version of 05AB1E, because the regular 05AB1E on TIO is out-of-date, and still contains an already fixed bug when using o on decimal values ending with .0.


Basically implements the formula found by @Neil:

$$a(n) = \frac{(n+1)\times 2^{\left|n-\left\lceil\sqrt{n}\right\rceil\times(\left\lceil\sqrt{n}\right\rceil-1)-1\right|}}{2}$$

t              # Get the square root of the (implicit) input-integer
 î             # Ceil it
  D            # Duplicate it
   <           # Decrease the copy by 1
    *          # Multiply the two together
     -         # Subtract this from the (implicit) input-integer
      <        # Decrease that by 1
       Ä       # Get the absolute value of that
        o      # Take 2 to the power that
         I>    # Push the input+1
           *   # Multiply the two together
            ;  # Halve it
               # (after which the result is output implicitly)

Mathematica, 53 bytes


$$ f(n)=\frac{(n+1) \times 2^{|n-\lceil\sqrt{n}\rceil \times(\lceil\sqrt{n}\rceil-1)-1|}}{2} $$


Try it online!


JavaScript, 49 bytes

Port of Neil's answer



$$ f(n) = \frac{(n+1)\times 2^{\left|n-\left\lceil\sqrt{n}\right\rceil\times(\left\lceil\sqrt{n}\right\rceil-1)-1\right|}}{2} $$

Try it:



Upd 54 -> 49

Thanks to Arnauld for the tip to reduce bytes count

  • \$\begingroup\$ 49 bytes \$\endgroup\$
    – Arnauld
    Commented Mar 6, 2023 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.