Tuesday, August 18, 2009

Euler Project 63

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.

How many n-digit positive integers exist which are also an nth power?


My Solution

(defun Euler63(n)
(loop for i from 1 to n
sum (loop for j from 1 to n
sum (if (= j (length (format nil "~a" (expt i j))))
1
0))))

Others' Solution
First Solution
(defun euler63 (&optional (n 1) (total 0))
(let ((min-base (ceiling (expt (expt 10 (1- n)) (/ 1 n))))
(max-base 9))
(cond ((> min-base max-base) total)
(t (do ((n min-base (1+ n))) ((> n max-base))
(incf total))
(euler63 (1+ n) total)))))

Second Solution
(do ((i 1 (1+ i)) (sum 0))
((> i 21) sum)
(incf sum (- 10 (ceiling (expt 10 (/ (1- i) i))))))

No comments:

Post a Comment