Check Prime NumberProblem StatementCheck if a given number is prime (only divisible by 1 and itself).Solutiondef is_prime(n): if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True
Generate First N PrimesProblem StatementGenerate a list containing the first n prime numbers.Solutiondef generate_n_primes(n): primes = [] num = 2 while len(primes) < n: is_p = True for i in range(2, int(num**0.5) + 1): if num % i == 0: is_p = False break if is_p: primes.append(num) num += 1 return primes
Find FactorialProblem StatementCalculate the factorial of a non-negative integer n (n!).Solutiondef factorial(n): res = 1 for i in range(1, n + 1): res *= i return res
Check Armstrong NumberProblem StatementCheck if a number is an Armstrong number (sum of its digits each raised to the power of the number of digits equals the number itself).Solutiondef is_armstrong(n): s_num = str(n) k = len(s_num) res = sum(int(digit)**k for digit in s_num) return res == n
Reverse a NumberProblem StatementReverse the digits of an integer.Solutiondef reverse_number(n): rev = 0 while n > 0: rev = rev * 10 + n % 10 n //= 10 return rev
Check Palindrome NumberProblem StatementDetermine if an integer is a palindrome (reads the same forward and backward).Solutiondef is_palindrome_num(n): return str(n) == str(n)[::-1]
Count DigitsProblem StatementCount the total number of digits in an integer.Solutiondef count_digits(n): return len(str(abs(n)))
Sum of DigitsProblem StatementCalculate the sum of the digits of a number.Solutiondef sum_digits(n): s = 0 for d in str(abs(n)): s += int(d) return s
Find GCDProblem StatementFind the Greatest Common Divisor (GCD) of two numbers.Solutiondef find_gcd(a, b): while b: a, b = b, a % b return a
Find LCMProblem StatementCalculate the Least Common Multiple (LCM) of two numbers.Solutiondef find_lcm(a, b): def gcd(x, y): while y: x, y = y, x % y return x return abs(a * b) // gcd(a, b) if a and b else 0
Fibonacci SeriesProblem StatementGenerate a list of the first n Fibonacci numbers.Solutiondef fibonacci(n): if n <= 0: return [] if n == 1: return [0] fib = [0, 1] while len(fib) < n: fib.append(fib[-1] + fib[-2]) return fib
Check Perfect NumberProblem StatementDetermine if a number is a perfect number (sum of its proper divisors equals the number itself).Solutiondef is_perfect(n): if n <= 1: return False s = 0 for i in range(1, n // 2 + 1): if n % i == 0: s += i return s == n
Power of NumberProblem StatementCalculate x raised to the power of y (x^y).Solutiondef power(x, y): return x ** y
Count Trailing ZerosProblem StatementCount the number of trailing zeros in n! (factorial).Solutiondef trailing_zeros(n): count = 0 while n >= 5: n //= 5 count += n return count
Check Leap YearProblem StatementDetermine if a year is a leap year.Solutiondef is_leap_year(year): return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
Convert Decimal to BinaryProblem StatementConvert a decimal number to its binary representation (string).Solutiondef dec_to_bin(n): return bin(n)[2:] if n >= 0 else "-" + bin(abs(n))[2:]
Convert Binary to DecimalProblem StatementConvert a binary string to its decimal integer equivalent.Solutiondef bin_to_dec(b_str): return int(b_str, 2)
Swap Two NumbersProblem StatementSwap the values of two variables.Solutiondef swap(a, b): return b, a
Find Largest of ThreeProblem StatementReturn the maximum of three numbers.Solutiondef find_max_of_three(a, b, c): return max(a, b, c)
Check Even or OddProblem StatementDetermine if a number is even or odd.Solutiondef even_or_odd(n): return "Even" if n % 2 == 0 else "Odd"
Print Multiplication TableProblem StatementReturn the first 10 multiples of a number as a list.Solutiondef multi_table(n): return [n * i for i in range(1, 11)]
Find Square RootProblem StatementCalculate the square root of a number (without built-in math.sqrt for practice).Solutiondef square_root(n): return n ** 0.5
Check Power of TwoProblem StatementDetermine if an integer is a power of two.Solutiondef is_power_of_two(n): return n > 0 and (n & (n - 1)) == 0
Count Set BitsProblem StatementCount the number of 1s in the binary representation of an integer (Hamming weight).Solutiondef count_set_bits(n): count = 0 while n: n &= (n - 1) count += 1 return count
Sum of Natural NumbersProblem StatementCalculate the sum of the first n natural numbers.Solutiondef sum_natural(n): return n * (n + 1) // 2
Find Cube of NumberProblem StatementCalculate the cube of a given number (n^3).Solutiondef cube(n): return n ** 3
Check Strong NumberProblem StatementDetermine if a number is a Strong number (sum of the factorials of its digits equals the original number).Solutiondef is_strong(n): def fact(x): res = 1 for i in range(1, x + 1): res *= i return res return sum(fact(int(d)) for d in str(n)) == n
Print Star Patterns (Basic)Problem StatementReturn a string representing a 3x3 square of stars.Solutiondef star_square(): return "\\n".join(["***"] * 3)
Find Highest Common Factor (HCF)Problem StatementCalculate the Highest Common Factor (HCF), which is another name for GCD.Solutiondef find_hcf(a, b): while b: a, b = b, a % b return a
Check Number SignProblem StatementDetermine if a number is positive, negative, or zero.Solutiondef check_sign(n): if n > 0: return "Positive" if n < 0: return "Negative" return "Zero"
Calculate Simple InterestProblem StatementCalculate simple interest using the formula: (P * R * T) / 100.Solutiondef simple_interest(p, r, t): return (p * r * t) / 100
Calculate Compound InterestProblem StatementCalculate compound interest (CI = P(1 + r/n)^(nt) - P).Solutiondef compound_interest(p, r, t): amount = p * (pow((1 + r / 100), t)) return amount - p
Count FactorsProblem StatementCount the total number of divisors (factors) of a given number.Solutiondef count_factors(n): return sum(1 for i in range(1, n + 1) if n % i == 0)
Find AverageProblem StatementCalculate the average of a list of numbers.Solutiondef find_average(arr): return sum(arr) / len(arr) if arr else 0
Check DivisibilityProblem StatementCheck if number 'a' is divisible by number 'b'.Solutiondef is_divisible(a, b): return a % b == 0 if b != 0 else False
Generate Random Number (Basic)Problem StatementSimulate getting a random number by using a pseudo-random multiplier logic.Solutiondef pseudo_random(seed): # Linear Congruential Generator return (seed * 1103515245 + 12345) % (2**31)
Convert Celsius to FahrenheitProblem StatementFormula: F = (C * 9/5) + 32.Solutiondef c_to_f(c): return (c * 9/5) + 32
Convert Fahrenheit to CelsiusProblem StatementFormula: C = (F - 32) * 5/9.Solutiondef f_to_c(f): return (f - 32) * 5/9
Check Buzz NumberProblem StatementA number is a Buzz number if it ends with 7 or is divisible by 7.Solutiondef is_buzz(n): return n % 10 == 7 or n % 7 == 0
Check Neon NumberProblem StatementA number whose sum of digits of its square is equal to the number itself (e.g., 9: 9^2=81, 8+1=9).Solutiondef is_neon(n): sq = n * n s_digits = sum(int(d) for d in str(sq)) return s_digits == n
Check Spy NumberProblem StatementA number where the sum of its digits equals the product of its digits (e.g., 123: 1+2+3 = 6, 1*2*3 = 6).Solutiondef is_spy(n): digits = [int(d) for d in str(n)] s = sum(digits) p = 1 for d in digits: p *= d return s == p
Find Smallest DigitProblem StatementFind the smallest numeric digit in a given integer.Solutiondef smallest_digit(n): return min(int(d) for d in str(abs(n)))
Find Largest DigitProblem StatementFind the largest numeric digit in a given integer.Solutiondef largest_digit(n): return max(int(d) for d in str(abs(n)))
Count Odd DigitsProblem StatementCount how many odd numeric digits are in an integer.Solutiondef count_odd_digits(n): return sum(1 for d in str(abs(n)) if int(d) % 2 != 0)
Count Even DigitsProblem StatementCount how many even numeric digits (including 0) are in an integer.Solutiondef count_even_digits(n): return sum(1 for d in str(abs(n)) if int(d) % 2 == 0)
Print Star Pattern (Triangle)Problem StatementReturn a string representing a right-angled star triangle of height 3.Solutiondef star_triangle(): return "\\n".join(["*" * i for i in range(1, 4)])
Print Number PatternProblem StatementReturn a string representing a number triangle (1, 12, 123) of height 3.Solutiondef num_triangle(): return "\\n".join(["".join(map(str, range(1, i+1))) for i in range(1, 4)])
Check Harshad NumberProblem StatementA Harshad (or Niven) number is an integer that is divisible by the sum of its digits.Solutiondef is_harshad(n): s_digits = sum(int(d) for d in str(n)) return n % s_digits == 0 if s_digits != 0 else False
Sum of First N NumbersProblem StatementCalculate the sum of numbers from 1 up to n.Solutiondef sum_n(n): return n * (n + 1) // 2
Print Hollow Square PatternProblem StatementReturn a string for a 3x3 hollow square of stars.Solutiondef hollow_square(): return "***\\n* *\\n***"