# The sequence of range-exponentiated integers

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!

• I think there is some error in test cases: in 1-indexed 10 should be 256; in 0-indexed 9 should be 256. – Galen Ivanov Nov 19 '17 at 19:01
• Can we have trailing spaces? – Stan Strum Nov 19 '17 at 19:09
• @StanStrum Yes. – Mr. Xcoder Nov 19 '17 at 19:10

# Python 3, 39 bytes

1-indexed

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


Try it online!

# 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


## Wolfram Language (Mathematica), 32 bytes

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


Try it online!

Ranges to the power of ranges of ranges...

• Range@Range@3 returns {{1}, {1, 2}, {1, 2, 3}}?! Damn, Mathematica's list-threading is insane. – numbermaniac Nov 21 '17 at 4:25

# 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.

# APL (Dyalog), 1513 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

• 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 – Graham Nov 19 '17 at 18:59
• @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. – Erik the Outgolfer Nov 19 '17 at 19:07
• 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. – Graham Nov 19 '17 at 19:22
• @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 – Uriel Nov 19 '17 at 19:34
• 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. – Graham Nov 19 '17 at 22:45

# Jelly, 7 bytes

*R$€Ẏ⁸ị  Try it online! -1 thanks to Mr. Xcoder. 1-indexed. • Awesome code right here. – Jonathan Allan Nov 19 '17 at 17:39 • Getting rid of fancy syntax with ", my own solution is 7 bytes: *R$€F⁸ị – Mr. Xcoder Nov 19 '17 at 17:44
• @Mr.Xcoder Thanks, although I had already removed the ", but still. That's what you get for having to study history :/ – Erik the Outgolfer Nov 19 '17 at 17:44

Saved 2 bytes thanks to xnor.

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


Try it online!

0-indexed

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

# 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


# 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.

• Very clever use of ∘ with ⍨. – Adám Nov 19 '17 at 20:44
• @Adám Thanks :-) – H.PWiz Nov 19 '17 at 20:47
• {⍵⌷∊*∘⍳⍨¨⍳⍵}⊢⌷∘∊((*∘⍳)⍨¨⍳)⊢⌷∘∊(⍳(*∘⍳)¨⍳)⊢⌷∘∊⍳*∘⍳¨⍳ – Adám Nov 19 '17 at 20:51
• I would post that... if I had any idea how that worked – H.PWiz Nov 19 '17 at 20:53
• ⊢ 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). – Adám Nov 22 '17 at 23:41

# Pyth, 8 bytes

@s^RSdSh


Try it here.

-1 thanks to Steven H..

0-indexed.

• @s^RSdSh to golf a byte off. – Steven H. Nov 19 '17 at 21:09

# 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è


# 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) }  # Ruby, 34 bytes ->n{w=0;n-=w+=1until n<0;w**=w-~n}  Try it online! # 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! • -1 byte inverting the test f=(x,n=1)=>x>n?f(x-n,n+1):n**x – Nahuel Fouilleul Nov 20 '17 at 13:27 • @NahuelFouilleul of course, thank you! – FlipTack Nov 20 '17 at 18:11 # 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. # Jelly, 15 12 bytes Probably not optimal. -3 thanks to Erik the Outgolfer's golfing. =RṁR€T€µT*FḢ  Try it online! • Wow, you must be really tired today. – Erik the Outgolfer Nov 19 '17 at 17:39 • I'm actually really ill! – Jonathan Allan Nov 19 '17 at 17:41 • Sorry! Hope you get better soon! – Erik the Outgolfer Nov 19 '17 at 17:41 • Also, here is some 3 bytes off I managed to pluck out of this trivially. – Erik the Outgolfer Nov 19 '17 at 17:51 # J, 20 bytes {1,[:;[:(^&.><\)1+i.  1-indexed Try it online! # 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  • What APL dialect does this work on? – Erik the Outgolfer Nov 19 '17 at 19:12 • It is written in APL+WIN. I will make this clear in any future answers – Graham Nov 19 '17 at 19:17 • I took ∊ from your answer to replace my ↑,/. I didn't know of that function. Thanks – H.PWiz Nov 19 '17 at 19:19 • @EriktheOutgolfer I think this will work on any modern APL. – Adám Nov 19 '17 at 20:17 # 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! # Perl 5, 30+1 (-p) bytes $_-=$.++until$.>$_;$_=$.**++$_


try it online

## Clojure 51 bytes

0-indexed, for example input 9 returns 256.0.

#(nth(for[i(range)j(range i)](Math/pow i(inc j)))%)


# 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

# 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.