11
\$\begingroup\$

Imagine enumerating the elements of rhombi which grow [1],[1,3,1],[1,3,5,3,1],… (only odd numbers such that they align nicely). This would look like as follows, note that you always begin enumerating with 1:

                   01
       1        02 03 04
 1   2 3 4   05 06 07 08 09          …
       5        10 11 12
                   13
(1) (1,3,1)    (1,3,5,3,1)    (1,3,5,7,5,3,1)   …

Now if you start summing the columns ([1],[2],[1,3,5],[4],[5],[2,6,10],…) you get the rhombus sequence. These are the first 100 elements of said sequence:

1,2,9,4,5,18,35,24,9,10,33,60,91,70,45,16,17,54,95,140,189,154,115,72,25,26,81,140,203,270,341,288,231,170,105,36,37,114,195,280,369,462,559,484,405,322,235,144,49,50,153,260,371,486,605,728,855,754,649,540,427,310,189,64,65,198,335,476,621,770,923,1080,1241,1110,975,836,693,546,395,240,81,82,249,420,595,774,957,1144,1335,1530,1729,1564,1395,1222,1045,864,679,490,297,100

IO

You are free to choose one of these three input/output methods (you won't need to handle invalid inputs):

  • Given an integer n output the nth element in that sequence (0- or 1-indexed, your choice)
  • Given an integer n output first n elements of that sequence
  • Print/return the sequence indefinetely

Testcases

Please refer to the first 100 terms above, here are some larger examples (1-indexed):

101 -> 101
443 -> 1329
1000 -> 49000    
1984 -> 164672
2017 -> 34289
2018 -> 30270
3000 -> 153000
\$\endgroup\$
3
\$\begingroup\$

Snowman, 72 bytes

((}1vn2nD#`nPnCdU!*2nM1`nR:#nSNaB#`nS2nMNdE;aM|#NdE2nP+#`nSNdE`|aA#nM*))

This is a subroutine that takes 1-indexed input and returns the corresponding output via the permavar +.

Try it online!

((        // begin subroutine
 }        // we'll only need 3 variables - b, e, g
 1vn2nD   // b = 0.5
 #`       // retrieve input and swap, now b = input and e = 0.5
 nP       // power, resulting in b=sqrt(input)
 nC       // ceiling - this gives the index i of the rhombus we want
 dU!*     // keep a copy of i in the permavar ! for later use
 2nM1`nR  // generate the range [1, 2i)
 :        // map the following block over the range...
  #nS     // subtract i, resulting in e.g. [-3 -2 -1 0 1 2 3] for i=4
  NaB     // absolute value - [3 2 1 0 1 2 3]
  #`nS    // subtract from i, giving [1 2 3 4 3 2 1]
  2nMNdE  // double and decrement, [1 3 5 7 5 3 1]
 ;aM      // map
 |        // shove the rhombus columns into g
 #NdE2nP  // b = (i-2)^2
 +#`      // move b into e and replace it with the original input
 nSNdE    // subtract the two and decrement, giving input-(i-2)^2-1
 `|aA     // this is the index into the rhombus columns that we want
 #nM*     // multiply by the original input and return
))

This uses basically the same algorithm as Mr. Xcoder's answer—the only difference is that here we only generate the columns of the rhombus we need, which is the ceil(sqrt(n))th one. To illustrate why this works, here are the inputs that correspond to each rhombus:

rhombus #   inputs
1           1
2           2 3 4
3           5 6 7 8 9
4           10 11 12 13 14 15 16
...

Note that the left column corresponds exactly to the ceiling of the square root of each element in the right column. To get the 1-based index from here, we simply subtract the square of the index of the previous rhombus.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 10 bytes

Ḥ€€’ŒBFị@×

Try it online!

A full program / monadic link returning the Nth term (1-indexed).

I noticed that each sum is the index of that column in the overall list of columns (the input itself) multiplied by the index of that column in the corresponding Rhombus, so no need to actually generate the rows.

How?

Ḥ€€’ŒBFị@× ~ Full program. I'll call the input N.

  €        ~ For each integer X in the range [1, N].
Ḥ€         ~ Double each integer in the range [1, X].
   ’       ~ Decrement (subtract 1).
    ŒB     ~ Bounce (element-wise). Palindromize each.
      F    ~ Flatten.
       ị@  ~ Get the element at the index N in our list.
         × ~ Multiply by N.
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES7), 42 41 bytes

Saved 1 byte thanks to @ovs

0-indexed. A closed-form expression derived from A004737.

n=>((k=n**.5|0)-Math.abs(n+k*~k))*2*++n+n

Test cases

let f =

n=>((k=n**.5|0)-Math.abs(n+k*~k))*2*++n+n

console.log(f(101-1))  // 101
console.log(f(443-1))  // 1329
console.log(f(1000-1)) // 49000    
console.log(f(1984-1)) // 164672
console.log(f(2017-1)) // 34289
console.log(f(2018-1)) // 30270
console.log(f(3000-1)) // 153000

\$\endgroup\$
2
\$\begingroup\$

Befunge, 62 60 bytes

&:1>:00p:*`|
00:-\*:g00:<>2-\-0v!`\g
*.@v+1<g00:<^*2g00_2*1+

Try it online!

Explanation

Source code with execution paths highlighted

* We start by reading the one-based element number, n, from stdin, and saving a duplicate.
* Then we determine which rhombus we're in, by counting up an integer, r, until r*r >= n.
* The column offset from the right hand side of the rhombus, c, is r*r - n.
* To get that offset reflected around the centre axis, we check if c >= r.
* And if it is, then the reflected c becomes r*2 - 2 - c.
* Once we have the reflected c, the sum of the column is simply (c*2 + 1) * n.

\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog), 18 bytes

{⍵×2×⌊/|⍵-.5+×⍨⍳⍵}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 8 bytes

_.ạ²€ṂḤ×

Try it online!

How it works

_.ạ²€ṂḤ×  Main link. Argument: n

_.        Subtract 0.5; yield (n - 0.5).
   ²€     Square each; yield [1², 2², ..., n²].
  ạ       Take the absolute differences of (n - 0.5) and each of the squares.
     Ṃ    Take the minimum.
      Ḥ   Unhalve; double the minimum.
       ×  Multiply the result by n.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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