Factorions: Difference between revisions

Content deleted Content added
CalmoSoft (talk | contribs)
Markjreed (talk | contribs)
→‎{{header|Common Lisp}}: Add implementation
Line 345: Line 345:
factorions for base 12: 1 2
factorions for base 12: 1 2
</pre>
</pre>
=={{header|Common Lisp}}==
<lang lisp>(defparameter *bases* '(9 10 11 12))
(defparameter *limit* 1500000)

(defun ! (n) (apply #'* (loop for i from 2 to n collect i)))

(defparameter *digit-factorials* (mapcar #'! (loop for i from 0 to (1- (apply #'max *bases*)) collect i)))

(defun fact (n) (nth n *digit-factorials*))

(defun digit-value (digit)
(let ((decimal (digit-char-p digit)))
(cond ((not (null decimal)) decimal)
((char>= #\Z digit #\A) (+ (char-code digit) (- (char-code #\A)) 10))
((char>= #\z digit #\a) (+ (char-code digit) (- (char-code #\a)) 10))
(t nil))))

(defun factorionp (n &optional (base 10))
(= n (apply #'+
(mapcar #'fact
(map 'list #'digit-value
(write-to-string n :base base))))))

(loop for base in *bases* do
(let ((factorions
(loop for i from 1 while (< i *limit*) if (factorionp i base) collect i)))
(format t "In base ~a there are ~a factorions:~%" base (list-length factorions))
(loop for n in factorions do
(format t "~c~a" #\Tab (write-to-string n :base base))
(if (/= base 10) (format t " (decimal ~a)" n))
(format t "~%"))
(format t "~%")))</lang>

{{Out}}
<pre>In base 9 there are 3 factorions:
1 (decimal 1)
2 (decimal 2)
62558 (decimal 41282)

In base 10 there are 4 factorions:
1
2
145
40585

In base 11 there are 5 factorions:
1 (decimal 1)
2 (decimal 2)
24 (decimal 26)
44 (decimal 48)
28453 (decimal 40472)

In base 12 there are 2 factorions:
1 (decimal 1)
2 (decimal 2)
</pre>


=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}