# 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

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


## 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. – Steadybox Jul 9 '17 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 – musicman523 Jul 10 '17 at 2:06
• @musicman523 Yes, you are right. – Steadybox Jul 10 '17 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 '20 at 13:34
• @HighlyRadioactive not with the updated input methods... Razetime, come to the rescue! :D – Dion Aug 12 '20 at 17:50
• @HighlyRadioactive and they get MAWP... 25 bytes. 8dion8.github.io/MAWP/… – Dion Aug 12 '20 at 17:52
• Dang! That's unfair!! @Dion – null Aug 13 '20 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 '20 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!

## 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, – colsw May 11 '17 at 15:19 • Your noteworthy code snippet may be transformed to the next one param($a)"$a*"*$a+1|iex. – Andrei Odegov May 11 '17 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
{}


# 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 '17 at 19:35
• And shave three more off by using \G1 instead of your last three lines. – Neil May 10 '17 at 19:38
• @Neil Ah, \G, that's a neat trick – user41805 May 11 '17 at 9:26

# PHP, 38 Bytes

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


Try it online!

• Drop that ,$i=0. – Christoph May 11 '17 at 12:01 • @Christoph as I write the answer there was a condition that no error should be throw – Jörg Hülsermann May 11 '17 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. – Christoph May 11 '17 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. # Pyth, 9 bytes u*GH*]QQ1  Try it online! # Pyth, 4 This took me a while to figure out in pyth, but I finally got there: *F*]  ### Explanation  ]Q # create one-item list (of implicit input) * Q # repeat (implicit input) times *F # reduce list over multiplication  # Pari/GP, 16 bytes n->prod(x=1,n,n)  Try it online! # tcl, 52 proc P n {incr p;time {set p [expr$p*$n]}$n;set p}


# tcl, 50

proc P n {time {lappend L $n}$n;expr [join \$L *]}


# TI-Basic (TI-84 Plus CE), 9 bytes

prod(seq(Ans,A,1,Ans


Input is from Ans, output is stored in Ans.

Run with 5:prgmNAME:Ans (5 or other input) for visual output.

Generates a list of Ans copies of Ans, then finds the product.