Basic Programming and math related problems:
Extracting digits means breaking a number like 1234 into [1, 2, 3, 4], often using string conversion or modulo/division. Counting digits involves finding how many digits are in a number using length of a string or repeated division. Reversing an integer flips its digits (e.g., 123 becomes 321), with care for signs and 32-bit limits. A palindrome number reads the same forward and backward, often checked by comparing the original and reversed number. Armstrong numbers are those equal to the sum of their digits each raised to the number of digits, like 153 = 1³ + 5³ + 3³.
To find the sum of all divisors of a number, loop through integers and check which divide the number without a remainder. Prime numbers are only divisible by 1 and themselves, and checking up to the square root is efficient. GCD, the greatest common divisor of two numbers, can be found using a simple loop or more efficiently with the Euclidean algorithm, which uses repeated modulo operations.
✅ Extract Digits from a Given Number
return [int(d) for d in str(abs(n))]
print(extract_digits(12345)) # Output: [1, 2, 3, 4, 5]
✅ Count Digits from a Given Number
✅ LeetCode Reverse Integer
def reverse(x):
✅ LeetCode Palindrome Number
def is_palindrome(x):
✅ Armstrong Number
(A number equal to the sum of its digits each raised to the power of number of digits)
✅ Sum of All the Divisors of a Given Number
✅ Prime Number Check
✅ GCD (Greatest Common Divisor: Euclidean Algorithm)
Comments
Post a Comment