# Puzzle: Last digit of a large number [algorithms]

Last digit of a large number” is a coding puzzle that people can be attempted in the following languages: haskell, javascript, python, ruby, kotlin, cpp, r, rust, and purescript.

• Difficulty: 5 kyu
• Stars: 535
• Category: algorithms
• Tags: Algorithms
• Source: codewars

# Description

Define a function that takes in two non-negative integers `\$a\$` and `\$b\$` and returns the last decimal digit of `\$a^b\$`. Note that `\$a\$` and `\$b\$` may be very large!

For example, the last decimal digit of `\$9^7\$` is `\$9\$`, since `\$9^7 = 4782969\$`. The last decimal digit of `\$({2^{200}})^{2^{300}}\$`, which has over `\$10^{92}\$` decimal digits, is `\$6\$`. Also, please take `\$0^0\$` to be `\$1\$`.

You may assume that the input will always be valid.

## Examples

``````haskell
lastDigit 4 1             `shouldBe` 4
lastDigit 4 2             `shouldBe` 6
lastDigit 9 7             `shouldBe` 9
lastDigit 10 (10^10)      `shouldBe` 0
lastDigit (2^200) (2^300) `shouldBe` 6

``````
``````javascript
lastDigit("4", "1")            // returns 4
lastDigit("4", "2")            // returns 6
lastDigit("9", "7")            // returns 9
lastDigit("10","10000000000")  // returns 0

``````
``````python
last_digit(4, 1)                # returns 4
last_digit(4, 2)                # returns 6
last_digit(9, 7)                # returns 9
last_digit(10, 10 ** 10)        # returns 0
last_digit(2 ** 200, 2 ** 300)  # returns 6

``````
``````kotlin
lastDigit(4, 1)                # returns 4
lastDigit(4, 2)                # returns 6
lastDigit(9, 7)                # returns 9
lastDigit(10, 10 ** 10)        # returns 0
lastDigit(2 ** 200, 2 ** 300)  # returns 6

``````
``````ruby
last_digit(4, 1)                # returns 4
last_digit(4, 2)                # returns 6
last_digit(9, 7)                # returns 9
last_digit(10, 10 ** 10)        # returns 0
last_digit(2 ** 200, 2 ** 300)  # returns 6

``````
``````cpp
last_digit("4", "1")            // returns 4
last_digit("4", "2")            // returns 6
last_digit("9", "7")            // returns 9
last_digit("10","10000000000")  // returns 0

``````
``````r
last_digit("4", "1")            # returns 4
last_digit("4", "2")            # returns 6
last_digit("9", "7")            # returns 9
last_digit("10","10000000000")  # returns 0

``````
``````rust
last_digit("4", "1")            // returns 4
last_digit("4", "2")            // returns 6
last_digit("9", "7")            // returns 9
last_digit("10","10000000000")  // returns 0

``````
``````purescript
lastDigit "4" "1"            -- =&gt; 4
lastDigit "4" "2"            -- =&gt; 6
lastDigit "9" "7"            -- =&gt; 9
lastDigit "10" "10000000000" -- =&gt; 0

``````

## Remarks

### JavaScript, C++, R, PureScript

Since these languages don’t have native arbitrarily large integers, your arguments are going to be strings representing non-negative integers instead.

# Solve It Here

Click the link below to solve it on Codewars:

# Notes

This puzzle was posted by a Slackbot via a slash command. If you want to help work on the app, send a message to @Josh.

If you don’t want to see the coding puzzles when you visit the forum, you can go into your settings and mute the puzzles category.

1 Like

I did this one in Python.