0% found this document useful (0 votes)
5 views3 pages

08 04 25

The document contains multiple Python class implementations for various algorithms. These include methods for finding two numbers that sum to a target, calculating the smallest even multiple, identifying prime palindromes, counting trailing zeroes in a factorial, checking if a number is a perfect square of a prime, counting common factors, and adding the digits of a number until a single digit is obtained. Each method is defined within a class named 'Solution' and includes type annotations and return types.

Uploaded by

eswarabalaji666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

08 04 25

The document contains multiple Python class implementations for various algorithms. These include methods for finding two numbers that sum to a target, calculating the smallest even multiple, identifying prime palindromes, counting trailing zeroes in a factorial, checking if a number is a perfect square of a prime, counting common factors, and adding the digits of a number until a single digit is obtained. Each method is defined within a class named 'Solution' and includes type annotations and return types.

Uploaded by

eswarabalaji666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1)class Solution(object):

def twoSum(self, nums, target):

"""

:type nums: List[int]

:type target: int

:rtype: List[int]

"""

for i in range(len(nums)):

for j in range(i + 1, len(nums)):

if nums[i] + nums[j] == target:

return [i, j]

‘’’

2)class Solution(object):

def smallestEvenMultiple(self, n):

"""

:type n: int

:rtype: int

"""

if n % 2 == 0:

return n

else:

return n * 2

‘’’

3) class Solution(object):

def primePalindrome(self, n):

def is_prime(x):

if x < 2 or (x % 2 == 0 and x != 2): return False

for i in range(3, int(x ** 0.5) + 1, 2):

if x % i == 0: return False

return True
if 8 <= n <= 11: return 11

for length in range(1, 6):

for half in range(10**(length - 1), 10**length):

s = str(half)

pal = int(s + s[-2::-1])

if pal >= n and is_prime(pal):

return pal

‘’’

4) class Solution(object):

def trailingZeroes(self, n):

"""

:type n: int

:rtype: int

"""

count = 0

while n >= 5:

n //= 5

count += n

return count

‘’’

5) class Solution(object):

def isThree(self, n):

"""

:type n: int

:rtype: bool

"""

def is_prime(x):

if x < 2: return False

for i in range(2, int(x**0.5) + 1):

if x % i == 0:
return False

return True

root = int(n**0.5)

return root * root == n and is_prime(root)

‘’’

6) class Solution(object):

def commonFactors(self, a, b):

"""

:type a: int

:type b: int

:rtype: int

"""

count = 0

for i in range(1, min(a, b) + 1):

if a % i == 0 and b % i == 0:

count += 1

return count

‘’’

7) class Solution(object):

def addDigits(self, num):

"""

:type num: int

:rtype: int

"""

while num >= 10:

num = sum(int(d) for d in str(num))

return num

‘’’

You might also like