Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; a bignum is characterized by a list of 0-9 integers in
- ;; reverse order of their base-10 representation,
- ;; followed by a zero
- ;; - nums : list
- (define-struct bignum (nums))
- ;; number2bignum : number -> bignum
- ;; Inputs : an integer, num
- ;; Output : a list with the digits of num in reverse order,
- ;; followed by a zero
- (define (number2bignum num)
- (cond
- [(= num 0) (make-bignum (list 0))]
- [(not (= num 0)) (make-bignum (cons (remainder num 10)
- (bignum-nums (number2bignum (floor (/ num 10))))))]))
- (check-expect (number2bignum 0) (make-bignum (list 0)))
- (check-expect (number2bignum 1) (make-bignum (list 1 0)))
- (check-expect (number2bignum 126) (make-bignum (list 6 2 1 0)))
- (check-expect (number2bignum 6581) (make-bignum (list 1 8 5 6 0)))
- (check-expect (number2bignum 9876) (make-bignum (list 6 7 8 9 0)))
- ;; bignum2number : bignum -> number
- ;; Inputs : a bignum, bnum
- ;; Output : the number with the digits of bignum in reverse order
- (define (bignum2number bnum)
- (cond
- [(empty? (bignum-nums bnum)) 0]
- [(cons? (bignum-nums bnum))
- (+ (first (bignum-nums bnum))
- (* 10 (bignum2number (make-bignum (rest (bignum-nums bnum))))))]))
- (check-expect (bignum2number (make-bignum (list 0))) 0)
- (check-expect (bignum2number (make-bignum (list 1 0))) 1)
- (check-expect (bignum2number (make-bignum (list 6 2 1 0))) 126)
- (check-expect (bignum2number (make-bignum (list 1 8 5 6 0))) 6581)
- (check-expect (bignum2number (make-bignum (list 6 7 8 9 0))) 9876)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;Bignum+;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; a helper procedure for bignum+, requires the extra parameter carry
- ;; plus-helper : bignum, bignum, number -> bignum
- ;; Inputs : two bignums, bnum1 and bnum2, and a number, carry
- ;; Output : the sum of bnum1 and bnum2
- (define (plus-helper bnum1 bnum2 carry)
- (cond
- [(empty? (bignum-nums bnum1)) bnum2]
- [(empty? (bignum-nums bnum2)) bnum1]
- [else (make-bignum (cons
- (bnrem
- (bnadd carry
- (bnadd (first (bignum-nums bnum1))
- (first (bignum-nums bnum2))))
- 10)
- (bignum-nums
- (plus-helper
- (make-bignum (rest (bignum-nums bnum1)))
- (make-bignum (rest (bignum-nums bnum2)))
- (bnquo
- (bnadd carry
- (bnadd (first (bignum-nums bnum1))
- (first (bignum-nums bnum2))))
- 10)))))]))
- ;;
- ;; bignum+ : bignum, bignum -> bignum
- ;; Inputs : two bignums, bnum1 and bnum2
- ;; Output : the sum of bnum1 and bnum2
- (define (bignum+ bnum1 bnum2)
- (number2bignum (bignum2number (plus-helper bnum1 bnum2 0))))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;Bignum*;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (define (singlemult s mul carry)
- (cond
- [(empty? (bignum-nums mul)) empty]
- [(cons? (bignum-nums mul)) (cons (bnadd carry (bnrem (bnmult s (first (bignum-nums mul))) 10 ))
- (singlemult s (make-bignum (rest (bignum-nums mul))) (bnquo (bnmult s (first (bignum-nums mul))) 10 )))]))
- (define (bignum* a b)
- (cond
- [(empty? (bignum-nums b)) (make-bignum (list 0))]
- [(cons? (bignum-nums b)) (bignum+ (number2bignum (bignum2number (make-bignum (singlemult (first (bignum-nums b)) a 0))))
- (make-bignum (cons 0 (bignum-nums (bignum* (make-bignum (rest (bignum-nums b)))
- (make-bignum (bignum-nums a)))))))]))
- (check-expect (bignum2number (bignum* (make-bignum (list 5 2 0 ))
- (make-bignum (list 5 2 0 )))) 625)
- (check-expect (bignum2number (bignum* (make-bignum (list 0 3 0 ))
- (make-bignum (list 0 2 0 )))) 600)
- (check-expect (bignum* (make-bignum (list 0))
- (make-bignum (list 0)))
- (make-bignum (list 0)))
- (check-expect (bignum* (make-bignum (list 1 0)) (make-bignum (list 9 0)))
- (make-bignum (list 9 0)))
- (check-expect (bignum* (make-bignum (list 1 0)) (make-bignum (list 9 0)))
- (make-bignum (list 9 0)))
- (check-expect (bignum* (make-bignum (list 1 2 0)) (make-bignum (list 9 0)))
- (make-bignum (list 9 8 1 0)))
- (check-expect (bignum* (make-bignum (list 5 5 5 5 0))
- (make-bignum (list 1 2 3 0)))
- (make-bignum (list 5 5 1 3 8 7 1 0)))
- (check-expect (bignum* (make-bignum (list 5 6 0))
- (make-bignum (list 5 6 7 8 9 0)))
- (make-bignum (list 5 2 7 9 1 4 6 0)))
- (check-expect (bignum* (make-bignum (list 1 1 1 1 0))
- (make-bignum (list 1 1 1 1 0)))
- (make-bignum (list 1 2 3 4 3 2 1 0)))
- (check-expect (bignum* (make-bignum (list 1 2 3 4 5 6 7 8 9 0))
- (make-bignum (list 1 2 3 4 5 6 7 8 9 0)))
- (make-bignum (list 1 4 0 1 7 9 9 8 7 7 5 0 1 6 4 5 7 9 0)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement