Welcome to Math Logic CodeSnap
The 'Math & Logic' category encompasses problems requiring mathematical insights, number theory concepts, logical reasoning, or manipulation of numerical data. Topics range from basic arithmetic and number properties (FizzBuzz, Palindrome Number, Power of 3) to more advanced concepts like prime numbers (Count Primes, Prime Factorization), GCD/LCM, base conversions (Excel Sheet Titles), and numerical algorithms (Sqrt(x), Pow(x,n)). These problems often test careful handling of edge cases, understanding of mathematical definitions, and the ability to devise efficient computational methods for numerical tasks.
Table of Contents
FizzBuzz
Write a program that outputs string representations of numbers from 1 to n. For multiples of three, output "Fizz"; for multiples of five, output "Buzz"; for multiples of both three and five, output "FizzBuzz". Tests basic conditional logic and loops.
Count Primes
Count the number of prime numbers strictly less than a given non-negative integer `n`. The Sieve of Eratosthenes is an efficient algorithm for this.
Palindrome Number
Given an integer `x`, return `true` if `x` is a palindrome (reads the same backward as forward), and `false` otherwise. Solve without converting the integer to a string.
Greatest Common Divisor (GCD)
Find the greatest common divisor (GCD) of two non-negative integers `a` and `b`. The Euclidean algorithm provides an efficient solution.
LCM of Two Numbers
Calculate the least common multiple (LCM) of two integers `a` and `b`. This can be derived from the GCD using the formula LCM(a, b) = |a * b| / GCD(a, b).
Check Power of 3
Given an integer `n`, determine if it is a power of three (i.e., `n = 3^k` for some integer `k`). Handle edge cases like non-positive numbers.
Excel Sheet Column Title
Given a positive integer `columnNumber`, return its corresponding column title as it would appear in an Excel spreadsheet (A, B, C, ..., Z, AA, AB, AC, ...). This involves a base-26 conversion.
Excel Sheet Column Number
Given a column title string `columnTitle` as appears in an Excel sheet, return its corresponding positive integer column number. This involves converting from a base-26 representation.
Add Digits (Digital Root)
Given a non-negative integer `num`, repeatedly sum its digits until the result has only one digit. Return this single-digit result (the digital root). Can be solved with loops or a direct mathematical formula involving modulo 9.
Happy Number
Determine if a number `n` is 'happy'. Replace the number by the sum of the squares of its digits. Repeat. If it reaches 1, it's happy. If it enters a cycle without 1, it's not. Requires cycle detection.
Roman to Integer
Convert a given Roman numeral string (e.g., "III", "LVIII", "MCMXCIV") into its corresponding integer value. Handle the subtractive notation (like IV, IX, XL, etc.).
Integer to Roman
Convert a given integer (within the range [1, 3999]) into its Roman numeral representation. A greedy approach using predefined value-symbol mappings works well.
Sqrt(x)
Given a non-negative integer `x`, compute and return the integer part of its square root. Standard library functions for square root are typically disallowed. Binary search is a common approach.
Pow(x, n)
Implement the function `pow(x, n)` which calculates `x` raised to the power `n` (i.e., x^n). Handle both positive and negative exponents `n`. Efficient solutions use exponentiation by squaring.
Prime Factorization
Find all prime factors of a given positive integer `n`. Return the factors as a list. An efficient approach involves trial division up to the square root of `n`.