| Bytes | Lang | Time | Link |
|---|---|---|---|
| 008 | Japt m | 251015T164837Z | Shaggy |
| 025 | Uiua | 251015T142747Z | ojdo |
| 012 | Dyalog APL | 250923T033725Z | Aaron |
| 014 | Ecmascript | 250925T203448Z | John Bol |
| 018 | Rust | 250925T195348Z | hoelzeli |
| 017 | R | 250924T162727Z | M-- |
| 014 | x86_64 machine code | 250924T113252Z | user1290 |
| 007 | 05AB1E | 250924T071821Z | Kevin Cr |
| 010 | GolfScript | 250923T141355Z | Glory2Uk |
| 020 | C gcc | 250923T134529Z | jdt |
| 1365 | Nibbles | 250922T225602Z | Dominic |
| 015 | Google Sheets | 250922T102412Z | doubleun |
Japt -m, 8 bytes
1½*6n°U²
1½*6n°U² :Implicit map of each U in input array
1½* :1.5 multiplied by
6n : 6 subtracted from
°U : Prefix increment U
² : Square
Uiua, 25 bytes
A naive implementation that actually sums the last 3 odd numbers. I like its basic "1-2-3" look:
/+↙¯3+1×2⇡ⁿ2÷2+1
Walkthrough
/+↙¯3+1×2⇡ⁿ2÷2+1
÷2+1 # convert line length (3,5,7) to line number (2,3,4)
ⁿ2 # square it to derive sequence length N
+1×2⇡ # convert to range of first N odd numbers
↙¯3 # take last 3 elements
/+ # calculate their sum
Dyalog APL, 14 13 12 bytes
-1 bytes thanks to @Tbw turning it into a tacit function -1 bytes thanks to @att mathing hard
New explanation:
1.5××⍨++⍨-5⍨
+⍨ # N+N
- # minus
5⍨ # just 5, as a function (for the train's sake)
+ # Plus
×⍨ # N^2
× # Times
1.5 #
💎
Created with the help of Luminespire.
Original explanation:
9-⍨1.5×2*⍨1+⊢
1+⊢ # 1 plus the input
2*⍨ # to the power of (backwards) 2
1.5× # times 1.5
💎
Created with the help of Luminespire.
Ecmascript, 15 14 bytes
n=>3*++n*n/2-9
x86_64 machine code, 14 bytes
0: 8d 47 01 lea eax,[rdi+0x1]
3: d1 e8 shr eax,1
5: f6 e0 mul al
7: 6b c0 06 imul eax,eax,0x6
a: 83 e8 09 sub eax,0x9
d: c3 ret
(M+1)>>1 converts an odd number M (the input) to it's ordinal placement in the series of odd numbers (i.e. 1 = 1, 3 = 2, 5 = 3, ect)
((n*n)*2)-1 gives the last number on the nth line (which corresponds to the ordinal placement of the number of elements in the line, since line 1 has 1 element, line 2 has 3, line 3 has 5, and so on) (i.e. 2 = 7, 3 = 17, 4 = 31)
Calculating the sum of the last 3 numbers on line n given the last number i is simply i + (i - 2) + (i - 4). Noting that the calculation for i ends in -1 we can move that out of i to give (i - 1) + (i - 3) + (i - 5) which can be simplified to (i*3)-9. Expanding i to ((n*n)*2) then gives ((n*n)*2*3)-9 = ((n*n)*6)-9.
Thus, the solution:
; convert input to ordinal placement
lea eax, [rdi+1]
shr eax
; square ordinal placement = i
mul al
; (i*6)
imul eax, eax, 6
; (i*6)-9
sub eax, 9
ret
Works for (odd) line lengths between 3 and 509 inclusive, due to the use of mul al (al * al -> ax), since 509 is the 255th odd number and the next one (511, the 256th) would no longer fit in al. Because we only need to be able to handle odd inputs between 1 and 100 (and it seems unspecified what to do for n == 1) this is fine.
05AB1E, 7 bytes
>n3;*9-
Uses the same formula as other answers.
I/O as a list.
Of course, the order of these operands can be changed slightly for the same byte-count and result:
>n6-3;*
Explanation:
> # Increase each value in the (implicit) input-list by 1
n # Square each
3* # Multiply each by 3
; # Halve each
9- # Subtract 9 from each
# (after which the resulting list is output implicitly)
6- # Subtract 6 from each
3;* # Multiply each by 1.5 (3 halved)
GolfScript, 10 bytes
This is not ruby though, but quite close to it.
~)2?3*2/9-
Explanation:
~ # take the input
) # increment by 1
2? # raise to power 2
3* # multiply by 3
2/ # divide by 2
9- # subtract 9
Google Sheets, 15 bytes
=(A1+1)^2*3/2-9
Assumes flexible input. Put \$N\$ in cell A1 and the formula in cell B1. Uses the logic shown in the spoiler. Doesn't cover the case \$N = 1\$ as that doesn't seem to be required by the rules. To cover that case (22 bytes):
=max(1,(A1+1)^2*3/2-9)
