20
\$\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\$
14
  • \$\begingroup\$ Can we accept input as a string? \$\endgroup\$
    – Shaggy
    May 9 '17 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
    May 10 '17 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
    May 10 '17 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
    May 10 '17 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
    May 10 '17 at 11:14

72 Answers 72

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\$

MATLAB/Octave, 23 bytes

@(n)(prod(n*ones(n,1)))
\$\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
    Jul 9 '17 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\$ Jul 10 '17 at 2:06
  • \$\begingroup\$ @musicman523 Yes, you are right. \$\endgroup\$
    – Steadybox
    Jul 10 '17 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\$

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\$
    – null
    Aug 12 '20 at 13:34
  • \$\begingroup\$ @HighlyRadioactive not with the updated input methods... Razetime, come to the rescue! :D \$\endgroup\$
    – Dion
    Aug 12 '20 at 17:50
  • \$\begingroup\$ @HighlyRadioactive and they get MAWP... 25 bytes. 8dion8.github.io/MAWP/… \$\endgroup\$
    – Dion
    Aug 12 '20 at 17:52
  • \$\begingroup\$ Dang! That's unfair!! @Dion \$\endgroup\$
    – null
    Aug 13 '20 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
    Aug 13 '20 at 5:10
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\$
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
    May 11 '17 at 15:19
  • \$\begingroup\$ Your noteworthy code snippet may be transformed to the next one param($a)"$a*"*$a+1|iex. \$\endgroup\$ May 11 '17 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\$

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
    May 10 '17 at 19:35
  • \$\begingroup\$ And shave three more off by using \G1 instead of your last three lines. \$\endgroup\$
    – Neil
    May 10 '17 at 19:38
  • \$\begingroup\$ @Neil Ah, \G, that's a neat trick \$\endgroup\$
    – user41805
    May 11 '17 at 9:26
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
    May 11 '17 at 12:01
  • \$\begingroup\$ @Christoph as I write the answer there was a condition that no error should be throw \$\endgroup\$ May 11 '17 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
    May 11 '17 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\$

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\$
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\$

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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy