From 9af65c8ac721024d0e45a2a6bde5c3f9fe638516 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 7 Apr 2020 17:20:43 +0200 Subject: problem 1 2 4 in common lisp --- lisp/004-largest_palindrome_product.lisp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lisp/004-largest_palindrome_product.lisp (limited to 'lisp/004-largest_palindrome_product.lisp') diff --git a/lisp/004-largest_palindrome_product.lisp b/lisp/004-largest_palindrome_product.lisp new file mode 100644 index 0000000..8cbc7c9 --- /dev/null +++ b/lisp/004-largest_palindrome_product.lisp @@ -0,0 +1,23 @@ +;;;; ;;;;;;;; +;;;; Largest palindrome product +;;;; Problem 4 +;;;; +;;;; 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 palindrome-p (n) + (equal (write-to-string n) (reverse (write-to-string n)))) + +(print (palindrome-p 32)) + +(defvar *largest* 0) + +(loop :for x :from 999 :downto 1 :do + (loop :for y :from 999 :downto 1 :do + (when (and (palindrome-p (* x y)) (> (* x y) *largest*)) + (print (* x y)) + (setf *largest* (* x y))))) + +(print *largest*) -- cgit