2013年5月29日 星期三

LISP NCNU HW手寫exercise2.1



(define (make-rat n d)
(cond ( (and (< n 0) (< d 0)) (let ((g (gcd n d))) (cons (/ (- n) g) (/ (- d) g))))
     ( (and (> n 0) (< d 0)) (let ((g (gcd n d))) (cons (/ (- n) g) (/ (- d) g))))
     (else (let ((g (gcd n d))) (cons (/ n g) (/ d g))))
)
)
(define (gcd a b)
  (if (= b 0)
      (if (< a 0) (- a)
    a)
      (gcd b (remainder a b))))

NCNU LISP HW7


(define (iterative-improve ok? do)
(define (iim guess)
(if (ok? guess) guess
                    (iim (do guess))
)
)
iim)
(define (square x)
(* x x))
(define (average x y)
    (/ (+ x y) 2))

(define (sqrt x)
   ((iterative-improve (lambda (guess)
                        (< (abs (- (square guess) x)) 0.001))
                       (lambda (guess)
                          (average guess (/ x guess))))
   1.0)
)

(define (fixed-point f guess)
   ((iterative-improve (lambda (guess)
                          (< (abs (- (f guess) guess)) 0.00001))
                       (lambda (guess)
                          (f guess)))
    guess)
)

NCNU LISP HW8

exercise 8: Combining three numbers with exponentiation and multiplication
  • Refer to exercise 2.5 but the pair is replaced with a triple. That is, the interface functions are :
    (combine a b c), (first x), (second x), (third x)
(define (combine a b c)
  (do-combine a b c 1)
(define (do-combine a b c num)
(if (= a 0)
(if (= b 0)
(if (= c 0) num
           (do-combine a b (- c 1) (* 5 num))
)
   (do-combine a (- b 1) c (* 4 num))
)
   (do-combine (- a 1) b c (* 3 num))
  )
)
(define (first x)
(common x 3)
)
(define (second x)
(common x 4)
)
(define (third x)
(common x 5)
)
(define (common n f)
(define (do-common x num)
(if (> (remainder x f) 0)
   num
    (do-common (/ x f) (+ num 1))
)
)
 (do-common n 0)
)



2013年5月16日 星期四

NCNU LISP HW6


computing the continued fractions
  • Refer to exercise 1.37.
  • Both versions of linear recusive and linear iterative are required.
  • Deadline: 2013 Apr 27 00:05 am


(define (cont-frac-iterative N D K)
        (do-cont-frac-iterative N D K 0)
)
(define (do-cont-frac-iterative N D K result)
        (if (= k 0) result
                    (do-cont-frac-iterative N D (- k 1) (/ (N K) (+ (D K)
result) ))
        )
)
(define (cont-frac-recursive N D K)
        (do-cont-frac-recursive N D K 1)
)

(define (do-cont-frac-recursive N D K counter)
        (if (= k 1) (/ (N counter) (D counter))
                    (/ (N counter) (+ (D counter) (do-cont-frac-recursive N
D (- k 1) (+ counter 1))))
        )
)

2013年5月12日 星期日

UVA 106 C++


#include<iostream>
using namespace std;
int gcd(int,int);
int main()
{
int n;
while(cin>>n)
{
bool *stored=new bool[n+1];
int count=0,second_count=0;
int f,s,t;
for(int a=1;a<=1000;a++)
for(int b=1;b<=a;b++)
{
if(gcd(a,b)!=1)
continue;
if((a-b)%2==0)
continue;
t=a*a+b*b;
if(t>n)
break;
count++;
f=a*a-b*b,s=2*a*b;
for (int i=1; t*i<=n; i++ )
stored[i*f]=stored[i*s]=stored[i*t]=1;
}
for(int j=1;j<=n;j++)
if(stored[j]==1)
second_count++;
cout<<count<<" "<<n-second_count<<endl;
}
return 0;
}
int gcd(int m,int n)
{
int Remainder;
while(Remainder=m%n)
{
m=n;
n=Remainder;
}
return n;
}

2013年5月2日 星期四

NCNU LISP HW5

omputing the continued fractions
  • Refer to exercise 1.37.
  • Both versions of linear recusive and linear iterative are required.
/////////////////////////////////////////////////////////////////////////////////

(define (cont-frac-iterative N D K)
(do-cont-frac-iterative N D K 0)
)
(define (do-cont-frac-iterative N D K result)
(if (= k 0) result
           (do-cont-frac-iterative N D (- k 1) (/ (N K) (+ (D K) result) ))
)
)
(define (cont-frac-recursive N D K)
(do-cont-frac-recursive N D K 1)
)

(define (do-cont-frac-recursive N D K counter)
(if (= k 1) (/ (N counter) (D counter))
   (/ (N counter) (+ (D counter) (do-cont-frac-recursive N D (- k 1) (+ counter 1))))    
)
)