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
Using Buttons in Tkinter to Navigate to Different Pages of the Application
How to Ignore Null Byte When Reading a CSV File
Importerror: No Module Named Bs4 (Beautifulsoup)
How to Correct Typeerror: Unicode-Objects Must Be Encoded Before Hashing
Convert a Python Int into a Big-Endian String of Bytes
Calculate Rgb Value for a Range of Values to Create Heat Map
Windowserror: [Error 193] %1 Is Not a Valid Win32 Application in Python
How to Check the Version of Python Modules
Issue in Using Win32Com to Access Excel File
How to Manage Division of Huge Numbers in Python
How to Check Url Change With Selenium in Python
Is There a Memory Efficient and Fast Way to Load Big Json Files
Running Two Python Scripts With Bash File
Regular Expression for Double and Integer Validation
How to Pass a .Txt File to a Function in Python
Django Viewset Has Not Attribute 'Get_Extra_Actions'
Django: Calling .Update() on a Single Model Instance Retrieved by .Get()
Typeerror: Unsupported Format String Passed to List._Format_