16
\$\begingroup\$

Consider a triangle where the Nth row (1-indexed) is the array of the first N positive integer powers of N. Here are the first few rows:

N | Triangle

1 | 1
2 | 2 4
3 | 3 9 27
4 | 4 16 64 256
5 | 5 25 125 625 3125
...

Now if we concatenate those powers into a single sequence, we get OEIS A075363:

1, 2, 4, 3, 9, 27, 4, 16, 64, 256, 5, 25, 125, 625, 3125, 6, 36, 216, 1296, 7776, 46656 ...

Given an integer N, your task is to return the Nth term of this sequence. You can choose either 0 or 1-indexing.

Test cases

1-indexed:

N  -> Output

1  -> 1
2  -> 2
3  -> 4
5  -> 9
10 -> 256
12 -> 25
15 -> 3125

0-indexed:

N  -> Output

0  -> 1
1  -> 2
2  -> 4
4  -> 9
9  -> 256
11 -> 25
14 -> 3125

Note that these loopholes are forbidden by default. This is , thus the shortest valid submission in each language wins!

\$\endgroup\$
3
  • \$\begingroup\$ I think there is some error in test cases: in 1-indexed 10 should be 256; in 0-indexed 9 should be 256. \$\endgroup\$ Commented Nov 19, 2017 at 19:01
  • \$\begingroup\$ Can we have trailing spaces? \$\endgroup\$
    – Stan Strum
    Commented Nov 19, 2017 at 19:09
  • \$\begingroup\$ @StanStrum Yes. \$\endgroup\$
    – Mr. Xcoder
    Commented Nov 19, 2017 at 19:10

23 Answers 23

7
\$\begingroup\$

Python 3, 39 bytes

1-indexed

f=lambda x,n=1:n**x*(x<=n)or f(x-n,n+1)

Try it online!

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

Husk, 7 bytes

!ṁṠM^ḣN

Try it online!

1-indexed

Explanation:

      N   Get the list of all natural numbers
 ṁ        Map over each n in that list and then concatenate
  Ṡ  ḣ      Create the range [1,n] then ...
   M^       raise n to the power of each
!         Index into that sequence
\$\endgroup\$
5
\$\begingroup\$

Wolfram Language (Mathematica), 32 bytes

r=Range;Flatten[r@#^r@r@#][[#]]&

Try it online!

Ranges to the power of ranges of ranges...

\$\endgroup\$
1
  • \$\begingroup\$ Range@Range@3 returns {{1}, {1, 2}, {1, 2, 3}}?! Damn, Mathematica's list-threading is insane. \$\endgroup\$ Commented Nov 21, 2017 at 4:25
4
\$\begingroup\$

R, 40 bytes

-2 bytes porting Halvard Hummel's answer

function(N){while(N>T){N=N-T;T=T+1};T^N}

Try it online!

R, (original answer) 42 bytes

function(N)(rep(1:N,1:N)^sequence(1:N))[N]

Try it online!

1-indexed.

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

APL (Dyalog), 15 13 10 bytes

3 bytes saved thanks to @Adám

⊢⊃∘∊⍳*⍳¨∘⍳

Try it online!

How?

⍳¨∘⍳ - create a range for each number in the range of input

⍳* - raise each number in the range of input to the corresponding powers

- flatten

⊢⊃ - pick the nth element

\$\endgroup\$
10
  • \$\begingroup\$ Trying it online the code appears to be 17 bytes and it does not appear to accept a scalar integer argument or produce a single integer output \$\endgroup\$
    – Graham
    Commented Nov 19, 2017 at 18:59
  • 1
    \$\begingroup\$ @Graham Online, the code is assigned to a variable f (thus f← is 2 more bytes, which is not counted here), and there is a test harness that returns the results from 1 to 10. \$\endgroup\$ Commented Nov 19, 2017 at 19:07
  • \$\begingroup\$ Surely you must count all bytes including any such assignments and those required to take in the input according to the question spec and to output according to the spec. My APL answer prompts for screen input. \$\endgroup\$
    – Graham
    Commented Nov 19, 2017 at 19:22
  • \$\begingroup\$ @Graham this is a dyalog APL dfns. It does not require assignment to be applied on any input, the TIO outlet is just for comfortable view \$\endgroup\$
    – Uriel
    Commented Nov 19, 2017 at 19:34
  • \$\begingroup\$ On that basis can I assume I can assign n within my workspace prior to running the APL one liner and thereby save 7 bytes, I am not sure our fellow competitors will accept that. \$\endgroup\$
    – Graham
    Commented Nov 19, 2017 at 22:45
3
\$\begingroup\$

Jelly, 7 bytes

*R$€Ẏ⁸ị

Try it online!

-1 thanks to Mr. Xcoder.

1-indexed.

\$\endgroup\$
3
  • \$\begingroup\$ Awesome code right here. \$\endgroup\$ Commented Nov 19, 2017 at 17:39
  • \$\begingroup\$ Getting rid of fancy syntax with ", my own solution is 7 bytes: *R$€F⁸ị \$\endgroup\$
    – Mr. Xcoder
    Commented Nov 19, 2017 at 17:44
  • \$\begingroup\$ @Mr.Xcoder Thanks, although I had already removed the ", but still. That's what you get for having to study history :/ \$\endgroup\$ Commented Nov 19, 2017 at 17:44
3
\$\begingroup\$

Haskell, 30 28 bytes

Saved 2 bytes thanks to xnor.

([n^m|n<-[1..],m<-[1..n]]!!)

Try it online!

0-indexed

\$\endgroup\$
2
  • 1
    \$\begingroup\$ It's actually shorter to use the list comp [n^i|n<-[1..],i<-[1..n]]. \$\endgroup\$
    – xnor
    Commented Nov 19, 2017 at 18:33
  • \$\begingroup\$ Oh, I was comparing it to the >>= form ... \$\endgroup\$
    – H.PWiz
    Commented Nov 19, 2017 at 18:34
3
\$\begingroup\$

MATL, 9 bytes

:t!^RXzG)

Indexing is 1-based. Try it online! Or verify all test cases.

Explanation

Consider input 5 as an example.

:     % Implcit input n. Push range [1 2 ... n]
      % STACK: [1 2 3 4 5]
t!^   % Matrix of all pair-wise powers
      % STACK: [1    2    3    4    5;
                1    4    9   16   25;
                1    8   27   64  125;
                1   16   81  256  625;
                1   32  243 1024 3125]
R     % Upper triangular matrix
      % STACK: [1    2    3    4    5;
                0    4    9   16   25;
                0    0   27   64  125;
                0    0    0  256  625;
                0    0    0    0 3125]
Xz    % Nonzeros. Reads values in column-major order
      % STACK: [1; 2; 4; 3; 9; ...; 625; 3125]
G)    % Get n-th entry (1-based). Implcit display
      % STACK: 9
\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog), 14 12 bytes

{⍵⌷∊*∘⍳⍨¨⍳⍵}

Try it online!

Uses 1-indexing

Saved 2 bytes with ↑,/ → ∊, taken from Graham's answer

Note that in the test link, the code requires an extra f←, but this is not counted as per our rules.

\$\endgroup\$
4
  • \$\begingroup\$ Very clever use of with . \$\endgroup\$
    – Adám
    Commented Nov 19, 2017 at 20:44
  • \$\begingroup\$ {⍵⌷∊*∘⍳⍨¨⍳⍵}⊢⌷∘∊((*∘⍳)⍨¨⍳)⊢⌷∘∊(⍳(*∘⍳)¨⍳)⊢⌷∘∊⍳*∘⍳¨⍳ \$\endgroup\$
    – Adám
    Commented Nov 19, 2017 at 20:51
  • \$\begingroup\$ I would post that... if I had any idea how that worked \$\endgroup\$
    – H.PWiz
    Commented Nov 19, 2017 at 20:53
  • \$\begingroup\$ is to a tacit function what is to a dfn. The between and is needed because is called monadically, so it is index the enlisted. And we change f⍨⍳ into ⍳ f ⍳ to avoid calling f (*∘⍳¨) monadically (whenever and ¨ are adjacent, they may swap position). \$\endgroup\$
    – Adám
    Commented Nov 22, 2017 at 23:41
3
\$\begingroup\$

Pyth, 8 bytes

@s^RSdSh

Try it here.

-1 thanks to Steven H..

0-indexed.

\$\endgroup\$
1
  • \$\begingroup\$ @s^RSdSh to golf a byte off. \$\endgroup\$
    – Steven H.
    Commented Nov 19, 2017 at 21:09
2
\$\begingroup\$

05AB1E, 9 bytes

ƒNDLm`}I@

Try it online!

Explanation

1-indexed.

ƒ           # for N in range [0 ... input]
 N          # push N
  DL        # push range [1 ... N]
    m       # raise N to the power of each in [1 ... N]
     `      # flatten to stack
      }     # end loop
       I@   # get the element at index (input)

Alternative solution over a list instead of a loop

ÝεDLm}˜sè
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 29 bytes

{({|($++X**1..$++)}...*)[$_]}

Test it

Expanded:

{  # bare block lambda with implicit parameter 「$_」

  (  # generate the sequence

    {  # code block used to generate each value in the sequence

      |(         # slip the values into the outer sequence

        $++      # post-incremented anonymous state value
        X**      # cross using &infix:«**»
        1 .. $++ # from 1 to post-incremented anonymous state value

      )
    }

    ...          # keep generating values until

    *            # never stop

  )[ $_ ]        # index into the sequence (0-based)
}
\$\endgroup\$
1
\$\begingroup\$

Ruby, 34 bytes

->n{w=0;n-=w+=1until n<0;w**=w-~n}

Try it online!

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

JavaScript, 30 bytes

-1 byte thanks to Nahuel Fouilleul

f=(x,n=1)=>x>n?f(x-n,n+1):n**x

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ -1 byte inverting the test f=(x,n=1)=>x>n?f(x-n,n+1):n**x \$\endgroup\$ Commented Nov 20, 2017 at 13:27
  • \$\begingroup\$ @NahuelFouilleul of course, thank you! \$\endgroup\$
    – FlipTack
    Commented Nov 20, 2017 at 18:11
0
\$\begingroup\$

Python 2, 57 bytes

lambda N:[n**-~e for n in range(N+2)for e in range(n)][N]

Try it online! (0-indexed.)

0-indexed alternative 74 bytes long version.

\$\endgroup\$
0
\$\begingroup\$

Jelly,  15  12 bytes

Probably not optimal.

-3 thanks to Erik the Outgolfer's golfing.

=RṁR€T€µT*FḢ

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Wow, you must be really tired today. \$\endgroup\$ Commented Nov 19, 2017 at 17:39
  • \$\begingroup\$ I'm actually really ill! \$\endgroup\$ Commented Nov 19, 2017 at 17:41
  • 10
    \$\begingroup\$ Sorry! Hope you get better soon! \$\endgroup\$ Commented Nov 19, 2017 at 17:41
  • \$\begingroup\$ Also, here is some 3 bytes off I managed to pluck out of this trivially. \$\endgroup\$ Commented Nov 19, 2017 at 17:51
0
\$\begingroup\$

J, 20 bytes

{1,[:;[:(^&.><\)1+i.

1-indexed

Try it online!

\$\endgroup\$
0
\$\begingroup\$

APL+WIN, 23 bytes

(∊n↑¨⊂[2]n∘.*n←⍳n)[n←⎕]

Explanation:

[n←⎕] prompts for screen input and selects the nth element of the concatenated vector
see below

n←⍳n creates a vector of 1 to n

∘.* outer product with exponentiation as the operator

⊂[2] enclose each row of the resulting array as an element of a nested array

∊n↑¨ take 1 to n elements from the 1 to nth row of the matrix and concatenate into a vector
\$\endgroup\$
4
  • \$\begingroup\$ What APL dialect does this work on? \$\endgroup\$ Commented Nov 19, 2017 at 19:12
  • \$\begingroup\$ It is written in APL+WIN. I will make this clear in any future answers \$\endgroup\$
    – Graham
    Commented Nov 19, 2017 at 19:17
  • \$\begingroup\$ I took from your answer to replace my ↑,/. I didn't know of that function. Thanks \$\endgroup\$
    – H.PWiz
    Commented Nov 19, 2017 at 19:19
  • \$\begingroup\$ @EriktheOutgolfer I think this will work on any modern APL. \$\endgroup\$
    – Adám
    Commented Nov 19, 2017 at 20:17
0
\$\begingroup\$

S.I.L.O.S, 45 bytes

readIO
lblb
a+1
i-a
if i b
i+a
a^i
printInt a

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 30+1 (-p) bytes

$_-=$.++until$.>$_;$_=$.**++$_

try it online

\$\endgroup\$
0
\$\begingroup\$

Clojure 51 bytes

0-indexed, for example input 9 returns 256.0.

#(nth(for[i(range)j(range i)](Math/pow i(inc j)))%)
\$\endgroup\$
0
\$\begingroup\$

C 76, 62 bytes

y;f(n){for(y=0;n>y*++y/2;);return(int)pow(y-1,n+y*(3-y)/2-1);}

y;f(n){y=(int)(-.5+sqrt(1+8*~-n)/2)+2;n+=y*(3-y)/2-1;return(int)pow(y-1,n);}

I based it on this code

Try it on ideone

\$\endgroup\$
0
\$\begingroup\$

Pyt, 39 37 bytes

1-indexed

←000`ŕŕ⁺ĐĐř^Đ04Ș↔+⇹Ł-Đ↔3Ș0>łŕ0↔⇹+⁻⦋↔ŕ

Explanation:

Briefly, it calculates each row. If the requested number is in that row, return it; otherwise, go to the next row.

\$\endgroup\$

Your Answer

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

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