99道lisp练习题----(二)数学

其他解法请参考: http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html

Arithmetic

P31 (**) Determine whether a given integer number is prime. (判断一个数是不是
Example:
* (is-prime 7)

T

(defun is-prime (num)
  (let ((n (sqrt num)))
    (labels ((rec (i)
	       (if (> i n)
		   t
		   (and (not (zerop (mod num i)))
			(rec (1+ i))))))
      (rec 2))))

P32 (**) Determine the greatest common divisor of two positive integer numbers.
Use Euclid's algorithm.
Example:
* (gcd 36 63)

9

(defun my-gcd (n1 n2)
  (if (zerop n2)
      n1
      (my-gcd n2 (mod n1 n2))))

P33 (*) Determine whether two positive integer numbers are coprime.
Two numbers are coprime if their greatest common divisor equals 1.
Example:
* (coprime 35 64)

T

(defun coprime (n1 n2)
  (= (my-gcd n1 n2)
     1))

P34 (**) Calculate Euler's totient function phi(m).
Euler's so-called totient function phi(m) is defined as the number of positive integers r (1 <= r < m) that are coprime to m.

Example: m = 10: r = 1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1.

* (totient-phi 10)
4

Find out what the value of phi(m) is if m is a prime number. Euler's totient function plays an important role in one of the most widely used public key cryptography methods (RSA). In this exercise you should use the most primitive method to calculate this function (there are smarter ways that we shall discuss later).

(defun phi (m)
  (labels ((rec (i count)
	     (cond ((> i m)
		    count)
		   ((coprime m i)
		    (rec (1+ i)
			 (1+ count)))
		   (t (rec (1+ i) count)))))
    (rec 1 0)))

P35 (**) Determine the prime factors of a given positive integer.
Construct a flat list containing the prime factors in ascending order.
Example:
* (prime-factors 315)

(3 3 5 7)

(defun prime-factors (m)
  (labels ((rec (left i acc)
	     (cond ((is-prime left)
		    (nreverse (cons left acc)))
		   ((zerop (mod left i))
		    (rec (/ left i) i (cons i acc)))
		   (t (rec left (1+ i) acc)))))
    (rec m 2 nil)))

P36 (**) Determine the prime factors of a given positive integer (2).
Construct a list containing the prime factors and their multiplicity.
Example:
* (prime-factors-mult 315)
((3 2) (5 1) (7 1))

Hint: The problem is similar to problem P13.

(defun prime-factor-mul (m)
  (general-process #'cons
		   #'(lambda (subl)
		       (list (car subl) (length subl)))
		   (prime-factors m)))

P37 (**) Calculate Euler's totient function phi(m) (improved).
See problem P34 for the definition of Euler's totient function. If the list of the prime factors of a number m is known in the form of problem P36 then the function phi(m) can be efficiently calculated as follows: Let ((p1 m1) (p2 m2) (p3 m3) ...) be the list of prime factors (and their multiplicities) of a given number m. Then phi(m) can be calculated with the following formula:

phi(m) = (p1 - 1) * p1 ** (m1 - 1) + (p2 - 1) * p2 ** (m2 - 1) + (p3 - 1) * p3 ** (m3 - 1) + ...

Note that a ** b stands for the b'th power of a.

上面的公式不对,应该使用乘法连接起来

phi(m) = (p1 - 1) * p1 ** (m1 - 1) * (p2 - 1) * p2 ** (m2 - 1) * (p3 - 1) * p3 ** (m3 - 1) +

(defun phi-improved (m)
  (reduce #'* (prime-factor-mul m) :initial-value 1
	  :key #'(lambda (x)
		   (let ((p (car x))
			 (m (cadr x)))
		     (* (- p 1)
			(expt p (- m 1)))))))

P38 (*) Compare the two methods of calculating Euler's totient function.

Use the solutions of problems P34 and P37 to compare the algorithms. Take the number of logical inferences as a measure for efficiency. Try to calculate phi(10090) as an example.

O(most p)  O(log1 + (log 2) + log3 + log4)

P39 (*) A list of prime numbers.

Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.

(defun range-prime (a b)
  (mapcan #'(lambda (x)
	      (if (is-prime x)
		  (list x)))
	  (range a b)))

P40 (**) Goldbach's conjecture.
Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been  numerically confirmed up to very large numbers (much larger than we can go with our Prolog system). Write a predicate to find the two prime numbers that sum up to a given even integer.

Example:
* (goldbach 28)
(5 23)

(defun goldbach (num)
  (let ((stop (/ num 2)))
    (labels ((rec (i)
	       (cond ((> i stop) nil)
		     ((and (is-prime i)
			   (is-prime (- num i)))
		      (list i (- num i)))
		     (t (rec (1+ i))))))
      (rec 2))))

P41 (**) A list of Goldbach compositions.
Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.

Example:
* (goldbach-list 9 20)
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17

In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000.

Example (for a print limit of 50):
* (goldbach-list 1 2000 50)
992 = 73 + 919
1382 = 61 + 1321
1856 = 67 + 1789
1928 = 61 + 1867

(defun goldbach (num &optional (least 2))
  (let ((stop (/ num 2)))
    (labels ((rec (i)
	       (cond ((> i stop) nil)
		     ((and (is-prime i)
			   (is-prime (- num i)))
		      (list i (- num i)))
		     (t (rec (1+ i))))))
      (rec least))))

(defun goldbach-list (start end &optional (least 2))
  (labels ((rec (i)
	     (cond ((> i end) nil)
		   ((evenp i)
		    (progn
		      (let ((result (goldbach i least)))
			(if (null result)
			    (format t "~d=?~%" i)
			    (format t 
				    "~d=~d+~d~%" 
				    i (car result) (cadr result))))
		      (rec (1+ i))))
		   (t (rec (1+ i))))))
    (rec start)))
  

你可能感兴趣的:(99道lisp练习题----(二)数学)