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
Z80Golf, 36 bytes
00000000: cd03 80f5 30fa f1f1 57f1 280d 4f41 15af ....0...W.(.OA..
00000010: 8110 fd47 1520 f818 ef7a d60a 30fc c60a ...G. ...z..0...
00000020: cd00 8076 ...v
Takes input as raw bytes. Limited to 2**8-1.
Explanation
input:
call 8003ドル ; the input bytes
push af ; push on the stack
jr nc, input ; until EOF
pop af ; the last byte is going to be pushed twice
pop af
outer:
ld d, a ; d = exponentiation loop counter, aka the exponent
pop af ; pop the new base off the stack
jr z, output ; The flags are being pushed and popped together with the
; accumulator. Since the Z flag starts as unset and no
; instruction in the input loop modifies it, the Z flag is
; going to be unset as long as there is input, so the jump
; won't be taken. After input is depleted, a controlled stack
; underflow will occur. Since SP starts at 0, the flags
; register will be set to the $cd byte from the very beginning
; of the program. The bit corresponding to the Z flag happens
; to be set in that byte, so the main loop will stop executing
ld c, a ; C = current base
ld b, c ; B = partial product of the exponentiation loop
dec d ; if the exponent is 2, the loop should only execute once, so
; decrement it to adjust that
pow:
xor a ; the multiplication loop sets A to B*C and zeroes B in the
mul: ; process, since it's used as the loop counter
add c ; it's definitely not the fastest multiplication algorithm,
djnz mul ; but it is the smallest
ld b, a ; save the multiplication result as the partial product
dec d ; jump back to make the next iteration of either
jr nz, pow ; the exponentiation loop or the main loop, adjusting the
jr outer ; loop counter in the process
output: ; after all input is processed, we jump here. We've prepared
ld a, d ; to use the result as the next exponent, so copy it back to A
mod: ; simple modulo algorithm:
sub 10 ; subtract ten
jr nc, mod ; repeatedly until you underflow,
add 10 ; then undo the last subtraction by adding ten
call 8000ドル ; output the result
halt ; and exit
Ruby, without limitation on very high exponential towers, 142 bytes
def d(b,h,*t)
h=h%b
return h if !t[0]
l=b.times.find_index{|i|h**(i+2)%b==h} unless h*h==b
l ?l==0?h:h**d(l+1,*t)%b:[1,h,0][[t[0],2].min]
end
This code operates a reduction at each step so every single exponentiation is done on very small numbers. It is able to deal with almost arbitrarily high exponential towers without using arbitrary precision, all is done within a very small integer range, the highest integer used at any point is 9**9.
The function must be called with an initial base, for the examples it would be
d(10, 3, 4, 2)
so it is able to return the "last digit" of the result in any base.
-
\$\begingroup\$ @neilslater answer is much better, based on the observation that we don't need to compute the cycle at each stage, because once we get to the first stage of the exponential, n^4 === n (mod whatever was the previous cycle, including the initial base 10). My answer is able to start say with base 13 instead of 10, where the cycles are much weirder at the first stages. \$\endgroup\$rewritten– rewritten2018年07月11日 17:43:30 +00:00Commented Jul 11, 2018 at 17:43
-
\$\begingroup\$ Actually
n^5 === n, notn^4 === n\$\endgroup\$rewritten– rewritten2018年07月12日 15:18:09 +00:00Commented Jul 12, 2018 at 15:18
Pyth, 7 bytes
e.U^Zb_
Explanation:
e.U^Zb_ //full program
.U //reduce with the function
^Zb //lambda b,Z:Z^b
_ //on reversed (implicit) input
e //return last digit
Also, if input can be taken in reverse order, the final _ can be removed for a final score of 6 bytes.
C (gcc), (削除) 76 (削除ここまで) 75 bytes
- Saved a byte thanks to ceilingcat; golfing
o<=OtoO/o.
o(int*O){O=_(O)%10;}_(O,o,Q)int*O;{for(o=Q=1;*O>=0&&_(O+1)/Q++;o*=*O);O=o;}
Try it online! Takes input as an int pointer to a -1-terminated list of integers.
Arturo, 25 bytes
$=>[%do join.with:"^"&10]
$=>[ ; a function where input is assigned to &
join.with:"^"& ; turn [3 4 2] into "3^4^2", for example
do ; eval
%...10 ; modulo 10
] ; end fucntion
ES6, 44 bytes, solves all cases from the original problem
Thanks to @NeilSlater for the (mod 4) condition
a=>a.reduceRight((t,n)=>n<2?n:n**(t%4+4))%10
Tested passing an array of 2^20 elements each equal to 2^20
-
1\$\begingroup\$ I think you can remove the
+and require the input string to be single-space separated. \$\endgroup\$Jonathan Frech– Jonathan Frech2018年07月12日 11:30:36 +00:00Commented Jul 12, 2018 at 11:30
PHP, 54 bytes
<?eval('echo '.str_replace(',','**',$argv[1]).'%10;');
To run it:
php -n <filename> <number,number,...>
Example:
php -n last_digit.php 5,3,2
How?
Simply creates a string with format of <number>**<number>**<number>...%10 and outputs the evaluation of that string.
For input of 5,3,2, the evaluation string will be echo 5**3**2%10;.
Java 10, 77 bytes (limited to 232-1)
a->{int i=a.length-1,j=a[i];for(;i-->0;)j=(int)Math.pow(a[i],j);return j%10;}
Input is an int[], output is an int.
Java 10, 82 bytes (limited to 264-1)
a->{int i=a.length-1;var j=a[i];for(;i-->0;)j=(long)Math.pow(a[i],j);return j%10;}
Input is a long[], output is a long.
Java 10, 177 bytes (unlimited)
a->{int i=a.length-1;var j=a[i];for(;i-->0;){var r=j.ONE;for(var t=a[i];j.signum()>0;t=t.multiply(t),j=j.shiftRight(1))if(j.testBit(0))r=r.multiply(t);j=r;}return j.mod(j.TEN);}
Input is a java.math.BigInteger[], output is a java.math.BigInteger.
java.math.BigInteger has a builtin pow method, but only accepts int as parameter. This last answer uses a golfed variation of this SO answer as implementation of a pow(BigInteger, BigInteger) method.
Thunno 2 t, 3 bytes
μʋ@
Explanation
μʋ@ # Implicit input
μʋ # Right-reduce the list by:
@ # Swapped exponentiation
# Take the last item (digit)
# Implicit output
TI-Basic, 34 bytes
Prompt A
1
For(I,dim(lA),1,~1
lA(I)^Ans
End
10fPart(.1Ans
~ represents the negative sign.
Python 3, 19 bytes
lambda N:eval(N)%10
The input is given as a string where each integer is separated by the characters **. I claim that the two characters ** are equally reasonable to , at separating digit sequences.
-
\$\begingroup\$ 16:
print input()%10\$\endgroup\$wastl– wastl2018年07月09日 17:17:54 +00:00Commented Jul 9, 2018 at 17:17 -
8\$\begingroup\$ -1, because I think the input format is not reasonable. Where to stop? I can take the input as a string representation of a list and declare the list opening as
(, the list separator as**and the list closing as)%10. My answer is then a simple eval. Very funny. \$\endgroup\$nimi– nimi2018年07月09日 18:05:09 +00:00Commented Jul 9, 2018 at 18:05 -
5\$\begingroup\$ Very similar to this standard loophole (-1) \$\endgroup\$wastl– wastl2018年07月09日 18:08:41 +00:00Commented Jul 9, 2018 at 18:08
-
\$\begingroup\$ Related meta discussion \$\endgroup\$JungHwan Min– JungHwan Min2018年07月09日 19:07:02 +00:00Commented Jul 9, 2018 at 19:07
-
1\$\begingroup\$ I don't consider this to violate the loophole linked to above, but don't think this kind of answer should be permitted. \$\endgroup\$The Fifth Marshal– The Fifth Marshal2019年09月30日 20:17:07 +00:00Commented Sep 30, 2019 at 20:17
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\$