Jump to Table of Contents Pop Out Sidebar

Problem 20

More details about this document
Create Date:
Publish Date:
Update Date:
2023-11-06 21:20
Creator:
Emacs 29.2 (Org mode 9.6.15)
License:
This work is licensed under CC BY-SA 4.0

1. Problem

Factorial digit sum

n! means n × (n − 1) × … × 3 × 2 × 1

For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

阶乘数字和

n! 的意思是 n × (n − 1) × … × 3 × 2 × 1。

例如, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,所以 10! 的各位数字和是 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27。

求 100! 的各位数字和。

2. Solution

使用 elisp 可以简单完成:

(defun eu20-fac (n)
  (pcase n
    (0 1)
    (a (* (eu20-fac (1- a)) a))))

(- (cl-reduce (lambda (s a) (+ s a -48)) (number-to-string (eu20-fac 100))) 48)
;; 648