17
\$\begingroup\$

In this challenge, your input is an integer value. Your task is to find the sum of the range of the sum of the range of n.

Examples:

Input -> Output
1     -> 1
2     -> 6
3     -> 21
4     -> 55
5     -> 120
6     -> 231
7     -> 406
8     -> 666
9     -> 1035
10    -> 1540

This challenge should be fairly simple to complete in most languages :)
Have fun!

\$\endgroup\$
2
  • 9
    \$\begingroup\$ OEIS: A002817 \$\endgroup\$
    – Shaggy
    May 16 at 9:04
  • 5
    \$\begingroup\$ Is the input an integer, or as in your example a strictly positive integer ? \$\endgroup\$ May 17 at 12:52

57 Answers 57

12
\$\begingroup\$

JavaScript (Node.js), 17 bytes

x=>(x*x-~x)**2>>3

Try it online!

Thanks Kevin Cruijssen for -1 byte.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ +x+1 can be -~x for -1 byte. \$\endgroup\$ May 16 at 9:42
11
\$\begingroup\$

C (gcc), 22 bytes

With unsequenced modification and access to n

f(n){n*=~n;n=--n*n/8;}

Try it online!

C (clang), 26 bytes

With unsequenced modification and access to n

f(n){n*=~n;return--n*n/8;}

Try it online!

C89, 28 bytes

f(n){n*=~n;return(n-2)*n/8;}

Try it online!

Explanation

f(n){n*=~n;return--n*n/8;}  // function which takes and returns an integer
     n*=~n;                 // n *= -(n + 1); here (n = -(n*n + n))
                 --n        // decrement n;   here (n = -(n*n + n + 1))
           return  n*n/8;   // return (n*n + n + 1)^2 / 8
\$\endgroup\$
10
\$\begingroup\$

Wolfram Language (Mathematica), 15 bytes

#@*#&@Tr@*Range

Try it online!

-1 byte from @att

Wolfram Language (Mathematica), 16 bytes

(f=Tr@*Range)@*f

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ #@*#&@Tr@*Range \$\endgroup\$
    – att
    May 19 at 20:11
9
\$\begingroup\$

APL (Dyalog Unicode), 6 bytes

Anonymous tacit prefix function.

+/∘⍳⍣2

Try it online!

+/ the sum

 of

 the range

⍣2 twice

\$\endgroup\$
7
\$\begingroup\$

Japt -x, 5 bytes

õ x õ

Try it

\$\endgroup\$
2
  • \$\begingroup\$ Just that the number of bytes encoding-dependent, utf-8 would be 7 bytes ;) \$\endgroup\$
    – Aconcagua
    May 17 at 7:18
  • \$\begingroup\$ @Aconcagua Actually, Japt is designed to be used with this encoding specifically, as the interpreter can read those raw bytes as they are in that codepage \$\endgroup\$
    – noodle man
    May 17 at 11:44
6
\$\begingroup\$

Brain-Flak, 32 bytes

({({}[()])()}{})
({({}[()])()}{})

Try it online!

(The newline isn't needed, but is included because it makes the answer pretty)

Polygonal numbers are one of the things that brain-flak uniquely excels at. This is just the code that calculates the N-th triangular number, repeated twice.

# Push (the sum of...)
(
    # While the top of stack is not 0...
    {
        # Decrement the top of the stack by 1
        # This adds n-1 to the running counter
        ({}[()])

        # Plus 1 (to the running counter)
        ()
    }

    # Pop the stack (which should contain a 0 now)
    {}
)
\$\endgroup\$
6
\$\begingroup\$

Haskell, 17 bytes

g.g
g n=sum[1..n]

Try it online!

Just implementing the definition seems to be shorter than arithmetical expressions such as

21 bytes

f n=((n^2+n+1)^2-1)/8

Try it online!

With floats:

17 bytes

g.g
g n=n*(n+1)/2

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Nibbles, 2 bytes (4 nibbles)

+,+,

Attempt This Online!

Explanation

+,+,$
    $  Input number (implicit)
   ,   Range from 1 to that number
  +    Sum
 ,     Range from 1 to that number
+      Sum
\$\endgroup\$
5
\$\begingroup\$

Binary Lambda Calculus, 125 bits = 15.625 bytes

01000001 11001110 10000101 01100001 10010000 00000101 10011111 01110010
11110111 10110000 00001110 01011110 11010000 10110001 00000100 00010

Verify it online!

Encodes the following lambda term:

main = 2 rangesum where
  succ = \n f x. f (n f x)
  pair = \x y f. f x y
  snd = \p. p (\x y. y)
  update = \p. p (\x y f. f (succ x) (x succ y))
  rangesum = \n. snd (n update (pair 1 0))

This is a function that takes a positive integer in Church numeral and outputs the answer in the same form. The numbers that appear in the Haskell-like code above are also Church numerals, e.g. 2 = \f x. f (f x).

\$\endgroup\$
4
\$\begingroup\$

Charcoal, 10 bytes

I÷⍘12320N⁸

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

   12320    Literal string `12320`
  ⍘         Interpret as base
        N   First input as a number
 ÷          Integer divide by
         ⁸  Literal integer `8`
I           Cast to string
            Implicitly print

The boring solution is 8 bytes:

IΣ…·Σ…·N

Try it online! Link is to verbose version of code. Explanation: InclusiveRange prefers two arguments, but fortunately we can arrange to use the one-argument version (twice, saving two bytes).

       N    First input as a number
     …·     Inclusive range from `1` to that
    Σ       Take the sum
  …·        Inclusive range from `1` to that
 Σ          Take the sum
I           Cast to string
            Implcitly print
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 4 bytes

LOLO

Try it online or verify all test cases.

Explanation:

L     # Push a list in the range [1, (implicit) input-integer]
 O    # Sum this list together
  L   # Pop and push a list in the range [1, sum]
   O  # Sum this list together again
      # (after which the result is output implicitly)

Laughing Out Loud! Out??

An equal-bytes alternative could be: 2FLO, where 2F is loop 2 times:
Try it online or verify all test cases.

\$\endgroup\$
1
  • \$\begingroup\$ I love that these make readable words \$\endgroup\$ May 16 at 22:02
4
\$\begingroup\$

Vyxal, 4 bytes

ɾ∑ɾ∑

Try it Online!

Uses the straightforward approach: ɾ gets the range from 1 through its argument, and sums. Rinse and repeat.

\$\endgroup\$
4
\$\begingroup\$

Pyth, 4 bytes

sSsS

Try it online!

Explanation

sSsSQ    # implicitly add Q
         # implicitly assign Q = eval(input())
   SQ    # range 1 to Q
  s      # sum
 S       # range 1 to result
s        # sum

Extra nice since when pronounced out loud it is the sound a python makes.

\$\endgroup\$
4
\$\begingroup\$

R, 19 bytes

\(n)sum(1:sum(1:n))

Attempt This Online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Attempt this online has a version of R that's on 4.3.0, so you can use it to include a link if you like. \$\endgroup\$
    – Giuseppe
    May 19 at 17:25
4
\$\begingroup\$

brainf**k, 110 bytes

,[>+>+>+<<<-]>>>[-[>+>+<<-]>>[-<<+>>]<<[-<+>]>[-<+>]<]<[>+>+>+<<<-]>>>[-[>+>+<<-]>>[-<<+>>]<<[-<+>]>[-<+>]<]<.

Try it online!

Probably not the shortest implementation but I'm too lazy to make this shorter lol...

Like most other b****fuck programs, it doesn't output the answer. Instead, it prints the ascii character corresponding to the numeric answer.

Check this out to see the running process.

\$\endgroup\$
3
  • \$\begingroup\$ I'd suggest changing the test case to this (where ascii 5 turns into x which is 120 in ascii), since when 4 is the input one expects 55 as the output, but your code returns 243 \$\endgroup\$ Jul 18 at 14:45
  • \$\begingroup\$ @CommandMaster Problem is, the code seems to tend to interpret numeric inputs as ascii values instead of their values. Try replacing "," with "++++" if you wanna test 4. You can view the running process on the online IDE I linked to. \$\endgroup\$ Jul 19 at 1:48
  • \$\begingroup\$ By the way, I suppose this could be golfed to around 80 bytes or so by introducing an outer loop, but my brain is already scorched so I'll leave that up to the more f**kable brains ;-P \$\endgroup\$ Jul 19 at 1:51
4
\$\begingroup\$

Desmos, 21 20 bytes

N=nn+n
f(n)=N(N+2)/8

Try It On Desmos!

\$\endgroup\$
3
\$\begingroup\$

Java, 18 17 bytes

n->(n=n*n-~n)*n/8

Port of @tsh' JavaScript answer, so make sure to upvote him/her as well.
-1 byte thanks to @tsh.

Try it online.

(n=n*n-~n) could alternatively be (n+=n*n+1) for the same byte-count:
Try it online.

Explanation:

n->               // Method with integer as both parameter and return-type
  (n=             //  Replace `n` with:
     n*n          //   `n` squared
        -~n)      //   `+n+1`
            *n    //  Square that new `n`
              /8  //  Integer-divide that by 8 to get the result

A literal implementation would be 56 bytes in comparison:

n->{int t=0;for(;n>0;)t+=n--;for(;t>0;)n+=t--;return n;}

Try it online.

Explanation:

n->{          // Method with integer as both parameter and return-type
  int t=0;    //  Temp-integer, starting at 0
  for(;n>0;)  //  Loop `n` down until it's 0:
    t+=n      //   Add the current `n` to the temp-integer `t`
        --;   //   And then decrease `n` by 1
              //  (at this point, `n=0`)
  for(;t>0;)  //  Now loop `t` down until it's 0:
    n+=t      //   Add the current `t` to `n`
        --;   //   And then decrease `t` by 1
  return n;}  //  Return the modified `n` as result
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Maybe you can write /8 instead of >>3 in Java. \$\endgroup\$
    – tsh
    May 16 at 10:06
  • \$\begingroup\$ @tsh I indeed can. Thanks. \$\endgroup\$ May 16 at 10:34
3
\$\begingroup\$

PARI/GP, 16 bytes

-2 byte thanks to @tsh.

n->(1+n^2+n)^2\8

Attempt This Online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I know nothing about PARI/GP. But seems n->(1+n^2+n)^2>>3 works. \$\endgroup\$
    – tsh
    May 16 at 9:41
3
\$\begingroup\$

Python, 23 bytes

-7 bytes thanks to c--'s suggestion based on tsh's answer.

lambda n:(n*n-~n)**2//8

Attempt This Online!

Python, 38 bytes

lambda n:sum(range(sum(range(n+1))+1))

Attempt This Online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 23 bytes porting tsh's JS answer \$\endgroup\$
    – c--
    May 16 at 15:11
3
\$\begingroup\$

Haskell, 21 bytes

t n=sum[1..sum[1..n]]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

x86-16 machine code, 13 bytes

00000000: 8ad8 43f6 e340 f7e0 b103 d3e8 c3         ..C..@.......

Listing:

8A D8       MOV   BL, AL        ; BL = x 
43          INC   BX            ; BL = x + 1 
F6 E3       MUL   BL            ; AX = x * (x + 1) 
40          INC   AX            ; AX = x * (x + 1) + 1 
F7 E0       MUL   AX            ; AX = AX * AX 
B1 03       MOV   CL, 3 
D3 E8       SHR   AX, CL        ; AX = AX >> 3
C3          RET                 ; return to caller

Callable with input n in AL, output in AX.

Uses the formula (x*(x+1)+1)^2>>3.

enter image description here

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 186 and later can use shr ax, 3 (3 bytes). (If you meant to stick to a tighter restriction of only 8088/8086 instructions, I assume you would have said that instead of x86-16, which includes modern x86 in 16-bit mode.) \$\endgroup\$ May 19 at 8:42
3
\$\begingroup\$

Julia, 16 bytes

!n=(n^2-~n)^2÷8

Attempt This Online!

The formula looks a bit shorter than directly writing out the sums.

\$\endgroup\$
3
\$\begingroup\$

Go, 41 37 bytes

func(n int)int{n=n*n-^n
return n*n/8}

Attempt This Online!

  • -4 bytes by @c--
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @c-- In Go, the tilde is not used as bitwise negation like in other languages; instead it's used for type constraints (~A is "types that have underlying type A"). Try running your own port and look at the compiler error. \$\endgroup\$
    – bigyihsuan
    May 17 at 15:23
3
\$\begingroup\$

Arturo, 18 bytes

$=>[∑1..∑1..&]

Try it!

\$\endgroup\$
3
\$\begingroup\$

Factor + math.unicode, 21 bytes

[ [1,b] Σ [1,b] Σ ]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Husk, 2 bytes

‼Σ

Try it online!

‼         # Apply function twice:
Σ         # Triangular number

(the more-boring ΣΣ is also 2 bytes...)

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

C, 65 bytes

f(n){int i=10;for(;i>1;)n+=i--;for(;n>1;)i+=n--;printf("%d",i);}

Earlier attempts

73 bytes

main(){int i=0,n=10;for(;n>0;)i+=n--;for(;i>0;)n+=i--;printf("%d\n",n);}

70 bytes

main(n){int i=10;for(;i>1;)n+=i--;for(;n>1;)i+=n--;printf("%d\n",i);}

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

><> (Fish), 13 bytes

  • -2 bytes thanks to Bubbler
i:1+*2,:1+*2,n;

Try it

Just the triangle number formula copied twice

\$\endgroup\$
1
  • \$\begingroup\$ 13 bytes: i:1+*:2+*8,n; using the formula expanded as n(n+1)(n(n+1)+2)/8. \$\endgroup\$
    – Bubbler
    May 16 at 7:34
2
\$\begingroup\$

Thunno 2, 4 bytes

RSRS

Attempt This Online!

Range, sum, range, sum.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nice one! You can also add the S flag and drop the last S. \$\endgroup\$
    – The Thonnu
    May 16 at 16:56
  • \$\begingroup\$ @TheThonnu Good point, I don’t think I’ll go through and edit my answer though, as this is such a trivial challenge. \$\endgroup\$
    – noodle man
    May 16 at 17:03
2
\$\begingroup\$

Lua, 24 bytes

print((...^2-~...)^2//8)

Try it online!

Based off tsh's answer, though it's been modified a bit to better fit Lua.

\$\endgroup\$

Your Answer

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

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