Problem 16

(require 'calc-ext)
(defvar calc-command-flags nil)

(defun convert-decimal-to-binary (d)
  (let (b (i 0))
    (while (> d 0)
      (setq b (cons (if (eq (% d 2) 0) 0 1) b))
      (setq d (/ d 2))
      (setq i (1+ i)) )
    b))

(defun digits-sum (str)
  (let ((sum 0))
    (dotimes (i (length str) sum)
      (setq sum (+ sum (- (aref str i) ?0))))))

(let ((b (convert-decimal-to-binary (math-read-number (car argv)))))
  (let ((len (1- (length b))) (tmp 2) (pow-lst '(2)) (rst 1))
    (dotimes (i len)
      (setq tmp (math-pow tmp 2))
      (setq pow-lst (cons tmp pow-lst)))
    (while b
      (setq rst (math-mul rst (if (zerop (car b)) 1 (car pow-lst))))
      (setq b (cdr b))
      (setq pow-lst (cdr pow-lst)))
    (princ (digits-sum (math-format-value rst)))
    (princ "\n")))
メモ

これQCQIでやったところだ!

    1. 指数部分を2進数展開しておく:  2^n = 2^{b_1 + b_2 \cdot 2^1 + \cdots + b_m  \cdot 2^m}
    2.  2^{2^k} = (2^{2^{(k-1)}})^2の規則に従って,  2^{2^k}をすべて計算.
    3. b_k \neq 0 の項のみかけ合わせる.

という手順.