C# Century From Year
static int CenturyFromYear(int year)
{
return (int)Math.Ceiling(year / 100.0);
}
This converts a year into its century. Years 1-100 are century 1, 101-200 are century 2, and so on.
C# Check Palindrome
static bool CheckPalindrome(string inputString)
{
var reversed = new string(inputString.Reverse().ToArray());
return reversed == inputString;
}
This compares the string with its reverse. If they match, it is a palindrome.
C# Chocolates By Numbers
static long ChocolatesByNumbers(long n, long m)
{
long Gcd(long x, long y) => x % y == 0 ? y : Gcd(y, x % y);
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.
C# Common Prime Divisors
static int CommonPrimeDivisors(int[] a, int[] b)
{
long Gcd(long n, long m) => n % m == 0 ? m : Gcd(m, n % m);
long RemoveCommonPrimeDivisors(long n, long m)
{
while (n != 1)
{
var d = Gcd(n, m);
if (d == 1)
{
break;
}
n /= d;
}
return n;
}
var counter = 0;
for (int i = 0; i < a.Length; i++)
{
long x = a[i];
long y = b[i];
var d = Gcd(x, y);
x = RemoveCommonPrimeDivisors(x, d);
if (x != 1)
{
continue;
}
y = RemoveCommonPrimeDivisors(y, d);
if (y == 1)
{
counter++;
}
}
return counter;
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
C# Count Div
static long CountDiv(long a, long b, long k)
{
var firstDiv = a % k == 0 ? a : a + (k - a % k);
var lastDiv = b - b % k;
return (lastDiv - firstDiv) / k + 1;
}
This counts how many numbers in a range are divisible by K without looping through every value.
C# Count Factors
static int CountFactors(long n)
{
var count = 0;
long i = 1;
while (i * i < n)
{
if (n % i == 0)
{
count += 2;
}
i++;
}
if (i * i == n)
{
count++;
}
return count;
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
C# Count Non Divisible
static int[] CountNonDivisible(int[] a)
{
var size = a.Length;
var nondivisor = new int[size];
var occurrences = new int[a.Max() + 1];
foreach (var v in a)
{
occurrences[v]++;
}
for (int k = 0; k < size; k++)
{
var v = a[k];
var count = 0;
var i = 1;
while (i * i <= v)
{
if (v % i == 0)
{
count += occurrences[i];
if (v / i != i)
{
count += occurrences[v / i];
}
}
i++;
}
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.
C# Count Semi Primes
static int[] CountSemiPrimes(int n, int[] p, int[] q)
{
var primes = new bool[n + 1];
Array.Fill(primes, true);
var semiPrimes = new int[n + 1];
for (int i = 2; i * i <= n; i++)
{
if (primes[i])
{
for (int k = i * i; k <= n; k += i)
{
primes[k] = false;
}
}
}
for (int k = 2; k * k <= n; k++)
{
if (primes[k])
{
for (int i = 2; i * k <= n; i++)
{
if (primes[i])
{
semiPrimes[k * i] = 1;
}
}
}
}
for (int i = 1; i <= n; i++)
{
semiPrimes[i] += semiPrimes[i - 1];
}
var semiPrimeCounts = new int[p.Length];
for (int k = 0; k < p.Length; k++)
{
semiPrimeCounts[k] = semiPrimes[q[k]] - semiPrimes[p[k] - 1];
}
return semiPrimeCounts;
}
This precomputes semiprimes and prefix sums so each range query becomes a quick subtraction.
C# Cyclic Rotation
static int[] CyclicRotation(int[] a, int k)
{
if (a.Length == 0)
{
return a;
}
var list = new List<int>(a);
for (int i = 0; i < k; i++)
{
var last = list[^1];
list.RemoveAt(list.Count - 1);
list.Insert(0, last);
}
return list.ToArray();
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
C# Distinct
static int Distinct(int[] a)
{
return new HashSet<int>(a).Count;
}
This counts unique values by tracking what has already been seen.