Lisp Century From Year
(defun century-from-year (year)
(ceiling year 100))
This converts a year into its century. Years 1-100 are century 1, 101-200 are century 2, and so on.
Lisp Check Palindrome
(defun check-palindrome (input-string)
(string= (reverse input-string) input-string))
This compares the string with its reverse. If they match, it is a palindrome.
Lisp Chocolates By Numbers
(defun choc-gcd (n m)
(if (zerop (mod n m))
m
(choc-gcd m (mod n m))))
(defun chocolates-by-numbers (n m)
(/ (/ (* n m) (choc-gcd n m)) m))
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
Lisp Common Prime Divisors
(defun cpd-gcd (n m)
(if (zerop (mod n m))
m
(cpd-gcd m (mod n m))))
(defun remove-common-prime-divisors (n m)
(loop while (/= n 1)
do (let ((d (cpd-gcd n m)))
(when (= d 1)
(return-from remove-common-prime-divisors n))
(setf n (/ n d))))
n)
(defun common-prime-divisors (a b)
(let ((counter 0))
(loop for x in a
for y in b
do (let* ((d (cpd-gcd x y))
(rx (remove-common-prime-divisors x d)))
(when (= rx 1)
(let ((ry (remove-common-prime-divisors y d)))
(when (= ry 1)
(incf counter))))))
counter))
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Lisp Count Div
(defun count-div (a b k)
(let* ((first-div (if (zerop (mod a k)) a (+ a (- k (mod a k)))))
(last-div (- b (mod b k))))
(1+ (/ (- last-div first-div) k))))
This counts how many numbers in a range are divisible by K without looping through every value.
Lisp Count Factors
(defun count-factors (n)
(let ((count 0)
(i 1))
(loop while (< (* i i) n)
do (progn
(when (zerop (mod n i))
(incf count 2))
(incf i)))
(when (= (* i i) n)
(incf count))
count))
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Lisp Count Non Divisible
(defun count-non-divisible (a)
(let* ((vec (coerce a 'vector))
(size (length vec))
(occurrences (make-array (1+ (reduce #'max vec)) :initial-element 0)))
(loop for v across vec do (incf (aref occurrences v)))
(loop for v across vec
collect (let ((count 0) (i 1))
(loop while (<= (* i i) v)
do (progn
(when (zerop (mod v i))
(incf count (aref occurrences i))
(unless (= (/ v i) i)
(incf count (aref occurrences (/ v i)))))
(incf i)))
(- size count)))))
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.
Lisp Count Semi Primes
(defun count-semi-primes (n p q)
(let ((primes (make-array (1+ n) :initial-element t))
(semi-primes (make-array (1+ n) :initial-element 0)))
(loop for i from 2 while (<= (* i i) n)
do (when (aref primes i)
(loop for k from (* i i) to n by i
do (setf (aref primes k) nil))))
(loop for k from 2 while (<= (* k k) n)
do (when (aref primes k)
(loop for i from 2 while (<= (* i k) n)
do (when (aref primes i)
(setf (aref semi-primes (* k i)) 1)))))
(loop for i from 1 to n
do (incf (aref semi-primes i) (aref semi-primes (1- i))))
(loop for v in p
for qi in q
collect (- (aref semi-primes qi) (aref semi-primes (1- v))))))
This precomputes semiprimes and prefix sums so each range query becomes a quick subtraction.
Lisp Cyclic Rotation
(defun cyclic-rotation (a k)
(if (null a)
a
(let ((result (copy-list a)))
(dotimes (i k)
(let ((last (car (last result))))
(setf result (cons last (butlast result)))))
result)))
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Lisp Distinct
(defun distinct (a)
(length (remove-duplicates a :test #'equal)))
This counts unique values by tracking what has already been seen.