# 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? May 9, 2017 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 May 10, 2017 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. May 10, 2017 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 May 10, 2017 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. May 10, 2017 at 11:14

# 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. May 10, 2017 at 17:57
• You can shave off two bytes by replacing lr sequences at the end with two ds at the beginning. May 10, 2017 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. May 10, 2017 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! May 10, 2017 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.

# Retina, 49 46 bytes

Golfed 3 bytes thanks to @Neil

.+
1;$&$*1;$&$*1
{1(?=1*;(1+);.)
$1 }1$

\G1


Try it online!

Supports positive integers just as stated in the challenge.

• I think you can save a byte by using .+ 1;$&$*1;$&$* instead of your first four lines.
– Neil
May 10, 2017 at 19:35
• And shave three more off by using \G1 instead of your last three lines.
– Neil
May 10, 2017 at 19:38
• @Neil Ah, \G, that's a neat trick May 11, 2017 at 9:26

# brainf*ck, 148 bytes

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


Try it online!

No built-ins ;)

How it works

,                                       - get ascii input
[->+>+<<]                               - duplicate input
>>[-<<+>>]                              - shift inputs left to start
++++++++[<------<------>>-]             - convert ascii into input numbers
<[->>+>>+<<<<]                          - get loop intervals (same as input #)
>>[-<<+>>]                              - shift input back again
[-<<+>>]>>>                             - Shift output back into input
]<<<++++++++[-<<++++++>>]<<.            - convert final output to ascii


In a nutshell, this works by multiplying x (the input) by itself x times. (a.k.a. iterating iterated addition). The net result is x^x.

I/O

The program takes a single ASCII input, and processes it as it's ASCII index minus 48. The minus 48 is to normalize inputs of actual numbers (4 becomes 52 -> 52-48 -> 4). To input a number higher than 9, use the next corrosponging ASCII character (: -> 58-48 -> 10). The program ouputs in a similar fashion.

Test I/O

INPUT > PROCESSED INPUT >> OUTPUT > TRANSLATED OUTPUT
1 > 1 >> 1 > 1
2 > 2 >> 4 > 4
3 > 3 >> K > 27


Since there are no printable ASCII characters after an input of 3, it can only print numbers in theory. Though, you can check all inputs do in fact work on visualizers such as this.

## MATLAB/Octave, 23 bytes

@(n)(prod(n*ones(n,1)))


# 05AB1E, 2 bytes

G*


Try it online!

### Explanation

G*
G  # Do input - 1 times
*  # Multiply with input


## Python, 32 bytes

f=lambda g,z=1:z>g or g*f(g,z+1)


Try it online!

• Welcome to PPCG! You don't need to count the f= part, so you can shorten your submission to 30 bytes. Jul 9, 2017 at 22:32
• @Steadybox The f= part does need to be counted, because it's recursive, so it relies upon the function being named f in order to work properly Jul 10, 2017 at 2:06
• @musicman523 Yes, you are right. Jul 10, 2017 at 2:23

# Common Lisp, 59 42 40 bytes

(lambda(x)(apply'*(fill(make-list x)x)))


Try it online!

# Ly, 16 bytes

ns<l1-[>l*<1-]>u


Being forced to use multiple stacks costs so many bytes...

Try it online!

# Integral, 5 Bytes

v↕♦▼e


Try it!

## Explanation

v↕    Multiply two items of the stack, to be evaluated
♦   Swap the top two items
▼  Decrement
e Evaluate ↕ that many times


# MAWP, 4025 22 bytes

%@![~!~1A]%1A[1A~W~]%:


-15 from Dion's improved input methods. -3 after modifying first loop to happen x+1 times.

Try it!

### Explanation

%@                    Gets multi digit input
![~!~1A]%             x gets cloned x+1 times in the stack
1A[1A~W~]%:           multiplies top 2 numbers x-1 times

• 1+ wins this time!
– null
Aug 12, 2020 at 13:34
• @HighlyRadioactive not with the updated input methods... Razetime, come to the rescue! :D
– Dion
Aug 12, 2020 at 17:50
• @HighlyRadioactive and they get MAWP... 25 bytes. 8dion8.github.io/MAWP/…
– Dion
Aug 12, 2020 at 17:52
• Dang! That's unfair!! @Dion
– null
Aug 13, 2020 at 2:12
• @HighlyRadioactive Well, a) I made the changes without seeing this first, and b) Multi-digit input was extremely annoying, so this was something that had to be done. ¯_(ツ)_/¯
– Dion
Aug 13, 2020 at 5:10

Input A1.

=DECIMAL(1&REPT(0,A1),A1


Is base conversion ok? If not, here's one at 26 (but it's really boring):

=PRODUCT(SEQUENCE(A1,1,A1,


# Java (JDK), 43+1 bytes

s->{int i=0,t=s;for(;++i<s;t*=s);return t;}


Try it online!

Overflows for 10, so all ints need to be changed to long a the cost of +1. This has been done for the TIO link.

I didn't think I could beat the IntStream though!

# Rust, 26 bytes

|x|(0..x).fold(1,|a,_|a*x)


Try it online!

# Keg, 9 bytes

¿(:;|:)∑*


Try it online!

I said I'd count Keg's victories over 1+ as victories for mawp, so it's 2-1 to mawp!

• No way! (15chrs)
– null
Aug 14, 2020 at 1:30
• stop 2-1ing. (15)
– null
Aug 14, 2020 at 1:45
• Who's post is this? Huh, looks like it's mine. Leave it alone Aug 14, 2020 at 1:46
• @HighlyRadioactive I flagged you for vandalizing a single post 10 times in a row. Stop it, or your account will get suspended. Aug 14, 2020 at 2:01

# C++ (clang), 45 bytes

int e(int x,int i=0){return++i<x?x*e(x,i):x;}


Try it online!

## QBIC, 12 bytes

:[a|q=q*a}?q


Explanation:

:   Get the integer
[a|     Start a FOR loop, for x iterations
q=q*a   Multiply q (starts out as 1) by our value, then store that in q
}       End loop
?q      Print q


## PowerShell, 34 33 bytes

,$n,$p=$args,1;1..$n|%{$p*=$n};$p  Previous version ,$n,$ofs=$args,'*';''+@($n)*$n|iex

• Using param($a)'1'+"*$a"*$a|iex works too, more in line with the other String->eval answers, nice to see other PowerShell golfers around, May 11, 2017 at 15:19 • Your noteworthy code snippet may be transformed to the next one param($a)"$a*"*$a+1|iex. May 11, 2017 at 16:48

# C (gcc), 34 bytes

i,k;f(x){for(i=k=x;--i;)x*=k;i=x;}


Try it online!

# MATL, 5 bytes

lyX"p


Try it online!

### Explanation

Example values with input 4.

l    % Push 1
% STACK: 1
y    % Implicit input. Duplicate from below
% STACK: 4, 1, 4
X"   % Replicate along two dimensions. Gives a row vector
% STACK: [4, 4, 4, 4]
p    % Product of array. Implicitly display.
% STACK: 256


# Fourier, 19 bytes

1~YI~X(Y*X~Yi^~i)Yo


Try it on FourIDE!

Multiplies Y by the input X number of times and then outputs Y.

FYI, the standard way to do this would be

I~XPXo


Where P is the power function

# Python, 51 bytes

def f(x,i=0):i=i or x;return x*f(x,i-1)if i>1else x


A little bit more verbose than @xnor's clever way of eval, but uses recursion

# Groovy, 24 bytes

{a->r=1;a.times{r*=a};r}


# S.I.L.O.S, 56 bytes

readIO
x=i
a=i
b=1
lbla
a-1
b*x
if a a
printInt b


Try it online! Loops through repeated multiplication.

# Brain-Flak, 76 bytes

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


Try it online!

This is an extremely inefficient algorithm, so don't try inputs larger than 7.

Explanation:

#Duplicate the input
(({}))

#Make *n* copies of the input
{({}<(({}))>[()])}{}

#While the stack has more than one element on it,
{{}

#Multiply the top two elements
({}<>)({<({}[()])><>({})<>}{}<><{}>)

#Push stack-height minus one to keep the loop running
([][()])

#Endwhile
}

#Pop a zero off the main stack, implicitly display the number left on the stack
{}


# PHP, 38 Bytes

for($p=1;$i++<$argn;)$p*=$argn;echo$p;


Try it online!

• Drop that ,$i=0. May 11, 2017 at 12:01 • @Christoph as I write the answer there was a condition that no error should be throw May 11, 2017 at 12:06 • There's no version of PHP that doesn't ignore E_NOTICE by default. Only with E_NOTICE it is thrown ;) And you really should track your score using <s></s> like everyone else. May 11, 2017 at 12:11 # Aceto, 33 bytes We have quite a bit of wasted space, so I'm sure we could get rid of a few more bytes. =| 0*v ds< MDL &_d id!L rDM@xp  The bottom half (except for the xp) reads the input, and puts it that many times on the stack, as an integer, i.e. '3' -> [3, 3, 3]. I really need a builtin for that.. The top half calculates the product by swapping the top two elements, checking if the top element is zero, otherwise multiplying the top two elements, and so on. xp prints. # MacOS Bash, 18 jot -s* -b$1 \$1|bc


Also runs on Linux if you install the jot utility:

sudo apt install athena-jot


# Java 9 JShell, 44 bytes

x->IntStream.range(0,x).reduce(1,(a,b)->a*x)


Try it online!

Notes:

• The imports are not included in the score since they are not required with the default JShell configuration (there are a number of packages imported by the default JShell startup script).
• Since TIO doesn't have JShell, a Java (OpenJDK 9) program is provided that demonstrates the function, but has been changed to use longs so that 10 to the tenth doesn't overflow, at the expense of one extra byte.