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

60 Answers 60

1
2
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
| improve this answer | |
\$\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
| improve this answer | |
\$\endgroup\$
  • 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\$ – Andrei Odegov 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!

| improve this answer | |
\$\endgroup\$
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
| improve this answer | |
\$\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

| improve this answer | |
\$\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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Groovy, 24 bytes

{a->r=1;a.times{r*=a};r}
| improve this answer | |
\$\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.

| improve this answer | |
\$\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
{}
| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\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!

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Jörg Hülsermann 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.

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

Pyth, 9 bytes

u*GH*]QQ1

Try it online!

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

Pari/GP, 16 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

tcl, 52

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

demo

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

tcl, 50

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

demo

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 36 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 2.7, 37 bytes

Full Program:

s,x=1,input();exec('s*=x;')*x;print s

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 2 bytes

G*

Try it online!

Explanation

G*
G  # Do input - 1 times
 *  # Multiply with input
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Julia 0.6, 18 bytes

x->prod(x*ones(x))
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

><>, 18 + 3 (-v flag) bytes

:l(?v:
=?v*>l1
n;>

Try it online!

-v flag to put our number on the stack allows for bigger numbers with little over head.

Explination

:l(?v:

:l(     | Copies the stack top, adds the length of the stack then compares.
   ?v:  | Checks if the compare was true,
        |  if not then copy the stack top and loop, else move down.

=?v*>l1 

    >l1 | Move the direction right, add the length of the stack, add 1 to the stack.
=?v*    | Compare the length of the stack to 1,
        |  if the stack is 1 item move down, else multiply the top 2 stack items.

n;>

n;>     | Moves the directions right, then prints the top stack item as a number then ends the program.
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 34 Bytes

Anonymous VBE immediate window function that takes input from Range [A1] and outputs [A1^A1] to the VBE immediate window

n=1:For i=1To[A1]:n=n*[A1]:Next:?n

Interesting Version, 40 Bytes

Anonymous VBE immediate window function that takes input from Range [A1] and outputs [A1^A1] by method of worksheet manipulation to the VBE immediate window

[A1].Resize([A1],1)=[A1]:?[PRODUCT(A:A)]
| improve this answer | |
\$\endgroup\$
0
\$\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!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ly, 16 bytes

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

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 44 bytes

<?=array_product(array_fill(1,$argn,$argn));

not the shortest solution; but it might trigger ideas for other languages.

Run as pipe with -R or try it online.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Pushy, 5 bytes

&tCP#

Try it online!

         \ Implicit: Input on stack                       [3]
&tC      \ Push (input - 1) extra copies of input         [3, 3, 3]
   P     \ Calculate product                              [3, 3, 3, 27]
    #    \ Output result                                  PRINT 27
| improve this answer | |
\$\endgroup\$
1
2

Your Answer

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