How to Find the Unit Digits of a Specific Number

How can we find the unit digits of a specific number?

It looks like the problem description is not complete. The algorithm could be:

  • shorten the number by leaving out the units
  • subtract the units once again from the number
  • multiply the number by -1

The end result will be the number modulo 11, although it could be negative.

num = int(input('Enter a number:'))
print("Given number: ", num, " Trying to find num % 11 =", num % 11)
while abs(num) >= 11:
units = num % 10
num //= 10 # integer division
num -= units
num = -num
print("Modified number:", num)
if num < 0:
num += 11
print("Result:", num)

A simplified algorithm, without the pesky num = -num that just checks for divisibility by 11:

num = int(input('Enter a number:'))
print("Given number: ", num, " Trying to find num % 11 == 0 :", num % 11 == 0)
given_number = num
while num >= 100:
units = num % 10 # find the last digit
num //= 10 # integer division (i.e. remove the last digit)
num -= units # subtract the just found last digi
print("Modified number:", num)

if num in [0, 11, 22, 33, 44, 55, 66, 77, 88, 99]:
print(given_number, "is divisible by 11")
else:
print(given_number, "is NOT divisible by 11")

get an integer -unit digit in a simple way

here is how to split the number into parts

int unitDigit = a % 10; //is 3
int tens= (a - unitDigit)/10; //is 53-3=50 /10 =5

To find unit digit at a particular index of fibonacci series in O(1) time. (fibonacci series may be <=10^18)

Fibonacci series has a cycle of 60 for its unit digit (without getting deep into map you can see that after 60 you get 1 and 1 again, so the sum would be 2 and so on).
Therefore, you can prepare a list of these Fibonacci unit digits:

fib_digit = [1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1, 0]

and return fib_digit[N % 60] in O(1).



Related Topics



Leave a reply



Submit