16
\$\begingroup\$

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.

asked Jul 9, 2018 at 16:07
\$\endgroup\$
13
  • 2
    \$\begingroup\$ One question I have: In your post you only talk about numbers. Do you mean positive integers exclusively? That is I feel how it was interpreted. \$\endgroup\$ Commented Jul 9, 2018 at 17:30
  • 1
    \$\begingroup\$ Is taking the input in reverse reasonable? Can the input be zero? \$\endgroup\$ Commented Jul 10, 2018 at 20:46
  • 2
    \$\begingroup\$ @NieDzejkob: 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\$ Commented Jul 11, 2018 at 12:56
  • 2
    \$\begingroup\$ Dear OP, we need more test cases. \$\endgroup\$ Commented Jul 11, 2018 at 12:58
  • 2
    \$\begingroup\$ In fact the solution for [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\$ Commented Jul 11, 2018 at 14:39

44 Answers 44

1
2
2
\$\begingroup\$

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

Try it online!

Brute-force test harness

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
answered Jul 11, 2018 at 10:18
\$\endgroup\$
2
\$\begingroup\$

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.

answered Jul 11, 2018 at 17:12
\$\endgroup\$
2
  • \$\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\$ Commented Jul 11, 2018 at 17:43
  • \$\begingroup\$ Actually n^5 === n, not n^4 === n \$\endgroup\$ Commented Jul 12, 2018 at 15:18
2
\$\begingroup\$

Pyth, 7 bytes

e.U^Zb_

Try it online!

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.

answered Jul 11, 2018 at 19:19
\$\endgroup\$
2
\$\begingroup\$

C (gcc), (削除) 76 (削除ここまで) 75 bytes

  • Saved a byte thanks to ceilingcat; golfing o<=O to O/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.

answered Jul 9, 2018 at 19:39
\$\endgroup\$
0
2
\$\begingroup\$

Factor, 37 bytes

[ reverse [ swap ^ ] 1reduce 10 mod ]

Attempt This Online!

Reverse the input, reduce by swapped exponentiation, take the last digit.

answered Aug 10, 2023 at 11:49
\$\endgroup\$
2
\$\begingroup\$

Arturo, 25 bytes

$=>[%do join.with:"^"&10]

Try it!

$=>[ ; 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
answered Aug 10, 2023 at 11:59
\$\endgroup\$
1
\$\begingroup\$

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

answered Jul 12, 2018 at 10:31
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 22 + 2 (-pl flag) = 24 bytes

One-liner:

$_=(s/ +/**/g,eval)%10

Try it online.

answered Jul 12, 2018 at 11:14
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you can remove the + and require the input string to be single-space separated. \$\endgroup\$ Commented Jul 12, 2018 at 11:30
1
\$\begingroup\$

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

Or Try it online!


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;.

answered Jul 15, 2018 at 12:39
\$\endgroup\$
1
\$\begingroup\$

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.

Try it online.

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.

Try it online.

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.

Try it online.

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.

answered Jul 23, 2018 at 14:04
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 t, 3 bytes

μʋ@

Try it online!

Explanation

μʋ@ # Implicit input
μʋ # Right-reduce the list by:
 @ # Swapped exponentiation
 # Take the last item (digit)
 # Implicit output
answered Aug 10, 2023 at 10:23
\$\endgroup\$
1
\$\begingroup\$

Uiua, 6 bytes

◿10/n⇌

Try it!

 ⇌ # reverse
 /n # reduce by power
◿10 # modulo 10
answered Sep 27, 2023 at 8:04
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 34 bytes

Prompt A
1
For(I,dim(lA),1,~1
lA(I)^Ans
End
10fPart(.1Ans

~ represents the negative sign.

answered Oct 11, 2023 at 19:20
\$\endgroup\$
-13
\$\begingroup\$

Python 3, 19 bytes

lambda N:eval(N)%10

Try it online!

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.

answered Jul 9, 2018 at 17:15
\$\endgroup\$
8
  • \$\begingroup\$ 16: print input()%10 \$\endgroup\$ Commented 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\$ Commented Jul 9, 2018 at 18:05
  • 5
    \$\begingroup\$ Very similar to this standard loophole (-1) \$\endgroup\$ Commented Jul 9, 2018 at 18:08
  • \$\begingroup\$ Related meta discussion \$\endgroup\$ Commented 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\$ Commented Sep 30, 2019 at 20:17
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.