# Raise integer x to power x, without exponentiation built-ins

Task - The title pretty much sums it up: raise an integer x to power x, where 0<x.

Restrictions:

• Use of exponentiation, exp(), ln(), and any other powers-related language built-ins, like pow(), x^x, x**x is forbidden.
• You can assume that the given integer fits the limits of the programming language of your choice.

Test cases:

Input | Output
---------------
2     | 4
3     | 27
5     | 3125
6     | 46656
10    | 10000000000

This is , so the shortest program in bytes wins.

• Can we accept input as a string? – Shaggy May 9 '17 at 22:32
• I have made an edit to this, hoping it will be reopened. I deleted rule 3 and instead stated that it should be a full program, as the OP probably intended – Mr. Xcoder May 10 '17 at 10:03
• Much better, @Mr.Xcoder but I suggest removing (or rewording) the second restriction. Does "not a function" exclude JS from participating? I'd also suggest, for the purposes of the challenge, that we should have to handle 0 and that the expected output be specified (0 or 1 or either). Finally, having to handle negative integers would be a nice addition to the challenge. – Shaggy May 10 '17 at 10:07
• @Shaggy added js back in... calculated 0^0 on the apple calculator and it returned 1. Maybe 1 should be the chosen value, because Python also returns 1 for 0^0. However, Foundation+ Swift returns 0 – Mr. Xcoder May 10 '17 at 10:08
• @Mr.Xcoder, I've removed the "restriction" that we need not handle 0 and instead specified that 0<x in the lead-in. I also removed the restriction that code shouldn't throw errors; that should go without saying. Feel free to roll back if necessary. – Shaggy May 10 '17 at 11:14

# Python, 25 bytes

lambda n:eval('1'+'*n'*n)

Try it online!

# APL (Dyalog), 4 bytes

For xx, takes x as left argument and x as right argument.

×/⍴⍨

Try all cases online!

×/ product of

⍴⍨ arg copies arg

And here here is one that handles negative integers too:

×/|⍴|*×

Try all cases!

×/ the product of

| absolute value

repetitions of

| the absolute value

* to the power of

× the signum

The built-in Power primitive is:

x*y

## Mathematica, 16 bytes

I've got two solutions at this byte count:

1##&@@#~Table~#&

Here, #~Table~# creates a list of n copies of n. Then the List head is replaced by 1##& which multiplies all its arguments together.

Nest[n#&,1,n=#]&

This simply stores the input in n and then multiplies 1 by n, n times.

• #~Product~{#}& – alephalpha May 12 '17 at 13:14
• @alephalpha ah, good point. You can post that as a separate answer. – Martin Ender May 12 '17 at 13:31

# JavaScript (ES6), 332825 24 bytes

n=>g=(x=n)=>--x?n*g(x):n

## Try It

f=
n=>g=(x=n)=>--x?n*g(x):n
o.innerText=f(i.value=3)()
i.oninput=_=>o.innerText=f(+i.value)()
<input id=i min=1 type=number><pre id=o>

## History

### 25 bytes

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

### 28 bytes

n=>eval(1+("*"+n).repeat(n))

### 33 bytes

n=>eval(Array(n).fill(n).join*)

# Pure bash, 43

echo $[$1<2?1:$[$1<2?2:$1]#printf 1%0$1d]

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

• printf 1%0$1d outputs a 1 followed by n 0s •$[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result. Unfortunately base <2 does not work, so the extra ?: bits handle input n=1.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).

• Same problem as I had, this doesn't work for x=1. Nonetheless, very interesting approach. – Maxim Mikhaylov May 10 '17 at 18:25
• @MaxLawnboy Thanks for pointing that out - that sadly bloated my answer. Perhaps I can figure out another shorter version... – Digital Trauma May 10 '17 at 19:39
• Cool stuff. Always wished to learn bash, but always been too lazy for it =) – user69099 May 12 '17 at 23:26

# Standard ML, 42 bytes

fn x=>foldl op*1(List.tabulate(x,fn y=>x))

Try it online!

Explanation:

fn y => x                 (* An anonymous function that always returns the inputted value *)
List.tabulate(x, fn y=>x) (* Create a list of size x where each item is x *)
foldl op* 1               (* Compute the product of the entire list *)
• Welcome to PPCG! – Martin Ender May 11 '17 at 19:20
• TIO has MLton now. tio.run/nexus/… – Dennis May 13 '17 at 17:01
• Oh that's awesome! Thanks! – musicman523 May 13 '17 at 17:30

## Alice, 13 bytes

/o
\i@/.&.t&*

Try it online!

### Explanation

/o
\i@/...

This is a framework for programs that read and write decimal integers and operate entirely in Cardinal mode (so programs for most arithmetic problems).

.    Duplicate n.
&.   Make n copies of n.
t    Decrement the top copy to n-1.
&*   Multiply the top two values on the stack n-1 times, computing n^n.

# R, 22 bytes

prod(rep(x<-scan(),x))

generates a list of x copies of x, then computes the product of the elements of that list. When x=0, the rep returns numeric(0), which is a numeric vector of length 0, but the prod of that is 1, so 0^0=1 by this method, which is consistent with R's builtin exponentiation, so that's pretty neat.

Try it online!

• Dang, I find a challenge where I am positive I've come up with an excellent solution exactly like this and you've beat me. And beat me by 3 years! I've barely been on Code Golf for a year now! – Sumner18 Sep 25 at 15:19

# Jelly, 3 bytes

ẋ⁸P

Try it online!

### How?

ẋ⁸P - Main link: x             e.g. 4
⁸  - link's left argument, x       4
ẋ   - repeat left right times       [4,4,4,4]
P - product                       256
• Darn, I wanted to do this. :P – HyperNeutrino May 9 '17 at 22:49
• @Jonathan Allan is it 3 bytes, or 3 wide-characters? let us view source code hex dump, please, to make correct decision on actual code bytesize. ;-) and make the corrections – user69099 May 12 '17 at 23:20
• @xakepp35 Jelly uses a SBCS and the bytes link in the header points to it. The program with hexdump F7 88 50 works as intended. – Dennis May 12 '17 at 23:48
• @Dennis thanks for reply! i could not ever imagine such a language before =) – user69099 May 13 '17 at 0:17

# Cubix, 19 bytes

..@OI:1*s;pu!vqW|($Try it online! Step by Step Expands out onto a cube with side length 2 . . @ O I : 1 * s ; p u ! v q W | ($ .
. .
. .
• I:1 Takes the input, duplicates it and pushs 1. This sets up the stack with a counter, multiplier and result.
• *s; Multiples the TOS, swaps the result with previous and remove previous.
• pu Bring the counter item to the TOS. U-turn. This use to be a lane change, but needed to shave a byte.
• |($This was done to save a byte. When hit it skips the decrement. reflects, decrements the counter and skips the no op wrapping around the cube. • !vqW Test the counter. If truthy skip the redirect, put the counter on BOS, change lane back onto the multiplier. Otherwise redirect. • |sO@ this is the end sequence redirected to from counter test. Goes past the horizontal reflect, swaps the TOS bringing result to the TOS, ouput and halt. # 05AB1E, 3 bytes .DP .D # pop a,b push b copies of a # 05AB1E implicitly takes from input if there aren't enough values on the stack # For input 5, this gives us the array: [5,5,5,5,5] P # Take the product of that array # Implicit print • Looks like you enjoyed .D. First time I've seen it used. – Magic Octopus Urn May 11 '17 at 19:36 • ah, i dont get what is happening here.. seems to be too exotic and no explanation on how that works. =( – user69099 May 12 '17 at 23:28 • @xakepp35 Does that help? – Riley May 12 '17 at 23:32 • .D can be и for -1 (probably a builtin that wasn't available before?) – Kevin Cruijssen Sep 25 at 14:53 # x86_64 machine language for Linux, 14 11 10 bytes 0: 6a 01 pushq$0x1
2:   58                      pop    %rax
3:   89 f9                   mov    %edi,%ecx
5:   f7 ef                   imul   %edi
7:   e2 fc                   loop   5
9:   c3                      retq

To Try it online!, compile and run the following C program.

const char h[]="\x6a\1\x58\x89\xf9\xf7\xef\xe2\xfc\xc3";

int main(){
for( int i = 1; i < 4; i++ ) {
printf( "%d %d\n", i, ((int(*)())h)(i) );
}
}

# Ruby, 20 18 bytes

-2 bytes because the spec changed and I no longer need an exponent argument.

->x{eval [x]*x*?*}

Try it online!

# Stacked, 10 bytes

{!1[n*]n*}

Try it online!

Two-argument exponentiation for the same size:

{%1[x*]y*}

Both are functions. Repeats a function that multiplies 1 by n n times.

# Scala, 32 26 bytes

n=>List.fill(n)(n).product

Try it online! (Added conversion to long in the TIO so it wouldn't overflow on n=10.)

f y=product$y<$[1..y]

Try it online!

• Saved 1 byte, thanks to Laikoni
• Saved 2 bytes, thanks to nimi
• f y=foldr1(*)$y<$[1..y] is a byte shorter. – Laikoni May 11 '17 at 14:19
• product$y<$[1..y] – nimi May 16 '17 at 5:27
• Not sure how I managed to forget about product, thanks! :D – sudee May 16 '17 at 7:53

# Japt, 4 bytes

ÆUÃ×

Try it online!

## Explanation

ÆUÃ×       // implicit: U = input integer
Uo{U} r*1  // ungolfed

Uo{ }      // create array [0, U) and map each value to...
U       //   the input value
r*1  // reduce with multiplication, starting at 1
// implicit output of result

# x86 machine code (Linux), 18 bytes

31 c0 ff c0 31 db 39 df 74 07 0f af c7 ff c3 eb f5 c3

It expects a C declaration as follows extern int XpowX(int).

### Disassembled

XpowX:
# edi : input register
# ebx : counter
# eax : result register
xor  %eax, %eax    # result  = 0
inc  %eax          # result += 1
xor  %ebx, %ebx    # counter = 0
loop:
cmp  %ebx, %edi  # if (counter == input)
je   done        #   return result
imul %edi, %eax  # result  *= input
inc        %ebx  # counter += 1
jmp   loop
done:
ret

# Flurry, 16 bytes

<>{{}}<<>()>{}{}

### Run example

$./flurry -nin -c "<>{{}}<<>()>{}{}" 4 256$ ./flurry -nin -c "<>{{}}<<>()>{}{}" 5
3125

Multiply n n times to 1. The lambda expression is n (\x. \y. n (x y)) I where \y. n (x y) or n ∘ x is the product of Church numeral of n and x, and its SKIB-transformation is as follows:

n (\x. \y. n (x y)) I
S I (\n. \x. \y. n (x y)) n I
S I (\n. \x. S (K n) x) n I
S I (\n. S (K n)) n I
S I (S ∘ K) n I

So the direct translation of this expression into Flurry gives the code above.

Expanding the S in front gives n ((S ∘ K) n) I, which does not change bytes:

({})[<<>()>{}]{}

If direct exponentiation is allowed, it is 6 bytes: ({}){} (pop n, push n, pop n; evaluates to n n), because n m evaluates to m^n when n and m are Church numerals.

# CJam, 6 bytes

ri_m*,

Try it online!

_      e# Duplicate
m*    e# Cartesian power. The first argument is interpreted as a range
,   e# Number of elements. Implicitly display

## Clojure, 22

#(apply *(repeat % %))

:)

# Röda, 17 bytes

{product([_]*_1)}

Try it online!

It's an anonymous function that takes it's input from the stream.

Explanation:

{product([_]*_1)}
{               } /* An anonymous function */
[_]      /* An array containing the input value */
*_1   /* repeated times the input value */
product(      )  /* Product of all values in the array */

# dc, 242326 22 bytes

This is my first attempt writing a recursive macro in dc. I am sure it is a sub-optimal solution which can be improved a lot.

dsr1+[lrr1-d1<F*]dsFxp

Try it online!

Edit: Thanks eush77! -4 bytes.

• Does not work for x=1. – eush77 May 10 '17 at 17:57
• You can shave off two bytes by replacing lr sequences at the end with two ds at the beginning. – eush77 May 10 '17 at 18:29
• Actually, you don't need that. Just increment the top of the stack before calling for the first time. This way you will end up with x copies of x on the stack (and 1 of course), and x multiplications thereafter. So the ending can just be plain dsFxp. – eush77 May 10 '17 at 18:35
• @eush77 I was about to say that removing second lr wouldn't work here. It's my first time golfing in a stack-based language, so it feels very unusual. Thanks for your help! – Maxim Mikhaylov May 10 '17 at 18:46

## Batch, 58 bytes

@set n=1
@for /l %%i in (1,1,%1)do @set/an*=%1
@echo %n%

Only works for single-digit inputs due to 32-bit arithmetic.