LISP: Computing Prime Numbers Between 1 and 25 (Inclusive)

LISP: Computing Prime Numbers Between 1 and 25 (Inclusive)

Verified Sources
Nov 4, 2026

To “write a function in LISP that computes prime number between 1 and 25 (inclusive)”, the most reliable approach is to (1) define a predicate prime? and then (2) build a function that enumerates candidates and filters using that predicate. A standard number-theory optimization is to check divisibility only up to n\sqrt{n}, because any composite nn has a factor ≤n\le \sqrt{n}.

A minimal Common Lisp solution typically uses integer loops and modular arithmetic: mod and dolist or loop.

Below is a complete, idiomatic Common Lisp implementation that returns the list of primes in the interval [1,25][1,25] (i.e., 2,3,5,7,11,13,17,19,232,3,5,7,11,13,17,19,23).

Keyword map: prime?, mod, sqrt, loop.

Common Lisp: Functions, loops, and basics (learning to read the code)

Prime predicate design

A number nn is prime if:

  1. n<2n < 2 is not prime,
  2. otherwise, test whether any integer dd in 2..⌊n⌋2.. \lfloor \sqrt{n}\rfloor divides nn evenly (i.e., n mod d=0n \bmod d = 0).

This uses the divisor bound d≤⌊n⌋d \le \lfloor\sqrt{n}\rfloor, reducing work while remaining correct.

Warning: if you accidentally start testing from d=1d=1, then every nn will appear “divisible” by 11 and you’ll misclassify primes. Always begin at 22.

prime? and divisor are the core logic pieces.

1(defun prime? (n) 2 "Return T if N is prime, NIL otherwise." 3 (cond 4 ((< n 2) nil) 5 ((= n 2) t) 6 ((evenp n) nil) 7 (t 8 ;; Check odd divisors up to sqrt(n) 9 (let ((limit (isqrt n))) 10 (loop for d from 3 to limit by 2 11 never (zerop (mod n d)))))) 12 13(defun primes-between-1-and-25 () 14 "Return the list of prime numbers between 1 and 25 inclusive." 15 (loop for n from 1 to 25 16 when (prime? n) 17 collect n)) 18 19;; Example: 20;; (primes-between-1-and-25) 21;; => (2 3 5 7 11 13 17 19 23)

Build the two functions: prime? then primes-between

  1. 1
    Step 1

    Return NIL for n<2n<2, return T for n=2n=2; even numbers greater than 2 are not prime using evenp.

  2. 2
    Step 2

    Let limit=⌊n⌋\text{limit} = \lfloor \sqrt{n} \rfloor using isqrt so the loop stays integer-only.

  3. 3
    Step 3

    Loop dd from 3 to limit by 2 and check mod with (zerop (mod n d)). If any divisor works, return NIL; otherwise return T.

  4. 4
    Step 4

    Use a loop from 1 to 25 and collect n whenever (prime? n) is true.

Pro Tip: prefer list-returning functions

If your goal is “compute primes between 1 and 25”, returning a list is the most useful shape. In Common Lisp, loop with collect makes this natural.

Common bug: wrong loop condition for divisibility

If you accidentally write something like for d below limit (excluding the limit) you can misclassify squares (e.g., n=9n=9 or n=25n=25) because the actual factor can equal n\sqrt{n}. Use an inclusive upper bound when checking d≤⌊n⌋d \le \lfloor\sqrt{n}\rfloor.

How you should develop this solution

Define prime? predicate

Step 1

Implement edge cases, even check, then divisor test up to ⌊n⌋\lfloor\sqrt{n}\rfloor."

Enumerate interval

Step 2

Loop over nn from 1 to 25 inclusive and filter using (prime? n)."

Verify with known primes

Step 3

Expected output: (2 3 5 7 11 13 17 19 23)."

Common variants and edge cases

Primes between 1 and 25

Expected result list values

Knowledge Check

Question 1 of 4
Q1Single choice

In a prime-checking function, why do we test divisors only up to ⌊n⌋\lfloor\sqrt{n}\rfloor?