| Bytes | Lang | Time | Link |
|---|---|---|---|
| 028 | Periodically | 250506T194000Z | madeforl |
| 014 | Juby | 250506T024323Z | Jordan |
| 006 | Thumb2 assembly 12 bytes | 250506T023434Z | petroleu |
| 246 | 64bit Aarch64 assembly | 250506T022151Z | petroleu |
| 011 | TIBASIC TI83 Plus | 250424T163625Z | madeforl |
| 046 | Swift 6 | 250425T183101Z | macOSist |
| 016 | CASIO BASIC CASIO fx9750GIII | 250424T160245Z | madeforl |
| 015 | Raku Perl 6 rakudo | 250424T142620Z | xrs |
| 072 | Java | 250328T132217Z | marco |
| 044 | Tcl | 250301T194437Z | sergiol |
| 024 | AWK | 250325T204755Z | xrs |
| 054 | SAKO | 250325T191213Z | Acrimori |
| 148 | Bespoke | 250308T044807Z | Josiah W |
| 040 | Tcl | 250301T223642Z | sergiol |
| 071 | Tcl | 250301T204007Z | sergiol |
| 050 | Tcl | 170708T010223Z | sergiol |
| 052 | Tcl | 170708T004728Z | sergiol |
| 004 | Uiua | 230927T180221Z | chunes |
| 002 | Nekomata | 230705T030856Z | alephalp |
| 003 | Thunno 2 | 230607T171626Z | The Thon |
| 026 | Rust | 230606T203058Z | Starwort |
| 004 | BQN | 230606T025559Z | chunes |
| 051 | Go | 230404T140046Z | bigyihsu |
| 020 | PowerShell | 230404T123546Z | James Fl |
| nan | 230324T023633Z | RARE Kpo | |
| 003 | vemf | 230328T222724Z | mí |
| 017 | Julia 1.0 | 230329T143122Z | MarcMush |
| 008 | Rattle | 230327T223432Z | d01 |
| 005 | Nibbles | 230327T012546Z | Deadcode |
| 017 | Desmos | 230326T200514Z | Youserna |
| 013 | Regex 🐇 ECMAScript+?^*RME | 220723T235557Z | Deadcode |
| 036 | Nibbles | 230326T201544Z | Dominic |
| 004 | APL Dyalog | 170509T222756Z | Adá |
| 073 | Fortran GFortran | 230324T150826Z | roblogic |
| nan | 230324T072558Z | The Thon | |
| 016 | Arturo | 230324T023040Z | chunes |
| 082 | C gcc with lgmp | 230324T014626Z | ErikF |
| 094 | Aussie++ | 230324T010053Z | Bbrk24 |
| 055 | ReRegex | 230323T232805Z | ATaco |
| 014 | FunStack alpha | 230323T232444Z | DLosc |
| 045 | C++ clang | 220724T220005Z | c-- |
| 002 | Vyxal | 220724T005217Z | naffetS |
| 018 | Factor + math.unicode | 220724T093235Z | chunes |
| 037 | Python 2 | 220724T020007Z | Eric Xue |
| 009 | Keg | 200813T223232Z | lyxal |
| 062 | Rockstar | 200916T145059Z | Shaggy |
| 026 | Rust | 200814T173318Z | madlaina |
| 431 | Java JDK | 200813T134846Z | mindover |
| 024 | Google Sheets | 200813T134832Z | General |
| 022 | MAWP | 200812T105040Z | Razetime |
| 024 | 1+ | 200812T133321Z | Twilight |
| 017 | Perl 5 + p | 200813T115429Z | Dom Hast |
| 037 | Rust | 200813T103753Z | TehPers |
| 008 | Arn | 200812T141012Z | ZippyMag |
| 005 | Integral | 200812T140652Z | user9649 |
| 016 | Flurry | 200812T081249Z | Bubbler |
| 005 | Pushy | 171218T195603Z | FlipTack |
| 010 | x86_64 machine language for Linux | 170708T223205Z | ceilingc |
| 044 | PHP | 170725T063433Z | Titus |
| 016 | Ly | 170725T062639Z | LyricLy |
| 005 | J | 170725T061843Z | Jonah |
| 034 | Excel VBA | 170712T141807Z | Taylor R |
| 183 | ><> | 170712T133648Z | Teal pel |
| 018 | x86 machine code Linux | 170708T152207Z | ბიმო |
| 040 | Common Lisp | 170708T220225Z | ceilingc |
| 004 | Japt | 170708T225651Z | Justin M |
| 032 | Python | 170709T221330Z | userNaN |
| 018 | Julia 0.6 | 170710T015048Z | Tanj |
| 002 | 05AB1E | 170709T222106Z | Datboi |
| 037 | Python 2.7 | 170709T212754Z | Koishore |
| 023 | MATLAB/Octave | 170709T124548Z | Batman |
| 024 | JavaScript ES6 | 170509T222509Z | Shaggy |
| 036 | PowerShell | 170709T022141Z | root |
| 009 | TIBasic TI84 Plus CE | 170708T014742Z | pizzapan |
| 148 | brainf*ck | 170510T211600Z | Graviton |
| 021 | Haskell | 170511T113711Z | sudee |
| 016 | Pari/GP | 170512T131320Z | alephalp |
| 004 | Pyth | 170516T031537Z | Digital |
| 009 | Pyth | 170515T023156Z | clapp |
| 042 | Standard ML | 170511T191608Z | musicman |
| 044 | Java 9 JShell | 170513T004927Z | David Co |
| 003 | 05AB1E | 170510T131732Z | Riley |
| 026 | Scala | 170512T083827Z | musicman |
| 018 | MacOS Bash | 170510T194750Z | Digital |
| 010 | Stacked | 170511T171601Z | Conor O& |
| 033 | Aceto | 170511T142837Z | L3viatha |
| 038 | PHP | 170509T223551Z | Jör |
| 046 | Retina | 170510T161616Z | user4180 |
| 076 | BrainFlak | 170511T041557Z | DJMcMayh |
| 056 | S.I.L.O.S | 170510T212733Z | Rohan Jh |
| 022 | R | 170510T182115Z | Giuseppe |
| 013 | Alice | 170510T200055Z | Martin E |
| 018 | Ruby | 170509T230521Z | Value In |
| 058 | Batch | 170510T194230Z | Neil |
| 043 | Pure bash | 170510T165420Z | Digital |
| 019 | Cubix | 170510T190121Z | MickyT |
| 024 | Groovy | 170510T185305Z | staticme |
| 022 | dc | 170510T151306Z | Max Mikh |
| 017 | Röda | 170510T181301Z | fergusq |
| 022 | Clojure | 170510T164703Z | NikoNyrh |
| 051 | Python | 170510T164201Z | Wondercr |
| 019 | Fourier | 170510T161257Z | Beta Dec |
| 006 | CJam | 170510T155309Z | Luis Men |
| 005 | MATL | 170509T224641Z | Luis Men |
| 013 | Perl 6 | 170510T154604Z | Sean |
| 034 | C gcc | 170510T140435Z | Steadybo |
| 033 | PowerShell | 170510T133932Z | Andrei O |
| 007 | CJam | 170510T132548Z | Business |
| 016 | Mathematica | 170510T130521Z | Martin E |
| 012 | QBIC | 170510T113519Z | steenber |
| 006 | Brachylog | 170510T113324Z | Fatalize |
| 003 | Jelly | 170509T221658Z | Jonathan |
| 025 | Python | 170509T225640Z | xnor |
Periodically, 28 bytes
this lang doesn't even have an exponent function or anything related.
HHHTi(ICa)2OOBCIIF[BINO]0ICl
explanation:
HHHTi(ICa)2OOBCIIF[BINO]0ICl packed
HHH init 3 numbers in tape
Ti get input (put it to var[0])
(ICa)2 copy var[0] to var[1] and var[2]
OOB subtract 2 from arg[0] and decrement var[0]
CIIF add 2 to arg[1]
[ ]0 while var[0] != 0...
B decrement var[0]
INO multiply var[1] by var[2]
ICl print final result
J-uby, 14 bytes
~:*%-[I]|:/&:*
Explanation
~:* % -[I] | :/ & :*
~:* % -[I] | # Construct an array with a single element, n, and multiply it by n, then
:/ & :* # Reduce with multiplication
Thumb-2 assembly; 12 bytes, 6 insns
f:
subs r1, r0, #1 // 2: 0x1e41
mov r2, r0 // 2: 0x4602
loop:
muls r0, r0, r2 // 2: 0x4350
subs r1, r1, #1 // 2: 0x1e49
cbnz r1, loop // 2: 0xbb9b
bx lr // 2: 0x4770
This is a fairly straightforward translation of another answer I just posted but using the Thumb-2 32-bit instruction set. It likewise follows a normal calling convention and can be called from regular C code as if it were extern int f(int n); where it returns the exponentiated number.
This is a mixed-width instruction set, with instructions either 2 or 4 bytes wide; all instructions used here are in their 2-byte variants.
I haven't tested it on hardware, but it should work fine for n <= 9 (since log₂(10^10) > 32).
64-bit Aarch64 assembly, 24 bytes / 6 insns
f:
sub x1, x0, #1
mov x2, x0
loop:
mul x0, x0, x2
sub x1, x1, #1
cbnz x1, loop
ret
It follows a normal calling convention and can be called from regular C code as if it were extern int f(int n); where it returns the exponentiated number. No interesting tricks here to explain.
All Aarch64 instructions are 4 bytes/32 bits long, so byte count = instruction count * 4 for all instructions. I haven't tested it on hardware, but it should work fine for n <= 16 (since 16^16 is 2^64)
TI-BASIC (TI-83 Plus), 11 bytes
Input N
prod(randInt(N,N,N
doesn't actually do any randomness, it just creates a list of N with length N.
Swift 6, 46 bytes
let p={{repeatElement}()($0+0,$0).reduce(1,*)}
This is the first time I've ever actually used repeatElement(_:count:) in a Swift golf. You'd think it'd be more useful...
CASIO BASIC (CASIO fx-9750GIII), 16 bytes
?→N
Prod Seq(N,K,1,N,1
simple!
Raku (Perl 6) (rakudo), 15 bytes
{[*] $^a xx$^a}
Creates a list of $^as with $^a number of elements and then folds multiplication.
Java, 72 bytes
Java is a bit verbose for this problem, but I'll state a solution anyway.
long p(int x,int...n){int e=n.length>0?n[0]:x;return e==0?1:x*p(x,e-1);}
formatted with tests:
public class Main {
public static void main(String[] args) {
final Main main = new Main();
for (int x = 1; x <= 10; x++) {
System.out.println(x + ":\t" + main.p(x));
}
}
long p(int x, int... n) {
int e=n.length>0 ? n[0] : x;
return e==0 ? 1 : x*p(x,e-1);
}
}
Output
1: 1
2: 4
3: 27
4: 256
5: 3125
6: 46656
7: 823543
8: 16777216
9: 387420489
10: 10000000000
Or a more efficient but longer version:
long p(int x, int...n) {
int e=n.length>0 ? n[0] : x;
return e==0 ? 1 : (e&1)==0 ? p(x*x, e>>1) : x*p(x, e-1);
}
AWK, 24 bytes
{for(x=$0;++i<x;)$0*=x}1
Doesn't handle negatives, but it's around 100 bytes shorter than the previous AWK answer.
{for(x=$0 # set x to input
;++i<x;) # repeat until x
$0*=x} # default output times-equals original input
1 # always print default output
SAKO, 54 bytes
PODPROGRAM:P(N)
R=1
*)R=R×N
POWTORZ:I=1(1)N
P()=R
WROC
Full programme version, 64 bytes
CZYTAJ:N
R=1
*1)R=R×N
POWTORZ:I=1(1)N
DRUKUJ(9,0):R
STOP1
KONIEC
Bespoke, 148 bytes
POWER
I am very rich
I assumed control over all of these plebians
influence is simple,actually;anything we read(even you)we may believe
who reigns?I
Uses a CONTROL DOWHILE loop to repeatedly multiply the inputted number with 1.
Bespoke does have an exponentiation builtin (STACKTOP POW); if this were allowed, the program would be 37 bytes:
POWER
I am very powerful
who reigns?I
Tcl, 71 bytes
proc P {n m\ -1} {if $m<0 {set m $n}
expr {$m?$n*[P $n [expr $m-1]]:1}}
Had to try recursive exponentiation for this, but I failed to oulgolf myself!
Thunno 2, 3 bytes
⁻{×
Wọp
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
Rust, 29 26 bytes
Saved 3c
|x|(0..x).fold(1,|a,_|a*x)
|x|(0..x).map(|_|x).product()
Go, 53 51 bytes
func(n int)int{p,i:=1,0
for;i<n;i++{p*=n}
return p}
- -2 by @The Thonnu: anonymous function
PowerShell, 20 bytes
@($n)*$n-join"*"|iex
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
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)
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
Rattle, 8 bytes
|$+[*~]~
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[*~]~
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)
Nibbles, 3 2.5 bytes (6 5 nibbles)
`*^$_
Returns the correct answer of \$1\$ for an input of \$0\$, as it is generally accepted that \$0^0=1\$ (even though this challenge doesn't require it, and several of the other answers take advantage of this and return \$0\$, or loop infinitely, for an input of \$0\$).
The ASCII representation uses ^, but in this context it's a Replicate operator, not exponentiation.
Explanation, shown with example input of 7:
`*^$_
_ # Input coerced to a list: [7]
$ # Input: 7
^ # Replicate: [7, 7, 7, 7, 7, 7, 7]
`* # Product (returns 1 for an empty list): 823543
The builtin is just 0.5 bytes (1 nibble):
^
Desmos, 19 17 bytes
f(x)=∏_{n=1}^xx
The ^ is not used for exponentation in this case.
-2 bytes thanks to Aiden Chow.
Regex 🐇 (ECMAScript+(?^*)RME), 13 bytes
^(x(?^*x+))*$
Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method. It can yield outputs bigger than the input, and is really good at multiplying.)
This version uses molecular lookinto, a new feature in RegexMathEngine. The basic form (?^*...) matches the expression inside it against the entire input string. (With a parameter, it evaluates it against the contents of a backreference, e.g. (?^5*...) to look into \5.)
On every iteration of the outer loop, there are \$n\$ possible matching states of the inner loop, and at the end of \$n\$ iterations, the match is complete. In a complete match, each iteration can independently be at any one of \$n\$ states, so there are \$n^n\$ total possible matches.
Changing the * at the end to + would make it give a result of \$0\$ for \$n=0\$.
^ # tail = N = input number
(
x # tail -= 1
(?^* # Non-atomic lookinto - starts with tail = N
x+ # Create a choice between N possibilities
)
)*$ # Loop as many times as possible, minimum 0. This will loop N times,
# because it subtracts 1 from tail on each iteration. The minimum of
# 0 iterations gives a correct result of 1 for N==0.
The constant-power \$n^k\$ version of this can be seen in Quartic Summation, for which there is also a method that doesn't require non-atomic lookaround. It might be impossible to implement \$n^n\$ without non-atomic lookaround, due to it being impossible to create \$m\$ number of choices (with the value \$m\$ being accessed via a backreference) without the guarantee of having \$O(m)\$ avaiable space.
The constant-base \$k^n\$ version of this would be e.g. ^((.+)*)+ or ^(x(||))*$ for \$3^n\$.
Regex 🐇 (PCRE2 v10.35+), 26 bytes
^(x((?<*(?*^x+|(?2)).)))*$
This solution uses non-atomic recursion, lookahead, and lookbehind to emulate non-atomic variable-length lookbehind. The algorithm used is the same as that of the 13 byte version.
^ # tail = N = input number
(
x # tail -= 1
( # Define subroutine (?2)
(?<* # Non-atomic lookbehind
(?* # Non-atomic lookahead
^ # If we've reached tail==0, then:
x+ # create a choice between N possibilities.
|
(?2) # Otherwise, recursively call (?2).
)
. # tail += 1 (Go back 1 character at a time)
)
)
)*$ # Loop as many times as possible, minimum 0. This
# will loop N times, because it subtracts 1 from
# tail on each iteration. The minimum of 0 iterations
# gives a correct result of 1 for N==0.
The equivalent in a hypothetical engine with real (not emulated) non-atomic variable-length lookbehind would be ^(x(?<*^x+.*))*$ (16 bytes).
Regex 🐇 (PCRE2 v10.35+ without lookbehind), 59 bytes
^(?=(x*)\1(x?))(((?*x+(?!\1)()?|\2+$))x(?(?=\2\1)(?4)))*\1$
This demonstrates this to be possible without the use of lookbehind or recursion. It works by emulating operation on \$n\$ within the space of \$n/2\$, using one of those halves as a counter for the outer loop, and the other half for the inner loop.
(I wrote this version on July 29, and am finally posting it now.)
^ # Anchor to start; tail = N = input number
(?=(x*)\1(x?)) # \1 = floor(N / 2); \2 = N % 2
(
( # Define subroutine (?4):
(?*
x+(?!\1) # Add \1 to possibility count
()? # Double the possibility count of the above
|
\2+$ # If \2==1, add 1 to possibility count
)
)
x # tail -= 1
(?(?=\2\1) # If tail ≥ \2 + \1:
(?4) # Call subroutine (?4), to multiply its possibility
# count with the above
)
)* # For every possible iteration count, from 0 to the
# maximum, add the above to the possibility count
\1$ # Assert tail == \1
Regex 🐇 (ECMAScript+(?*)RME / PCRE2 v10.35+), 81 bytes
^(?=(x*)\1(x?))((?*x+(?!\1)(|)|\2+$)x((?=\2\1)(?*x+(?!\1)(|)|\2+$)|(?!\2\1)))*\1$
Try it on replit.com - RegexMathEngine
Attempt This Online! - PCRE2 v10.40+
This is a port of the 59 byte version. Basic ECMAScript lacks not only lookbehind, but subroutine calls and conditionals as well. The only feature added to basic ECMAScript is (?*...), molecular lookahead (a.k.a. non-atomic lookahead). Note that (|) must be used instead of ()? due to ECMAScript's no-empty-optional rule.
(I wrote this version on July 29, and am finally posting it now.)
^ # Anchor to start; tail = N = input number
(?=(x*)\1(x?)) # \1 = floor(N / 2); \2 = N % 2
(
(?* # Define "subroutine":
x+(?!\1) # Add \1 to possibility count
(|) # Double the possibility count of the above
|
\2+$ # If \2==1, add 1 to possibility count
)
x # tail -= 1
# Multiply the above possibility count with the following:
( # Emulated conditional, upon whether tail ≥ \2 + \1
(?=\2\1) # If yes:
# Call "subroutine" (duplicates it, since this regex flavor lacks
# subroutine calls) to multiply with the above possibility count
(?*
x+(?!\1)
(|)
|
\2+$
)
|
(?!\2\1) # If no: Use possibility multiplier of 1
)
)* # For every possible iteration count, from 0 to the
# maximum, add the above to the possibility count
\1$ # Assert tail == \1
Nibbles, 3 bytes (6 nibbles)
/,$*_@
/,$*_@
/ # fold from right over
,$ # 1..input
* # multiplying
@ # result-so-far with
_ # input
APL (Dyalog), 4 bytes
For xx, takes x as left argument and x as right argument.
×/⍴⍨
×/ product of
⍴⍨ arg copies arg
And here here is one that handles negative integers too:
×/|⍴|÷1⌊|×⊢
×/ the product of
| absolute value
⍴ repetitions of
| the absolute value
÷ divided by
1⌊ the Lesser of 1 and
| the absolute value
× times
⊢ the argument
The built-in Power primitive is:
x*y
Fortran (GFortran), 73 bytes
integer(selected_int_kind(32))::n
read*,n;print*,product([(n,j=1,n)]);end
Uses a larger integer type because the implicit integer type overflows doing 10**10. This solution can compute results up to 170141183460469231731687303715884105727.
We make an inline anonymous array of ns : [(n,j=1,n)], take the product(), and print the result.
Thunno D, \$5\log_{256}(96)\approx\$ 4.12 bytes
ZOA*P
Note: the built-in solution would have been just one character: ^ or @ (either would work).
Explanation
ZOA*P # Implicit input
ZO # Wrap the input in a list
A* # Repeat it input times
P # Take the product of this list
# Implicit output
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);}
Aussie++, 94 bytes
Not that there's an exponentiation builtin in the first place.
THE HARD YAKKA FOR f IS(x)<I RECKON a =1;I RECKON _ IS A WALKABOUT FROM[0TO x)a =a *x;BAIL a;>
Tested in commit 9522366. All whitespace included is necessary.
Just a straightforward golf of intPow from Math.aussie.
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.
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
Python 2, 37 bytes
lambda x:reduce(lambda a,b:a*b,[x]*x)
For Python 3, add
from functools import reduce
Keg, 9 bytes
¿(:;|:)∑*
I said I'd count Keg's victories over 1+ as victories for mawp, so it's 2-1 to mawp!
Rockstar, 71 62 bytes
listen to N
X's0
P's1
while N-X
let X be+1
let P be*N-0
say P
Try it here (Code will need to be pasted in)
Java (JDK), 43+1 bytes
s->{int i=0,t=s;for(;++i<s;t*=s);return t;}
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!
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,
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.
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+, 29 27 25 24 bytes
1."1##/"\*\1+\"/^"/<1+#:
This was already shockingly short, given how messy it is to create a halting loop... I did a poor job on stack manipulation, so it can probably be improved.
Special notes on 25 -> 24
Remember that you're programming in brainfuck. It's tempting to avoid confusion and tedium by thinking in terms of higher-level abstractions as much as possible, rather than thinking in brainfuck. This can buy enough control to make a working program, usually at the cost of such inefficiencies as: -excessive pointer movement, sometimes as simple as ">+<->" and sometimes less apparent; often a result of doing things in an intuitively appealing order. -- Daniel B. Cristofani
This applies to 1+ as well. I designed this program with pseudo-code, which results in two consecutive /s - cuz there are only three elements on the stack, replacing it with \ saves one byte.
Rust, 37 bytes
|x|(|mut y|{for _ in 1..x{y*=x}y})(x)
This might be able to be shortened once bindings_after_at stabilizes by using |mut y@x| instead.
Arn, 8 bytes
xõPÑ_=>$
Explained
Unpacked: *v{_}\1=>
\ Fold...
* ...with multiplication after...
v{ ...mapping with key of v...
_ ...by replacing with input
} End map
1 Literal one
=> inclusive range to...
_ ...input, implied
Integral, 5 Bytes
v↕♦▼e
Explanation
v↕ Multiply two items of the stack, to be evaluated
♦ Swap the top two items
▼ Decrement
e Evaluate ↕ that many times
Flurry, 16 bytes
<>{{}}<<>()>{}{}
Run example
$ ./flurry -nin -c "<>{{}}<<>()>{}{}" 4
256
$ ./flurry -nin -c "<>{{}}<<>()>{}{}" 5
3125
Multiply n n times to 1. The lambda expression is n (\x. \y. n (x y)) I where \y. n (x y) or n ∘ x is the product of Church numeral of n and x, and its SKIB-transformation is as follows:
n (\x. \y. n (x y)) I
S I (\n. \x. \y. n (x y)) n I
S I (\n. \x. S (K n) x) n I
S I (\n. S (K n)) n I
S I (S ∘ K) n I
So the direct translation of this expression into Flurry gives the code above.
Expanding the S in front gives n ((S ∘ K) n) I, which does not change bytes:
({})[<<>()>{}]{}
If direct exponentiation is allowed, it is 6 bytes: ({}){} (pop n, push n, pop n; evaluates to n n), because n m evaluates to m^n when n and m are Church numerals.
Pushy, 5 bytes
&tCP#
\ 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
x86_64 machine language for Linux, 14 11 10 bytes
0: 6a 01 pushq $0x1
2: 58 pop %rax
3: 89 f9 mov %edi,%ecx
5: f7 ef imul %edi
7: e2 fc loop 5
9: c3 retq
To Try it online!, compile and run the following C program.
const char h[]="\x6a\1\x58\x89\xf9\xf7\xef\xe2\xfc\xc3";
int main(){
for( int i = 1; i < 4; i++ ) {
printf( "%d %d\n", i, ((int(*)())h)(i) );
}
}
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.
J, 5 bytes
*/@$~
$~shapes the argument into a list with as many items as it is, eg,3becomes3 3 3.@pipes the result into...*/reduce by multiplication
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)]
><>, 18 + 3 (-v flag) bytes
:l(?v:
=?v*>l1
n;>
-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.
x86 machine code (Linux), 18 bytes
31 c0 ff c0 31 db 39 df 74 07 0f af c7 ff c3 eb f5 c3
It expects a C declaration as follows extern int XpowX(int).
Disassembled
XpowX:
# edi : input register
# ebx : counter
# eax : result register
xor %eax, %eax # result = 0
inc %eax # result += 1
xor %ebx, %ebx # counter = 0
loop:
cmp %ebx, %edi # if (counter == input)
je done # return result
imul %edi, %eax # result *= input
inc %ebx # counter += 1
jmp loop
done:
ret
Japt, 4 bytes
ÆUÃ×
Explanation
ÆUÃ× // implicit: U = input integer
Uo{U} r*1 // ungolfed
Uo{ } // create array [0, U) and map each value to...
U // the input value
r*1 // reduce with multiplication, starting at 1
// implicit output of result
Julia 0.6, 18 bytes
x->prod(x*ones(x))
MATLAB/Octave, 23 bytes
@(n)(prod(n*ones(n,1)))
JavaScript (ES6), 33 28 25 24 bytes
n=>g=(x=n)=>--x?n*g(x):n
Try It
f=
n=>g=(x=n)=>--x?n*g(x):n
o.innerText=f(i.value=3)()
i.oninput=_=>o.innerText=f(+i.value)()
<input id=i min=1 type=number><pre id=o>
History
25 bytes
f=(n,x=n)=>--x?n*f(n,x):n
28 bytes
n=>eval(1+("*"+n).repeat(n))
33 bytes
n=>eval(Array(n).fill(n).join`*`)
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.
brainf*ck, 148 bytes
,[->+>+<<]>>[-<<+>>]++++++++[<------<------>>-]<[->>+>>+<<<<]>>[-<<+>>]>>-[-<<<<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>[-<<+>>]>>>]<<<++++++++[-<<++++++>>]<<.
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.
Haskell, 24 23 21 bytes
f y=product$y<$[1..y]
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
Standard ML, 42 bytes
fn x=>foldl op*1(List.tabulate(x,fn y=>x))
Explanation:
fn y => x (* An anonymous function that always returns the inputted value *)
List.tabulate(x, fn y=>x) (* Create a list of size x where each item is x *)
foldl op* 1 (* Compute the product of the entire list *)
Java 9 JShell, 44 bytes
x->IntStream.range(0,x).reduce(1,(a,b)->a*x)
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.
05AB1E, 3 bytes
.DP
Try it online! or Try all examples
.D # pop a,b push b copies of a
# 05AB1E implicitly takes from input if there aren't enough values on the stack
# For input 5, this gives us the array: [5,5,5,5,5]
P # Take the product of that array
# Implicit print
Scala, 32 26 bytes
n=>List.fill(n)(n).product
Try it online! (Added conversion to long in the TIO so it wouldn't overflow on n=10.)
MacOS Bash, 18
jot -s* -b$1 $1|bc
Also runs on Linux if you install the jot utility:
sudo apt install athena-jot
Stacked, 10 bytes
{!1[n*]n*}
Two-argument exponentiation for the same size:
{%1[x*]y*}
Both are functions. Repeats a function that multiplies 1 by n n times.
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.
Retina, 49 46 bytes
Golfed 3 bytes thanks to @Neil
.+
1;$&$*1;$&$*1
{`1(?=1*;(1+);.)
$1
}`1$
\G1
Supports positive integers just as stated in the challenge.
Brain-Flak, 76 bytes
(({})){({}<(({}))>[()])}{}{{}({}<>)({<({}[()])><>({})<>}{}<><{}>)([][()])}{}
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
{}
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.
R, 22 bytes
reads x from stdin.
prod(rep(x<-scan(),x))
generates a list of x copies of x, then computes the product of the elements of that list. When x=0, the rep returns numeric(0), which is a numeric vector of length 0, but the prod of that is 1, so 0^0=1 by this method, which is consistent with R's builtin exponentiation, so that's pretty neat.
Alice, 13 bytes
/o
\i@/.&.t&*
Explanation
/o
\i@/...
This is a framework for programs that read and write decimal integers and operate entirely in Cardinal mode (so programs for most arithmetic problems).
. Duplicate n.
&. Make n copies of n.
t Decrement the top copy to n-1.
&* Multiply the top two values on the stack n-1 times, computing n^n.
Ruby, 20 18 bytes
-2 bytes because the spec changed and I no longer need an exponent argument.
->x{eval [x]*x*?*}
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.
Pure bash, 43
echo $[$1<2?1:$[$1<2?2:$1]#`printf 1%0$1d`]
Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.
printf 1%0$1doutputs a1followed by n0s$[b#a]is an arithmetic expansion to treataas a basebnumber, which gives the required result. Unfortunately base <2 does not work, so the extra?:bits handle input n=1.
Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).
Cubix, 19 bytes
..@OI:1*s;pu!vqW|($
Expands out onto a cube with side length 2
. .
@ O
I : 1 * s ; p u
! v q W | ( $ .
. .
. .
I:1Takes the input, duplicates it and pushs 1. This sets up the stack with a counter, multiplier and result.*s;Multiples the TOS, swaps the result with previous and remove previous.puBring the counter item to the TOS. U-turn. This use to be a lane change, but needed to shave a byte.|($This was done to save a byte. When hit it skips the decrement. reflects, decrements the counter and skips the no op wrapping around the cube.!vqWTest the counter. If truthy skip the redirect, put the counter on BOS, change lane back onto the multiplier. Otherwise redirect.|sO@this is the end sequence redirected to from counter test. Goes past the horizontal reflect, swaps the TOS bringing result to the TOS, ouput and halt.
Groovy, 24 bytes
{a->r=1;a.times{r*=a};r}
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
Edit: Thanks eush77! -4 bytes.
Röda, 17 bytes
{product([_]*_1)}
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 */
Clojure, 22
#(apply *(repeat % %))
:)
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
Fourier, 19 bytes
1~YI~X(Y*X~Yi^~i)Yo
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
CJam, 6 bytes
ri_m*,
ri e# Read integer
_ e# Duplicate
m* e# Cartesian power. The first argument is interpreted as a range
, e# Number of elements. Implicitly display
MATL, 5 bytes
lyX"p
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
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.
PowerShell, 34 33 bytes
,$n,$p=$args,1;1..$n|%{$p*=$n};$p
Previous version
,$n,$ofs=$args,'*';''+@($n)*$n|iex
CJam, 7 bytes
ri_a*:*
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
Mathematica, 16 bytes
I've got two solutions at this byte count:
1##&@@#~Table~#&
Here, #~Table~# creates a list of n copies of n. Then the List head is replaced by 1##& which multiplies all its arguments together.
Nest[n#&,1,n=#]&
This simply stores the input in n and then multiplies 1 by n, n times.
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
Brachylog, 6 bytes
g;?j₎×
Explanation
Example input: 5
g Group: [5]
;? Pair with the Input: [[5], 5]
j₎ Juxtapose [5] 5 times: [5, 5, 5, 5, 5]
× Multiply
Jelly, 3 bytes
ẋ⁸P
How?
ẋ⁸P - Main link: x e.g. 4
⁸ - link's left argument, x 4
ẋ - repeat left right times [4,4,4,4]
P - product 256
