For a given list of number \$[x_1, x_2, x_3, ..., x_n]\$ find the last digit of \$x_1 ^{x_2 ^ {x_3 ^ {\dots ^ {x_n}}}}\$ Example:
[3, 4, 2] == 1
[4, 3, 2] == 4
[4, 3, 1] == 4
[5, 3, 2] == 5
Because \3ドル ^ {(4 ^ 2)} = 3 ^ {16} = 43046721\$.
Because \4ドル ^ {(3 ^ 2)} = 4 ^ {9} = 262144\$.
Because \4ドル ^ {(3 ^ 1)} = 4 ^ {3} = 64\$.
Because \5ドル ^ {(3 ^ 2)} = 5 ^ {9} = 1953125\$.
Rules:
This is code golf, so the answer with the fewest bytes wins.
If your language has limits on integer size (ex. \2ドル^{32}-1\$) n will be small enough that the sum will fit in the integer.
Input can be any reasonable form (stdin, file, command line parameter, integer, string, etc).
Output can be any reasonable form (stdout, file, graphical user element that displays the number, etc).
Saw on code wars.
44 Answers 44
-
2\$\begingroup\$ What on earth, why does this work?! \$\endgroup\$Caleb Jay– Caleb Jay2018年07月09日 18:06:39 +00:00Commented Jul 9, 2018 at 18:06
-
\$\begingroup\$ @komali_2:
**is JavaScript's exponentiation operator. The rest is pretty straightforward. \$\endgroup\$Shaggy– Shaggy2018年07月09日 18:31:08 +00:00Commented Jul 9, 2018 at 18:31 -
3\$\begingroup\$ @komali_2
a.join`**`is equivalent toa.join(['**'])and['**']is coerced to'**'by thejoinmethod. \$\endgroup\$Arnauld– Arnauld2018年07月09日 18:59:14 +00:00Commented Jul 9, 2018 at 18:59 -
1\$\begingroup\$ I think that OP intends that the limit is on the sum of values, in which case this does not solve the problems as posed. \$\endgroup\$Neil Slater– Neil Slater2018年07月11日 09:35:27 +00:00Commented Jul 11, 2018 at 9:35
-
1\$\begingroup\$ @AJFaraday the %10 at the end. When dividing any number by 10, the remainder (the modulus) will always be the last digit, so
n % 10will return the last digit ofn\$\endgroup\$Mayube– Mayube2018年07月11日 15:54:21 +00:00Commented Jul 11, 2018 at 15:54
-
\$\begingroup\$ Not sure what people liked so much about this answer. \$\endgroup\$ngm– ngm2018年07月10日 20:32:45 +00:00Commented Jul 10, 2018 at 20:32
-
1\$\begingroup\$ I personally like to see
Reducebeing used. \$\endgroup\$JayCe– JayCe2018年07月11日 16:04:10 +00:00Commented Jul 11, 2018 at 16:04
HP 49G RPL, 36.5 bytes
Run it in APPROX mode (but enter the program in EXACT mode). Takes input on the stack with the first element deepest in the stack, as integers or reals.
WHILE DEPTH 1 - REPEAT ^ END 10 MOD
It straightforwardly exponentiates on the stack as in Sophia's solution until there is one value left, then takes it mod 10 to get the last digit.
The reason I use APPROX for computation is because 0.0^0.0 = 1 (when they are both reals), but 0^0 = ? (when they are both integers). APPROX coerces all integers to reals, so the input is fine with either. However, I use EXACT to enter the program because 10 (integer) is stored digit-by-digit, and is 6.5 bytes, but 10.0 (real) is stored as a full real number, and is 10.5 bytes. I also avoid using RPL's reduce (called STREAM) because it introduces an extra program object, which is 10 bytes of overhead. I already have one and don't want another.
Limited to the precision of an HP 49G real (12 decimal digits)
-10 bytes after empty list -> 1 requirement was removed.
-2 bytes by taking input on stack.
-
1\$\begingroup\$ Hey, could you explain how the bytecount is calculated? Just curious how that language uses nibbles. \$\endgroup\$JungHwan Min– JungHwan Min2018年07月09日 16:52:39 +00:00Commented Jul 9, 2018 at 16:52
-
1\$\begingroup\$ @JungHwanMin The HP 49G uses a 4-bit processor and BCD arithmetic, since it's a calculator. Internally most commands are transformed into 2.5 byte pointers to the routines they represent, to save space. Small numbers (0-9) are also transformed in this way. \$\endgroup\$Jason– Jason2018年07月09日 17:07:46 +00:00Commented Jul 9, 2018 at 17:07
-
1\$\begingroup\$ The Saturn processor is actually pretty fun to work with. A long time ago, I wrote this port of BurgerTime (in assembly) for the HP 48G(X). It was later ported to the 49G. Good memories! \$\endgroup\$Arnauld– Arnauld2018年07月09日 19:15:05 +00:00Commented Jul 9, 2018 at 19:15
dc, (削除) 17 (削除ここまで) 15 bytes
1[^z1<M]dsMxzA%
Takes input from the stack, outputs to the stack. Very straightforward implementation - exponentiates until only one value is left on the stack and mod for the last digit.
Thanks to brhfl for saving two bytes!
-
3\$\begingroup\$ You can golf one byte by changing
10%toA%, and one more byte by not checking stack depth twice - just put a1atop the stack before executing since n^1==n:1[^z1<M]dsMxA%\$\endgroup\$brhfl– brhfl2018年07月09日 17:45:51 +00:00Commented Jul 9, 2018 at 17:45 -
\$\begingroup\$ Good ideas! I had no idea dc would let me use
Aas a literal while set to decimal input. Thank you @brhfl! \$\endgroup\$Sophia Lechner– Sophia Lechner2018年07月09日 18:12:08 +00:00Commented Jul 9, 2018 at 18:12 -
2\$\begingroup\$ @SophiaLechner This trick works for all input bases: codegolf.stackexchange.com/a/77728/11259 \$\endgroup\$Digital Trauma– Digital Trauma2018年07月09日 20:23:35 +00:00Commented Jul 9, 2018 at 20:23
-
\$\begingroup\$ Does
10|^/not work? \$\endgroup\$cole– cole2018年07月09日 23:41:18 +00:00Commented Jul 9, 2018 at 23:41 -
\$\begingroup\$ @cole Of course it works, thanks! \$\endgroup\$Galen Ivanov– Galen Ivanov2018年07月10日 03:15:40 +00:00Commented Jul 10, 2018 at 3:15
-
1\$\begingroup\$ Finally, a challenge where J beats Jelly! \$\endgroup\$Jonah– Jonah2018年07月10日 15:11:05 +00:00Commented Jul 10, 2018 at 15:11
-
1\$\begingroup\$ As far as I am aware, stack-based languages can assume the input is present on the stack instead of STDIN or alternatives, so something like this should work for 4 bytes (alternatively, just place
Ein the header). \$\endgroup\$Mr. Xcoder– Mr. Xcoder2018年07月09日 20:26:31 +00:00Commented Jul 9, 2018 at 20:26 -
\$\begingroup\$ Relevant Meta. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2018年07月09日 20:28:19 +00:00Commented Jul 9, 2018 at 20:28
-
1\$\begingroup\$ I have fixed this issue in the latest commit for future use. \$\endgroup\$Adnan– Adnan2018年07月09日 20:44:48 +00:00Commented Jul 9, 2018 at 20:44
-
\$\begingroup\$ @Mr.Xcoder: Right! I should have remembered that. So rarely need to though with implicit input. Thanks :) \$\endgroup\$Emigna– Emigna2018年07月10日 06:04:43 +00:00Commented Jul 10, 2018 at 6:04
-
\$\begingroup\$ @Mr.Xcoder Uh, I'm not sure if that's what the meta really means. What is a "function" in 05AB1E? I think it should simply be a string, since you can assign it to a variable, and can be eval-ed with
.V..«mθlooks more like a snippet, since, by itself, you can't assign it to a variable for later reuse. Well, Adnan now fixed the issue, but eh. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年07月10日 10:32:26 +00:00Commented Jul 10, 2018 at 10:32
Pure Bash (builtins only - no external utilities), 21
echo $[${1//,/**}%10]
Input is given on the command line as a comma-separated list.
Bash integers are subject to normal signed integer limits for 64- and 32-bit versions.
-
2\$\begingroup\$
^is bitwise XOR, which is why you're getting5out of the test case instead of the correct1. You'll need to add a byte to switch to**\$\endgroup\$Sophia Lechner– Sophia Lechner2018年07月09日 21:58:36 +00:00Commented Jul 9, 2018 at 21:58 -
\$\begingroup\$ @SophiaLechner Yes - of course - good catch! Not sure how that
^crept in - I had**in previous iterations of my dev cycle. \$\endgroup\$Digital Trauma– Digital Trauma2018年07月09日 22:06:13 +00:00Commented Jul 9, 2018 at 22:06
Python 2 and Python 3, 30 bytes
lambda N:eval('**'.join(N))%10
The input N is expected to be an iterable object over string representations of number literals.
Perl 6, 14 bytes
{([**] $_)%10}
Uses the reduction meta square brackets with the operator **, modulo 10.
Julia 0.6, 30 bytes
first∘digits∘x->foldl(^,x)
This is an anon function
∘ is the composition operator.
It is multiple bytes
Ruby, (削除) 41 (削除ここまで) 47 bytes
Size increase due to handling of any 0 in the input array, which needs extra consideration. Thanks to rewritten
->a{a.reverse.inject{|t,n|n<2?n:n**(t%4+4)}%10}
This is solved as I believe the original source intended, i.e. for very large exponentiations that will not fit into language native integers - the restriction is that the array will sum into 2**32-1, not that the interim calculations are also guaranteed to fit. In fact that would seem to be the point of the challenge on Code Wars. Although Ruby's native integers can get pretty big, they cannot cope with the example below processed naively with a %10 at the end
E.g.
Input: [999999,213412499,34532597,4125159,53539,54256439,353259,4314319,5325329,1242149,142219,1243219,14149,1242149,124419,999999999]
Output:
9
-
\$\begingroup\$ Impressive. By spending a 4 more bytes, you can also cope with much higher towers: replace
n**(t%4+4)withn**((t-1)%4+1)so you getn**1instead ofn**5etc. Kudos for the observation that at any stage 4 would be a good cycle. \$\endgroup\$rewritten– rewritten2018年07月11日 17:36:52 +00:00Commented Jul 11, 2018 at 17:36 -
1\$\begingroup\$ There is an issue if the sequence has 0s \$\endgroup\$rewritten– rewritten2018年07月11日 17:48:53 +00:00Commented Jul 11, 2018 at 17:48
-
\$\begingroup\$ @rewritten: Good spot! I'll have to think about that. In theory the sequence should be forced to terminate 2 steps before the first zero. \$\endgroup\$Neil Slater– Neil Slater2018年07月11日 18:16:10 +00:00Commented Jul 11, 2018 at 18:16
-
\$\begingroup\$ Indeed, but that will require a lot more code, exactly 6 more bytes:
n<2?n:beforen**. \$\endgroup\$rewritten– rewritten2018年07月12日 08:01:41 +00:00Commented Jul 12, 2018 at 8:01
-
\$\begingroup\$ I think you mean
...gGm}10%(or something golfier). \$\endgroup\$Jonathan Allan– Jonathan Allan2018年07月09日 18:00:35 +00:00Commented Jul 9, 2018 at 18:00
C# (.NET Core), 84 bytes
a=>{int i=a.Length-1,j=a[i];for(;i-->0;)j=(int)System.Math.Pow(a[i],j);return j%10;}
- -7 bytes thanks to @raznagul
-
\$\begingroup\$ You can save some bytes by removing the brackers around
aand by combining the loop condition with the decrement (for(var i=a.Lengt-1;i-->0;)). Butusing-statement must be included in he byte count. \$\endgroup\$raznagul– raznagul2018年07月10日 11:56:24 +00:00Commented Jul 10, 2018 at 11:56 -
\$\begingroup\$ @raznagul: sorry, I'm pretty new to code-golf in C#, is it ok now ? \$\endgroup\$digEmAll– digEmAll2018年07月10日 12:06:02 +00:00Commented Jul 10, 2018 at 12:06
-
\$\begingroup\$ No problem. Yes this is fine now. \$\endgroup\$raznagul– raznagul2018年07月10日 12:08:20 +00:00Commented Jul 10, 2018 at 12:08
-
1\$\begingroup\$ You can save 3 more bytes by using a new variable to hold the result and remove most of the index access to the array: Try it online! \$\endgroup\$raznagul– raznagul2018年07月10日 12:11:02 +00:00Commented Jul 10, 2018 at 12:11
-
\$\begingroup\$ @raznagul: great ! \$\endgroup\$digEmAll– digEmAll2018年07月10日 13:53:44 +00:00Commented Jul 10, 2018 at 13:53
C (gcc), 56
- Saved 4 bytes thanks to @JonathanFrech
Recursive function r() called from macro f - normal stack limits apply.
R;r(int*n){R=pow(*n,n[1]?r(n+1):1);}
#define f(n)r(n)%10
Input given as a zero-terminated int array. This is under the assumption that none of the xn are zero.
-
2\$\begingroup\$
) r(->)r(. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年07月09日 21:49:00 +00:00Commented Jul 9, 2018 at 21:49 -
1\$\begingroup\$ Also, if you want to use UB, you could golf
r(int*n){return powtoR;r(int*n){R=pow. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年07月10日 11:30:34 +00:00Commented Jul 10, 2018 at 11:30
-
\$\begingroup\$ btw, you can use
join()to shave off 3 bytes because it's an alias ofimplode(), but I don't agree with how you're passing in input, you're just setting an array before your script is called. I think you should parse$argvor$argn. \$\endgroup\$Ethan– Ethan2018年07月10日 04:00:14 +00:00Commented Jul 10, 2018 at 4:00 -
\$\begingroup\$ You're supposed to return the last digit, not the whole thing. \$\endgroup\$Maya– Maya2018年07月11日 08:02:45 +00:00Commented Jul 11, 2018 at 8:02
-
-
Japt -h, 7 bytes
OvUqp)ì
Explanation:
OvUqp)ì
Ov ) // Japt eval:
q // Join
U // Input with
p // Power method
ì // Split into an array of numbers
-h // Return the last number
-
\$\begingroup\$ Weird, that wouldn't work for me. \$\endgroup\$Shaggy– Shaggy2018年07月10日 19:28:53 +00:00Commented Jul 10, 2018 at 19:28
-
-
-
\$\begingroup\$ Ah, for Jaysis' sake! :\ That's happening more & more often! \$\endgroup\$Shaggy– Shaggy2019年03月25日 15:40:39 +00:00Commented Mar 25, 2019 at 15:40
Japt -h, (削除) 7 (削除ここまで) 6 bytes
If input can be taken in reverse order then the first character can be removed.
Limited to 2**53-1.
Ôr!p ì
Explanation
Ô :Reverse the array
r :Reduce by
!p : Raising the current element to the power of the current total, initially the first element
ì :Split to an array of digits
:Implicitly output the last element
-
\$\begingroup\$ I got the exact same answer without the flag so this looks like the optimal way for now. \$\endgroup\$Etheryte– Etheryte2018年07月09日 18:41:27 +00:00Commented Jul 9, 2018 at 18:41
-
\$\begingroup\$ @Nit: until it's confirmed we can take input in reverse :) \$\endgroup\$Shaggy– Shaggy2018年07月09日 18:46:53 +00:00Commented Jul 9, 2018 at 18:46
-
\$\begingroup\$ @Oliver Yeah, but you're still using a flag. Personally I think the byte count without flags is the most accurate scoring result. \$\endgroup\$Etheryte– Etheryte2018年07月10日 20:35:40 +00:00Commented Jul 10, 2018 at 20:35
-
\$\begingroup\$ @Nit Shouldn't a flag add 3 bytes by meta consensus? \$\endgroup\$LegionMammal978– LegionMammal9782018年07月11日 15:51:19 +00:00Commented Jul 11, 2018 at 15:51
-
\$\begingroup\$ @LegionMammal978, not any more. \$\endgroup\$Shaggy– Shaggy2018年07月11日 16:01:08 +00:00Commented Jul 11, 2018 at 16:01
Excel VBA, 60 bytes
An anonymous VBE immediate window function that takes input from range [A1:XFD1]
s=1:For i=-[Count(1:1)]To-1:s=Cells(1,-i)^s:Next:?Right(s,1)
Python 3, 55 bytes
p=lambda l,i=-1:not l or f'{l[0]**int(p(l[1:],0))}'[i:]
Older versions
(削除)
p=lambda l,i=-1:len(l)and f'{l[0]**int(p(l[1:],0))}'[i:]or 1 (60 bytes)
(削除ここまで)
-
\$\begingroup\$ Wouldn't that have to be
p=lambda...? Python can't handle recursive anonymous lambdas, so if you need your function to be named, it needs to be part of your solution, and the naming counts towards your byte count for code-golf challenges. \$\endgroup\$mypetlion– mypetlion2018年07月09日 20:20:21 +00:00Commented Jul 9, 2018 at 20:20
Brain-Flak, 161 bytes
Includes +1 for -r
([][()]){({}[()]<({}<(({}))>[()]){({}<(({})<({}<>)({<({}[()])><>({})<>}{}<><{}>)>)>[()])}{}{}>)}{}({}((()()()()()){})(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]{})
The example [3, 4, 2] takes longer than 60 seconds, so the TIO link uses [4, 3, 2].
The -r can be removed if input can be taken in reverse order for a byte count of 160.
# Push stack size -1
([][()])
# While there are 2 numbers on the stack
{({}[()]<
# Duplicate the second number on the stack (we're multiplying this number by itself)
({}<(({}))>[()])
# For 0 .. TOS
{({}<
# Copy TOS
(({})<
# Multiple Top 2 numbers
({}<>)({<({}[()])><>({})<>}{}<><{}>)
# Paste the old TOS
>)
# End for (and clean up a little)
>[()])}{}{}
# End While (and clean up)
>)}{}
# Mod 10
({}((()()()()()){})(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]{})
numbers. Do you mean positive integers exclusively? That is I feel how it was interpreted. \$\endgroup\$2**3 % 10 == 2**7 % 10 == 2**11 % 10. Other last digits have different patterns. This knowledge is necessary to complete the puzzle and cope with arrays of numbers that would otherwise be too large to process. As far as I can tell, none of the answers here actually cope with the original problem as posed on Code Wars. \$\endgroup\$[999999,213412499,34532599,4125159,53539,54256439,353259,4314319,5325329,1242149,142219,1243219,14149,1242149,124419,999999999]is straightforward, base ends with 9, so last digit is 9 when first exponent is odd, 1 when exponent is even. I would really like a codegolf that limits inputs to the available integer range, and not the full exponential. \$\endgroup\$