8
\$\begingroup\$

Introduction

In my CS4 class I worked with another student to golf this lab as much as possible. I learned a lot about Ruby with it, and I am curious what optimizations could be done. :D

Challenge

A 3rd grade class is learning about odd and even numbers. To reinforce the concept, the teacher had the students write the odd numbers an odd number of times per line like so:

1
3 5 7
9 11 13 15 17
19 21 23 25 27 29 31

Notice the first line has 1 number, the next line 3, the next 5, and so forth containing all odd numbers in sequential order.

Given the number of odd numbers in a line, you are expected to write a program that will give the sum of the last 3 numbers of that line.

Input

The input is a sequence of lines, each having a single odd number N, where (1 < N < 100). N denotes the number of odd numbers in that particular line.

Output

For each value of N, you will print the sum of the last 3 odd numbers in that line.

Example Input

3
5
7

Example Output

15
45
87

My best attempt is 26 characters, and this took a lot of little optimizations.

I recommend working on this yourself, but here's our solution:

$<.map{p~_1.to_i**2*3/2-9}

Wheat Wizard
103k23 gold badges299 silver badges697 bronze badges
asked Sep 22 at 2:22
\$\endgroup\$
3
  • 10
    \$\begingroup\$ Welcome to CGCC! We generally don’t like when questions require pointlessly specific I/O methods. Please consider allowing any I/O method based on this list. \$\endgroup\$ Commented Sep 22 at 5:06
  • \$\begingroup\$ Of course! I will update this immediately, and update my solution as well! \$\endgroup\$ Commented Sep 22 at 22:34
  • 2
    \$\begingroup\$ Your solution can be reduced to 21 bytes if you write it as a function and further to 19 bytes if we remove p \$\endgroup\$ Commented Sep 23 at 10:55

12 Answers 12

5
\$\begingroup\$

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)
answered Sep 22 at 10:24
\$\endgroup\$
0
4
\$\begingroup\$

05AB1E, 7 bytes

>n3;*9-

Uses the same formula as other answers.
I/O as a list.

Try it online.

Of course, the order of these operands can be changed slightly for the same byte-count and result:

>n6-3;*

Try it online.

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)
answered Sep 24 at 7:18
\$\endgroup\$
4
\$\begingroup\$

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:

×ばつ⍨++⍨-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:

×ばつ2*⍨1+⊢­⁡​‎‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌­
 1+⊢ # ‎⁡1 plus the input
 2*⍨ # ‎⁢to the power of (backwards) 2
 ×ばつ # ‎⁣times 1.5
💎

Created with the help of Luminespire.

answered Sep 23 at 3:37
\$\endgroup\$
4
  • 1
    \$\begingroup\$ -1 byte by being tacit: 9-⍨1.5×2*⍨1+⊢ \$\endgroup\$ Commented Sep 23 at 14:00
  • 1
    \$\begingroup\$ Thanks @Tbw; I got so fixated on doing ×⍨ for squaring but then couldn't figure out how to get that into the rest of a tacit function. \$\endgroup\$ Commented Sep 23 at 19:12
  • \$\begingroup\$ You can get another -1 in a few ways \$\endgroup\$ Commented Oct 1 at 8:42
  • \$\begingroup\$ FOILed again. Thanks @att \$\endgroup\$ Commented Oct 1 at 22:57
3
\$\begingroup\$

Nibbles, 13 nibbles (6.5 bytes)

/*-^+$~~6 3~

Attempt This Online!

enter image description here

answered Sep 22 at 22:56
\$\endgroup\$
3
\$\begingroup\$

x86_64 machine code, 14 bytes

Running on Godbolt

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.

answered Sep 24 at 11:32
\$\endgroup\$
2
  • \$\begingroup\$ ((n*n)*6)-9 does not produce the correct output for input n, per the examples in the question. This appears to arise from misconstruing the question. It does not ask to compute the sum of the last numbers of the nth line. Rather, it asks to compute the sum of the last numbers of the line containing exactly n numbers. \$\endgroup\$ Commented Sep 25 at 19:55
  • \$\begingroup\$ @JohnBollinger Yes, that's exactly why the first step is to convert the odd number to it's ordinal placement in the series of odd numbers. I realise I did a small amount of slight of hand in renaming n, in the first conversion it refers to the number of elements on the line, then afterwards refers to the ordinal position. I'll edit the answer to make that clearer. \$\endgroup\$ Commented Sep 26 at 14:07
2
\$\begingroup\$

C (gcc), 20 bytes

f(n){n=++n*n*1.5-9;}

Try it online!

answered Sep 23 at 13:45
\$\endgroup\$
4
  • \$\begingroup\$ Undefined behaviour. The calculated value is undefined. \$\endgroup\$ Commented Sep 23 at 21:42
  • 1
    \$\begingroup\$ @gnasher729 codegolf.meta.stackexchange.com/questions/5486/… \$\endgroup\$ Commented Sep 24 at 0:33
  • \$\begingroup\$ How about using a statement expression instead, and specifying the language as GNU C? That shaves two bytes and gives you something that is well defined in the specified language (dialect). \$\endgroup\$ Commented Sep 25 at 20:12
  • \$\begingroup\$ @JohnBollinger statement expressions looks interesting, but I'm not sure how to apply it to this example? #define f(n){...}? \$\endgroup\$ Commented Sep 25 at 22:25
2
\$\begingroup\$

R, (削除) 23 (削除ここまで) 17 bytes

\(n)1.5*(n+1)^2-9

Attempt This Online!

answered Sep 24 at 16:27
\$\endgroup\$
2
\$\begingroup\$

Rust, (削除) 19 (削除ここまで) (thanks jdt!) 18 characters

|x|3*((x+2)*x-5)/2

try it out!

answered Sep 25 at 19:53
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to CGCC! You don't need to include the semicolon at the end of a lambda function. See this as an example. \$\endgroup\$ Commented Sep 26 at 20:02
2
\$\begingroup\$

Ecmascript, (削除) 15 (削除ここまで) 14 bytes

n=>3*++n*n/2-9
\$\endgroup\$
4
  • \$\begingroup\$ Why need u **2 than *n? \$\endgroup\$ Commented Sep 29 at 9:37
  • 1
    \$\begingroup\$ @l4m2, because it's ++n**2, as a shorter stand-in for (n+1)**2. The alternative with multiplication is not n*n, but rather (n+1)*(n+1). \$\endgroup\$ Commented Sep 29 at 13:42
  • 4
    \$\begingroup\$ The alternative is ++n*n, the second use of n is incremented \$\endgroup\$ Commented Sep 30 at 3:50
  • \$\begingroup\$ I see what you mean, @l4m2. Thanks. \$\endgroup\$ Commented Sep 30 at 12:22
1
\$\begingroup\$

GolfScript, 10 bytes

This is not ruby though, but quite close to it.

~)2?3*2/9-

Try it online!

Explanation:
~ # take the input
 ) # increment by 1
 2? # raise to power 2
 3* # multiply by 3
 2/ # divide by 2
 9- # subtract 9
answered Sep 23 at 14:13
\$\endgroup\$
1
\$\begingroup\$

Uiua, 25 bytes

A naive implementation that actually sums the last 3 odd numbers. I like its basic "1-2-3" look:

/+↙ ×ばつ2⇡n2÷2+1

Uiua Pad this online

Walkthrough

/+↙ ×ばつ2⇡n2÷2+1
 ÷2+1 # convert line length (3,5,7) to line number (2,3,4)
 n2 # square it to derive sequence length N
 ×ばつ2⇡ # convert to range of first N odd numbers
 ↙ ̄3 # take last 3 elements
/+ # calculate their sum
answered Oct 15 at 14:27
\$\endgroup\$
1
\$\begingroup\$

Japt -m, 8 bytes

11⁄2*6n°U2

Try it

11⁄2*6n°U2 :Implicit map of each U in input array
11⁄2* :1.5 multiplied by
 6n : 6 subtracted from
 °U : Prefix increment U
 2 : Square
answered Oct 15 at 16:48
\$\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.