Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9026389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:30:27+00:00 2026-06-16T06:30:27+00:00

I just started learning common lisp and so I’ve been working on project euler

  • 0

I just started learning common lisp and so I’ve been working on project euler problems. Here’s my solution (with some help from https://github.com/qlkzy/project-euler-cl ). Do you guys have any suggestions for stylistic changes and the sort to make it more lisp-y?

; A palindromic number reads the same both ways. The largest palindrome made 
; from the product of two 2-digit numbers is 9009 = 91 99.
; Find the largest palindrome made from the product of two 3-digit numbers.

(defun num-to-list (num)
    (let ((result nil))
        (do ((x num (truncate x 10)))
            ((= x 0 ) result)
            (setq result (cons (mod x 10) result)))))

(defun palindrome? (num) 
    (let ((x (num-to-list num)))
        (equal x (reverse x))))

(defun all-n-digit-nums (n)
    (loop for i from (expt 10 (1- n)) to (1- (expt 10 n)) collect i))

(defun all-products-of-n-digit-nums (n)
    (let ((nums (all-n-digit-nums n)))
        (loop for x in nums
            appending (loop for y in nums collecting (* x y)))))

(defun all-palindromes (n)
    (let ((nums (all-products-of-n-digit-nums n)))
        (loop for x in nums
            when (palindrome? x) collecting x)))

(defun largest-palindrome (n)
    (apply 'max (all-palindromes 3)))

(print (largest-palindrome 3))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:30:28+00:00Added an answer on June 16, 2026 at 6:30 am
    (setq list (cons thing list))
    

    can be simplified to:

    (push thing list)
    

    My other comments on your code are not so much about Lisp style as about the algorithm. Creating all those intermediate lists of numbers seems like a poor way to do it, just write nested loops that calculate and test the numbers.

    (defun all-palindromes (n)
      (loop for i from (expt 10 (1- n)) to (1- (expt 10 n))
        do (loop for j from (expt 10 (1- n)) to (1- (expt 10 n))
                 for num = (* i j)
             when (palindrome? num)
               collect num)))
    

    But LOOP has a feature you can use: MAXIMIZE. So instead of collecting all the palindroms in a list with COLLECT, you can:

    (defun largest-palindrome (n)
      (loop with start = (expt 10 (1- n))
            and end = (1- (expt 10 n))
            for i from start to end
        do (loop for j from start to end
                 for num = (* i j)
             when (palindrome? num)
               maximize num)))
    

    Here’s another optimization:

    (defun largest-palindrome (n)
      (loop with start = (expt 10 (1- n))
            and end = (1- (expt 10 n))
            for i from start to end
        do (loop for j from i to end
                 for num = (* i j)
             when (palindrome? num)
               maximize num)))
    

    Making the inner loop start from i instead of start avoids the redundancy of checking both M*N and N*M.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning Common Lisp a few days ago, and I'm trying to
I just started learning Java and I encountered some problems with the tag in
I started learning Common Lisp recently, and (just for fun) decided to rename the
I've just started learning Common Lisp--and rapidly falling in love with it--and I've just
I just started learning C# and started with Windows Forms project. When I try
I just started learning C++ (coming from Java ) and am having some serious
Just started learning STL and here is the first problem: vector<int> vec1; for(int i
I just started learning Zend. I managed to get the basic working (using zf
I just started learning about Kinect through some quick start videos and was trying
I just started learning lisp in my class. I'm doing a homework assignment in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.