| Paste number 54924: | lychrel numbers |
| Pasted by: | lnostdal |
| When: | 2 years, 1 week ago |
| Share: | Tweet this! | http://paste.lisp.org/+16DO |
| Channel: | None |
| Paste contents: |
#| From wikipedia:
A Lychrel number is a natural number which cannot form a palindrome through the iterative process of repeatedly reversing its base 10 digits and adding the resulting numbers.
|#
(defmacro for ((var initial-value pred &optional update-value) &body body)
"Similar to the `for' keyword in other languages."
(if update-value
`(do ((,var ,initial-value ,update-value)) ((not ,pred))
,@body)
`(do ((,var, initial-value)) ((not ,pred))
,@body)))
(defparameter *max-number-of-tries* 2000
"How many times to try the reverse and add process.")
(defun reverse-of (number)
"Return a number based on the reverse sequence of digits in `number'."
(parse-integer (nreverse (princ-to-string number))))
(defun palindrome-p (number)
"Returns `number' if `number' is a palindrome. NIL otherwise."
(if (= (reverse-of number) number)
number
nil))
(defun reverse-and-add-of (number)
"Returns the sum of `number' and the number designated by reversing the
digits in `number'."
(+ number (reverse-of number)))
(defun reverse-and-add-process (number &optional (max-tries *max-number-of-tries*))
"Returns the following values:
* T or NIL, based on whether the algorithm thinks `number' is a lychrel number.
* `current-number',
* `steps', the number of reverse and add operations executed.
"
(let ((step 1) (current-number number))
(loop
(setf current-number (reverse-and-add-of current-number))
(if (palindrome-p current-number)
;; Not a lychrel number, for sure.
(return (values nil current-number step))
(when (= (incf step) max-tries)
;; Might be a lychrel number; we give up.
(return (values t current-number step)))))))
(defun lychrel (number)
(multiple-value-bind (lychrel-p resulting-number steps)
(reverse-and-add-process number)
(if lychrel-p
(format t "After ~A steps, ~A is still not a palindrome (it's a lychrel candidate in this test).~%"
steps number)
(format t "After ~A steps, ~A became the palindrome ~A and is thus not a lychrel number.~%"
steps number resulting-number))))
#|
Some tests:
cl-user> (lychrel 56)
After 1 steps, 56 became the palindrome 121 and is thus not a lychrel number.
nil
cl-user> (lychrel 57)
After 2 steps, 57 became the palindrome 363 and is thus not a lychrel number.
nil
cl-user> (lychrel 59)
After 3 steps, 59 became the palindrome 1111 and is thus not a lychrel number.
nil
cl-user> (lychrel 89)
After 24 steps, 89 became the palindrome 8813200023188 and is thus not a lychrel number.
nil
cl-user> (lychrel 10911)
After 55 steps, 10911 became the palindrome 4668731596684224866951378664 and is thus not a lychrel number.
nil
cl-user> (lychrel 1186060307891929990)
After 261 steps, 1186060307891929990 became the palindrome 44562665878976437622437848976653870388884783662598425855963436955852489526638748888307835667984873422673467987856626544 and is thus not a lychrel number.
nil
cl-user> (lychrel 196)
After 2000 steps, 196 is still not a palindrome (it's a lychrel candidate in this test).
nil
cl-user> (for (i 10 (< i 100) (incf i))
(lychrel i))
After 1 steps, 10 became the palindrome 11 and is thus not a lychrel number.
After 1 steps, 11 became the palindrome 22 and is thus not a lychrel number.
After 1 steps, 12 became the palindrome 33 and is thus not a lychrel number.
After 1 steps, 13 became the palindrome 44 and is thus not a lychrel number.
After 1 steps, 14 became the palindrome 55 and is thus not a lychrel number.
After 1 steps, 15 became the palindrome 66 and is thus not a lychrel number.
After 1 steps, 16 became the palindrome 77 and is thus not a lychrel number.
After 1 steps, 17 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 18 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 19 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 20 became the palindrome 22 and is thus not a lychrel number.
After 1 steps, 21 became the palindrome 33 and is thus not a lychrel number.
After 1 steps, 22 became the palindrome 44 and is thus not a lychrel number.
After 1 steps, 23 became the palindrome 55 and is thus not a lychrel number.
After 1 steps, 24 became the palindrome 66 and is thus not a lychrel number.
After 1 steps, 25 became the palindrome 77 and is thus not a lychrel number.
After 1 steps, 26 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 27 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 28 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 29 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 30 became the palindrome 33 and is thus not a lychrel number.
After 1 steps, 31 became the palindrome 44 and is thus not a lychrel number.
After 1 steps, 32 became the palindrome 55 and is thus not a lychrel number.
After 1 steps, 33 became the palindrome 66 and is thus not a lychrel number.
After 1 steps, 34 became the palindrome 77 and is thus not a lychrel number.
After 1 steps, 35 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 36 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 37 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 38 became the palindrome 121 and is thus not a lychrel number.
After 2 steps, 39 became the palindrome 363 and is thus not a lychrel number.
After 1 steps, 40 became the palindrome 44 and is thus not a lychrel number.
After 1 steps, 41 became the palindrome 55 and is thus not a lychrel number.
After 1 steps, 42 became the palindrome 66 and is thus not a lychrel number.
After 1 steps, 43 became the palindrome 77 and is thus not a lychrel number.
After 1 steps, 44 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 45 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 46 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 47 became the palindrome 121 and is thus not a lychrel number.
After 2 steps, 48 became the palindrome 363 and is thus not a lychrel number.
After 2 steps, 49 became the palindrome 484 and is thus not a lychrel number.
After 1 steps, 50 became the palindrome 55 and is thus not a lychrel number.
After 1 steps, 51 became the palindrome 66 and is thus not a lychrel number.
After 1 steps, 52 became the palindrome 77 and is thus not a lychrel number.
After 1 steps, 53 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 54 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 55 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 56 became the palindrome 121 and is thus not a lychrel number.
After 2 steps, 57 became the palindrome 363 and is thus not a lychrel number.
After 2 steps, 58 became the palindrome 484 and is thus not a lychrel number.
After 3 steps, 59 became the palindrome 1111 and is thus not a lychrel number.
After 1 steps, 60 became the palindrome 66 and is thus not a lychrel number.
After 1 steps, 61 became the palindrome 77 and is thus not a lychrel number.
After 1 steps, 62 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 63 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 64 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 65 became the palindrome 121 and is thus not a lychrel number.
After 2 steps, 66 became the palindrome 363 and is thus not a lychrel number.
After 2 steps, 67 became the palindrome 484 and is thus not a lychrel number.
After 3 steps, 68 became the palindrome 1111 and is thus not a lychrel number.
After 4 steps, 69 became the palindrome 4884 and is thus not a lychrel number.
After 1 steps, 70 became the palindrome 77 and is thus not a lychrel number.
After 1 steps, 71 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 72 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 73 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 74 became the palindrome 121 and is thus not a lychrel number.
After 2 steps, 75 became the palindrome 363 and is thus not a lychrel number.
After 2 steps, 76 became the palindrome 484 and is thus not a lychrel number.
After 3 steps, 77 became the palindrome 1111 and is thus not a lychrel number.
After 4 steps, 78 became the palindrome 4884 and is thus not a lychrel number.
After 6 steps, 79 became the palindrome 44044 and is thus not a lychrel number.
After 1 steps, 80 became the palindrome 88 and is thus not a lychrel number.
After 1 steps, 81 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 82 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 83 became the palindrome 121 and is thus not a lychrel number.
After 2 steps, 84 became the palindrome 363 and is thus not a lychrel number.
After 2 steps, 85 became the palindrome 484 and is thus not a lychrel number.
After 3 steps, 86 became the palindrome 1111 and is thus not a lychrel number.
After 4 steps, 87 became the palindrome 4884 and is thus not a lychrel number.
After 6 steps, 88 became the palindrome 44044 and is thus not a lychrel number.
After 24 steps, 89 became the palindrome 8813200023188 and is thus not a lychrel number.
After 1 steps, 90 became the palindrome 99 and is thus not a lychrel number.
After 2 steps, 91 became the palindrome 121 and is thus not a lychrel number.
After 1 steps, 92 became the palindrome 121 and is thus not a lychrel number.
After 2 steps, 93 became the palindrome 363 and is thus not a lychrel number.
After 2 steps, 94 became the palindrome 484 and is thus not a lychrel number.
After 3 steps, 95 became the palindrome 1111 and is thus not a lychrel number.
After 4 steps, 96 became the palindrome 4884 and is thus not a lychrel number.
After 6 steps, 97 became the palindrome 44044 and is thus not a lychrel number.
After 24 steps, 98 became the palindrome 8813200023188 and is thus not a lychrel number.
After 6 steps, 99 became the palindrome 79497 and is thus not a lychrel number.
nil
cl-user>
|#This paste has no annotations.