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

2
\$\begingroup\$

Nibbles, 3 bytes (6 nibbles)

/,$*_@
/,$*_@
/           # fold from right over
 ,$         # 1..input
   *        # multiplying
     @      # result-so-far with
    _       # input

enter image description here

\$\endgroup\$
2
\$\begingroup\$

Rattle, 8 bytes

|$+[*~]~

Try it Online!

I posted the 9-byte answer and immediately found a way to improve it. This method uses $ to swap the top of the stack with the existing value in memory (0) then increments to get 1. It otherwise follows the same logic.

Rattle, 9 bytes

|s=1[*~]~

Try it Online!

Explanation

|            parse input
 s           save input to memory
  =1         set top of stack to 1
    [  ]~    loop n times where n is the value in memory
     *~      multiply the top of the stack by the value in memory
             (print implicitly)
\$\endgroup\$
8
  • \$\begingroup\$ How many more bytes would it take to work with an input of 0? \$\endgroup\$
    – Deadcode
    Commented Mar 28, 2023 at 1:17
  • 1
    \$\begingroup\$ @Deadcode depends if you think 0^0=1 \$\endgroup\$
    – Daniel H.
    Commented Mar 29, 2023 at 3:40
  • \$\begingroup\$ Rattle itself thinks 0^0=1, so that is indeed Rattle's own exponentiation operator reimplemented (for nonnegative integer input) without using it, in 13 bytes. \$\endgroup\$
    – Deadcode
    Commented Mar 29, 2023 at 3:55
  • \$\begingroup\$ @Deadcode that's right, it would be 13 bytes if it had to handle x=0 (possibly less if someone thinks of a more elegant solution). However 0<x for this question. Good observation though, I never considered the 0^0 case when implementing exponentiation in Rattle but I'm glad it ended up being 1 because that's the right answer imo \$\endgroup\$
    – Daniel H.
    Commented Mar 29, 2023 at 6:49
  • 1
    \$\begingroup\$ @Deadcode you make a very good point, it is a little annoying that it loops infinitely here. Maybe an infinite loop should only occur if the argument is explicitly 0 (e.g. ]0), and loop 0 times if implicitly 0 (e.g. ]~ where the value at the pointer is 0). Currently ]0 is equivalent to while(true) \$\endgroup\$
    – Daniel H.
    Commented Mar 29, 2023 at 7:12
2
\$\begingroup\$

vemf, 6 3 bytes

-3 bytes thanks to language creator

┴‼*

try it online (uses 3 as example)

Explanation:

┴‼*
┴   ' do this function with the same value for both arguments
 ‼  '   repeat each element of [left] the-element-at-the-corresponding-index-of-[right] times
  * ' multiply elements of resulting list
\$\endgroup\$
2
\$\begingroup\$

UPDATE 2 ::

and finally, the super overskill version that uses binary squaring algorithm, safely catches division by zero, and properly handles negative integer bases and negative integer exponents — that's quite a nifty bit of data sanity checking and edge case handling for 3 lines of code that has no hard-coded constants at all, plus avoiding use of the exponentiation operator (^ or **)

Center column is function output, and right most column is reference output.

It could even handle some extremely large magnitude negative exponents, like x^x for :

-16777213  ^  ()  -5.53904374025776e-121210664
                  -5.53904374025776e-121210664

-log(!_) exists within the function merely to return +inf

for __ in -3 -2 -1 0 '' -0 1 2 3; do 

    jot 13 -4 | 

    gawk -v PREC=2000 -Mbe '

    func ____(___,__,_){return(__+=(_="")<__?_:___)<++_\
        ?(!__?_:___?_/____(___,-__):-log(!_)):__<++_?___\
        :(__%_?___:!!_)*____(___*___,int(__/_))

    } BEGIN { CONVFMT = "%.15g"
           _ = ""
    } ($++NF = " ^ (" (___) ")")^_ + \
      ($++NF = "|" ____(__=$!_,___))^_ + \
      ($++NF = "|" __^(_<___?___:__))' ___="$__" 

done 
     1  -4  ^  (-3)  -0.015625            -0.015625
     2  -3  ^  (-3)  -0.037037037037037   -0.037037037037037
     3  -2  ^  (-3)  -0.125               -0.125
     4  -1  ^  (-3)  -1                   -1
     5  0   ^  (-3)  +inf                 +inf
     6  1   ^  (-3)  1                    1
     7  2   ^  (-3)  0.125                0.125
     8  3   ^  (-3)  0.037037037037037    0.037037037037037
     9  4   ^  (-3)  0.015625             0.015625
    10  5   ^  (-3)  0.008                0.008
    11  6   ^  (-3)  0.00462962962962963  0.00462962962962963
    12  7   ^  (-3)  0.00291545189504373  0.00291545189504373
    13  8   ^  (-3)  0.001953125          0.001953125
    14  -4  ^  (-2)  0.0625               0.0625
    15  -3  ^  (-2)  0.111111111111111    0.111111111111111
    16  -2  ^  (-2)  0.25                 0.25
    17  -1  ^  (-2)  1                    1
    18  0   ^  (-2)  +inf                 +inf
    19  1   ^  (-2)  1                    1
    20  2   ^  (-2)  0.25                 0.25
    21  3   ^  (-2)  0.111111111111111    0.111111111111111
    22  4   ^  (-2)  0.0625               0.0625
    23  5   ^  (-2)  0.04                 0.04
    24  6   ^  (-2)  0.0277777777777778   0.0277777777777778
    25  7   ^  (-2)  0.0204081632653061   0.0204081632653061
    26  8   ^  (-2)  0.015625             0.015625
    27  -4  ^  (-1)  -0.25                -0.25
    28  -3  ^  (-1)  -0.333333333333333   -0.333333333333333
    29  -2  ^  (-1)  -0.5                 -0.5
    30  -1  ^  (-1)  -1                   -1
    31  0   ^  (-1)  +inf                 +inf
    32  1   ^  (-1)  1                    1
    33  2   ^  (-1)  0.5                  0.5
    34  3   ^  (-1)  0.333333333333333    0.333333333333333
    35  4   ^  (-1)  0.25                 0.25
    36  5   ^  (-1)  0.2                  0.2
    37  6   ^  (-1)  0.166666666666667    0.166666666666667
    38  7   ^  (-1)  0.142857142857143    0.142857142857143
    39  8   ^  (-1)  0.125                0.125
    40  -4  ^  (0)   1                    1
    41  -3  ^  (0)   1                    1
    42  -2  ^  (0)   1                    1
    43  -1  ^  (0)   1                    1
    44  0   ^  (0)   1                    1
    45  1   ^  (0)   1                    1
    46  2   ^  (0)   1                    1
    47  3   ^  (0)   1                    1
    48  4   ^  (0)   1                    1
    49  5   ^  (0)   1                    1
    50  6   ^  (0)   1                    1
    51  7   ^  (0)   1                    1
    52  8   ^  (0)   1                    1
    53  -4  ^  ()    0.00390625           0.00390625
    54  -3  ^  ()    -0.037037037037037   -0.037037037037037
    55  -2  ^  ()    0.25                 0.25
    56  -1  ^  ()    -1                   -1
    57  0   ^  ()    1                    1
    58  1   ^  ()    1                    1
    59  2   ^  ()    4                    4
    60  3   ^  ()    27                   27
    61  4   ^  ()    256                  256
    62  5   ^  ()    3125                 3125
    63  6   ^  ()    46656                46656
    64  7   ^  ()    823543               823543
    65  8   ^  ()    16777216             16777216
   (trimming out the -0 exponent ones since they're same as +0)
    79  -4  ^  (1)   -4                   -4
    80  -3  ^  (1)   -3                   -3
    81  -2  ^  (1)   -2                   -2
    82  -1  ^  (1)   -1                   -1
    83  0   ^  (1)   0                    0
    84  1   ^  (1)   1                    1
    85  2   ^  (1)   2                    2
    86  3   ^  (1)   3                    3
    87  4   ^  (1)   4                    4
    88  5   ^  (1)   5                    5
    89  6   ^  (1)   6                    6
    90  7   ^  (1)   7                    7
    91  8   ^  (1)   8                    8
    92  -4  ^  (2)   16                   16
    93  -3  ^  (2)   9                    9
    94  -2  ^  (2)   4                    4
    95  -1  ^  (2)   1                    1
    96  0   ^  (2)   0                    0
    97  1   ^  (2)   1                    1
    98  2   ^  (2)   4                    4
    99  3   ^  (2)   9                    9
   100  4   ^  (2)   16                   16
   101  5   ^  (2)   25                   25
   102  6   ^  (2)   36                   36
   103  7   ^  (2)   49                   49
   104  8   ^  (2)   64                   64
   105  -4  ^  (3)   -64                  -64
   106  -3  ^  (3)   -27                  -27
   107  -2  ^  (3)   -8                   -8
   108  -1  ^  (3)   -1                   -1
   109  0   ^  (3)   0                    0
   110  1   ^  (3)   1                    1
   111  2   ^  (3)   8                    8
   112  3   ^  (3)   27                   27
   113  4   ^  (3)   64                   64
   114  5   ^  (3)   125                  125
   115  6   ^  (3)   216                  216
   116  7   ^  (3)   343                  343
   117  8   ^  (3)   512                  512

====================================

UPDATE 1 ::: The shorter version that only does x^x, w/o binary squaring

jot 15 0 | 

gawk 'func ___(__,_){$NF*=(__+=(""~__)*($++_+=!$_))>_?$_ ___(--__):_}!___()'   

1
1
4
27
256
3125
46656
823543
16777216
387420489
10000000000
285311670611
8916100448256
302875106592253
11112006825558016

====================================

awk - a LOT of bytes (123 ?, per Deadcode), but it includes the full recursive binary squaring algorithm tailored for x^x without using any alphanumerics (other than unavoidable keywords like function and return) or the power (^ | **) operator :

jot 14 | 

mawk 'function ____(__,___,_){return(___+=((_="")==___)*__)<(_+=++_)?\
           __+!+__:(___%_?__:!!_)*____(__*__,int(___/_))}$++NF=____($_)'
0 1
1 1
2 4
3 27
4 256
5 3125
6 46656
7 823543
8 16777216
9 387420489
10 10000000000
11 285311670611
12 8916100448256
13 302875106592253
14 11112006825558016

Yes this is code golfing so mine is equivalent to Tiger Woods doing 10 strokes at a par 3 in Augusta, but unlike most other entries, this one scales nicely even for extremely large inputs, AND, also being POSIX-compliant (not that it matters for golfing)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This appears to be 123 bytes: Try it online! \$\endgroup\$
    – Deadcode
    Commented Mar 24, 2023 at 2:40
2
\$\begingroup\$

PowerShell, 20 bytes

@($n)*$n-join"*"|iex

Try it online!

Explanation

@($n)*$n-join"*"|iex    # full command
@($n)                   # array consisting of single element
*$n                     # multiply the array element x times
-join"*"                # joins all elements with * separating (3*3*3)
|iex                    # executes the provided expression 
\$\endgroup\$
2
\$\begingroup\$

Go, 53 51 bytes

func(n int)int{p,i:=1,0
for;i<n;i++{p*=n}
return p}

Attempt This Online!

  • -2 by @The Thonnu: anonymous function
\$\endgroup\$
1
2
\$\begingroup\$

BQN, 4 bytes

×´⥊˜

Try it

  ⥊˜ # a list of N Ns
×´   # product
\$\endgroup\$
2
\$\begingroup\$

Rust, 29 26 bytes

Saved 3c

|x|(0..x).fold(1,|a,_|a*x)
|x|(0..x).map(|_|x).product()
\$\endgroup\$
2
\$\begingroup\$

Nekomata, 2 bytes

ř∏

Attempt This Online!

ř   Replicate
 ∏  Product
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 6 bytes

g;?j₎×

Try it online!

Explanation

          Example input: 5
g         Group: [5]
 ;?       Pair with the Input: [[5], 5]
   j₎     Juxtapose [5] 5 times: [5, 5, 5, 5, 5]
     ×    Multiply
\$\endgroup\$
1
\$\begingroup\$

CJam, 7 bytes

ri_a*:*

Try it online!

Explanation

ri       e# Read an int from input
  _      e# Duplicate it
   a*    e# Put the copy in the array and repeat it that many times
     :*  e# Take the product of the array
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 13 bytes

{[*] $_ xx$_}

$_ xx $_ evaluates to a list of $_ copies of $_ ($_ being the argument to the anonymous function), and then [*] reduces that list with multiplication.

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

CJam, 6 bytes

ri_m*,

Try it online!

ri       e# Read integer
  _      e# Duplicate
   m*    e# Cartesian power. The first argument is interpreted as a range
     ,   e# Number of elements. Implicitly display
\$\endgroup\$
1
\$\begingroup\$

Clojure, 22

#(apply *(repeat % %))

:)

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

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

dc, 24 23 26 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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Does not work for x=1. \$\endgroup\$
    – eush77
    Commented May 10, 2017 at 17:57
  • \$\begingroup\$ You can shave off two bytes by replacing lr sequences at the end with two ds at the beginning. \$\endgroup\$
    – eush77
    Commented May 10, 2017 at 18:29
  • \$\begingroup\$ 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. \$\endgroup\$
    – eush77
    Commented May 10, 2017 at 18:35
  • \$\begingroup\$ @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! \$\endgroup\$ Commented May 10, 2017 at 18:46
1
\$\begingroup\$

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.

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

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.

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

MacOS Bash, 18

jot -s* -b$1 $1|bc

Try it online.

Also runs on Linux if you install the jot utility:

sudo apt install athena-jot
\$\endgroup\$
1
\$\begingroup\$

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
>>-[-<<<<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>  - iterated addition (multiplication)
[-<<+>>]>>>                             - 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.

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

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.

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

MATLAB/Octave, 23 bytes

@(n)(prod(n*ones(n,1)))
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 2 bytes

G*

Try it online!

Explanation

G*
G  # Do input - 1 times
 *  # Multiply with input
\$\endgroup\$
1
\$\begingroup\$

Julia 0.6, 18 bytes

x->prod(x*ones(x))
\$\endgroup\$
1
\$\begingroup\$

Python, 32 bytes

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

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to PPCG! You don't need to count the f= part, so you can shorten your submission to 30 bytes. \$\endgroup\$
    – Steadybox
    Commented Jul 9, 2017 at 22:32
  • \$\begingroup\$ @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 \$\endgroup\$ Commented Jul 10, 2017 at 2:06
  • \$\begingroup\$ @musicman523 Yes, you are right. \$\endgroup\$
    – Steadybox
    Commented Jul 10, 2017 at 2:23
1
\$\begingroup\$

Common Lisp, 59 42 40 bytes

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

Try it online!

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

J, 5 bytes

*/@$~
  • $~ shapes the argument into a list with as many items as it is, eg, 3 becomes 3 3 3.
  • @ pipes the result into...
  • */ reduce by multiplication

Try it online!

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

Ly, 16 bytes

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

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

Try it online!

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

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

MAWP, 40 25 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
\$\endgroup\$
10
  • \$\begingroup\$ 1+ wins this time! \$\endgroup\$ Commented Aug 12, 2020 at 13:34
  • \$\begingroup\$ @HighlyRadioactive not with the updated input methods... Razetime, come to the rescue! :D \$\endgroup\$
    – Dion
    Commented Aug 12, 2020 at 17:50
  • \$\begingroup\$ @HighlyRadioactive and they get MAWP... 25 bytes. 8dion8.github.io/MAWP/… \$\endgroup\$
    – Dion
    Commented Aug 12, 2020 at 17:52
  • \$\begingroup\$ Dang! That's unfair!! @Dion \$\endgroup\$ Commented Aug 13, 2020 at 2:12
  • \$\begingroup\$ @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. ¯_(ツ)_/¯ \$\endgroup\$
    – Dion
    Commented Aug 13, 2020 at 5:10

Your Answer

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