gpt4 book ai didi

lisp - 对 project euler 4 的 lisp 程序的反馈

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:12 25 4
gpt4 key购买 nike

<分区>

我刚开始学习 common lisp,所以我一直在研究 project euler 问题。这是我的解决方案(在 https://github.com/qlkzy/project-euler-cl 的帮助下)。你们有什么关于风格改变的建议,以及让它更口齿不清的类型吗?

; 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))

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com