Python Century From Year
import math
def century_from_year(year: int) -> int:
return math.ceil(year / 100)
This converts a year into its century. Years 1-100 are century 1, 101-200 are century 2, and so on.
Python Check Palindrome
def check_palindrome(input_string: str) -> bool:
return input_string[::-1] == input_string
This compares the string with its reverse. If they match, it is a palindrome.
Python Chocolates By Numbers
from math import gcd
def chocolates_by_numbers(n: int, m: int) -> int:
return (n * m) // gcd(n, m) // m
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
Python Common Prime Divisors
from math import gcd
def common_prime_divisors(a: list[int], b: list[int]) -> int:
def remove_common_prime_divisors(n: int, m: int) -> int:
while n != 1:
d = gcd(n, m)
if d == 1:
break
n //= d
return n
counter = 0
for x, y in zip(a, b):
d = gcd(x, y)
x = remove_common_prime_divisors(x, d)
if x != 1:
continue
y = remove_common_prime_divisors(y, d)
if y == 1:
counter += 1
return counter
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Python Count Div
def count_div(a: int, b: int, k: int) -> int:
first_div = a if a % k == 0 else a + (k - a % k)
last_div = b - b % k
return (last_div - first_div) // k + 1
This counts how many numbers in a range are divisible by K without looping through every value.
Python Count Factors
def count_factors(n: int) -> int:
count = 0
i = 1
while i * i < n:
if n % i == 0:
count += 2
i += 1
if i * i == n:
count += 1
return count
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Python Count Non Divisible
def count_non_divisible(a: list[int]) -> list[int]:
size = len(a)
occurrences = [0] * (max(a) + 1)
for v in a:
occurrences[v] += 1
nondivisor = [0] * size
for k, v in enumerate(a):
count = 0
i = 1
while i * i <= v:
if v % i == 0:
count += occurrences[i]
if v // i != i:
count += occurrences[v // i]
i += 1
nondivisor[k] = size - count
return nondivisor
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.
Python Count Semi Primes
def count_semi_primes(n: int, p: list[int], q: list[int]) -> list[int]:
primes = [True] * (n + 1)
semi_primes = [0] * (n + 1)
i = 2
while i * i <= n:
if primes[i]:
for k in range(i * i, n + 1, i):
primes[k] = False
i += 1
k = 2
while k * k <= n:
if primes[k]:
i = 2
while i * k <= n:
if primes[i]:
semi_primes[k * i] = 1
i += 1
k += 1
for i in range(1, n + 1):
semi_primes[i] += semi_primes[i - 1]
return [semi_primes[q[idx]] - semi_primes[p[idx] - 1] for idx in range(len(p))]
This precomputes semiprimes and prefix sums so each range query becomes a quick subtraction.
Python Cyclic Rotation
from collections import deque
def cyclic_rotation(a: list[int], k: int) -> list[int]:
if not a:
return a
d = deque(a)
d.rotate(k)
return list(d)
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Python Distinct
def distinct(a: list[int]) -> int:
return len(set(a))
This counts unique values by tracking what has already been seen.