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)
0
and that the expected output be specified (0
or1
or either). Finally, having to handle negative integers would be a nice addition to the challenge. \$\endgroup\$1
for0^0
. However,Foundation
+ Swift returns 0 \$\endgroup\$0
and instead specified that0<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\$