26
\$\begingroup\$

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.

\$\endgroup\$
16
  • \$\begingroup\$ Can we accept input as a string? \$\endgroup\$
    – Shaggy
    Commented May 9, 2017 at 22:32
  • \$\begingroup\$ 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 \$\endgroup\$
    – Mr. Xcoder
    Commented May 10, 2017 at 10:03
  • \$\begingroup\$ 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. \$\endgroup\$
    – Shaggy
    Commented May 10, 2017 at 10:07
  • \$\begingroup\$ @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 \$\endgroup\$
    – Mr. Xcoder
    Commented May 10, 2017 at 10:08
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Shaggy
    Commented May 10, 2017 at 11:14

98 Answers 98

1
\$\begingroup\$

Google Sheets, 24

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,
\$\endgroup\$
1
\$\begingroup\$

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!

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

Rust, 26 bytes

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

Try it online!

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

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!

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

Factor + math.unicode, 18 bytes

[ dup <array> Π ]

Try it online!

        ! 5
dup     ! 5 5
<array> ! { 5 5 5 5 5 }
Π       ! 3125
\$\endgroup\$
1
\$\begingroup\$

C++ (clang), 45 bytes

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

Try it online!

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

FunStack alpha, 14 bytes

Product Repeat

Try it at Replit!

Explanation

        Repeat  Given one argument N, create a list of N copies of N
Product         Return the product of that list
\$\endgroup\$
1
\$\begingroup\$

ReRegex, 55 bytes

#import math
(_+)p(_+)_/$1*($1p$2)/p_\b//#input
p#input

I/O as Unary _'s.

Explanation

Implements exponentiation by repeating the argument itself times. This is done by transforming, for instance ____p__ into ____*(____p_). Decrementing the right, and pulling the left out. Finally, any instance of p_ has the p removed, as x^1==x.

Try it online!

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

C (gcc) with -lgmp, 82 bytes

Uses GMP because everyone needs to be able to do 100**100.

#import<gmp.h>
f(x,a,y)int*a;{mpz_init_set_ui(a,y=x);for(;--y;)mpz_mul_ui(a,a,x);}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Of course this challenge doesn't require it, but it costs +1 byte to make \$0^0=1\$: Try it online! \$\endgroup\$
    – Deadcode
    Commented Mar 24, 2023 at 2:15
1
\$\begingroup\$

Arturo, 16 bytes

$=>[∏@.of:<=&]

Try it

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

Julia 1.0, 17 bytes

!x=prod(_->x,1:x)

Try it online!

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

Thunno 2, 3 bytes

⁻{×

Attempt This Online!

Wọp

Attempt This Online!

Explanation

⁻{×  # Implicit input
⁻    # Decrement the input
 {   # Repeat that many times:
  ×  #  Multiply TOS with input
     # Implicit output
Wọp  # Implicit input
W    # Wrap the input in a list
 ọ   # Repeat it input number of times
  p  # Product of this list
     # Implicit output
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 34 33 bytes

,$n,$p=$args,1;1..$n|%{$p*=$n};$p

Try it online!

Previous version

,$n,$ofs=$args,'*';''+@($n)*$n|iex
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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, \$\endgroup\$
    – colsw
    Commented May 11, 2017 at 15:19
  • \$\begingroup\$ Your noteworthy code snippet may be transformed to the next one param($a)"$a*"*$a+1|iex. \$\endgroup\$ Commented May 11, 2017 at 16:48
0
\$\begingroup\$

C (gcc), 34 bytes

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

Try it online!

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

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
\$\endgroup\$
0
\$\begingroup\$

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

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

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

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

Groovy, 24 bytes

{a->r=1;a.times{r*=a};r}
\$\endgroup\$
0
\$\begingroup\$

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

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

Try it online! Loops through repeated multiplication.

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

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
{}
\$\endgroup\$
0
\$\begingroup\$

PHP, 38 Bytes

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

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Drop that ,$i=0. \$\endgroup\$
    – Christoph
    Commented May 11, 2017 at 12:01
  • \$\begingroup\$ @Christoph as I write the answer there was a condition that no error should be throw \$\endgroup\$ Commented May 11, 2017 at 12:06
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Christoph
    Commented May 11, 2017 at 12:11
0
\$\begingroup\$

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.

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

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.
\$\endgroup\$
0
\$\begingroup\$

Pyth, 9 bytes

u*GH*]QQ1

Try it online!

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

Pyth, 4

This took me a while to figure out in pyth, but I finally got there:

*F*]

Try it online.

Explanation

   ]Q     # create one-item list (of implicit input)
  *  Q    # repeat (implicit input) times
*F        # reduce list over multiplication
\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 16 bytes

n->prod(x=1,n,n)

Try it online!

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

tcl, 52

proc P n {incr p;time {set p [expr $p*$n]} $n;set p}

demo

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

tcl, 50

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

demo

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

PowerShell, 36 bytes

$a="$args";$b=1;1..$a|%{$b=$b*$a};$b

Try it online!

\$\endgroup\$

Your Answer

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