12
\$\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
asked Dec 29, 2017 at 15:46
\$\endgroup\$

8 Answers 8

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.

answered Dec 29, 2017 at 17:18
\$\endgroup\$
2
\$\begingroup\$

Jelly, 10 bytes

×ばつ

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?

×ばつ ~ 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.
answered Dec 29, 2017 at 15:58
\$\endgroup\$
1
  • \$\begingroup\$ Alternative. \$\endgroup\$ Commented Dec 29, 2017 at 16:45
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

answered Dec 29, 2017 at 16:47
\$\endgroup\$
0
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.

answered Dec 29, 2017 at 19:42
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog), 18 bytes

×ばつ⍨⍳⍵}

Try it online!

answered Dec 30, 2017 at 17:41
\$\endgroup\$
1
\$\begingroup\$

Jelly, 8 bytes

_×ばつ

Try it online!

How it works

_×ばつ Main link. Argument: n
_. Subtract 0.5; yield (n - 0.5).
 2€ Square each; yield [12, 22, ..., n2].
 ạ 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.
answered Dec 29, 2017 at 17:51
\$\endgroup\$
0
\$\begingroup\$

Husk, 13 bytes

*1D▼mo≠-.1mしろいしかく

Try it online!

Similar to the existing Jelly and APL answers.

answered Oct 29, 2020 at 10:58
\$\endgroup\$
0
\$\begingroup\$

Japt, 12 bytes

Ñ*UÇ2a1⁄2nUÃrm

Try it

Ñ*UÇ2a1⁄2nUÃrm :Implicit input of integer U
Ñ :Multiply by 2
 * :Multiply by
 UÇ : Map the range [0,U)
 2 : Square
 a : Absolute difference with
 1⁄2 : 0.5
 nU : Subtracted from U
 Ã : End map
 r : Reduce by
 m : Minimum
answered Oct 29, 2020 at 11:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.