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