Skip to content

Commit d2e7501

Browse files
authored
Create Unit Digit of a raised to power b.py
One need not to calculate entire a^b for finding last Digit.
1 parent 85fd9cc commit d2e7501

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def last_digit(a, b):
2+
if b==0: #This Code assumes that 0^0 is 1
3+
return 1
4+
elif a%10 in [0,5,6,1]:
5+
return a%10
6+
elif b%4==0:
7+
return ((a%10)**4)%10
8+
else:
9+
return ((a%10)**(b%4))%10
10+
11+
#Courtesy to https://brilliant.org/wiki/finding-the-last-digit-of-a-power/

0 commit comments

Comments
 (0)