How to resolve the algorithm Long multiplication step by step in the Racket programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Long multiplication step by step in the Racket programming language

Table of Contents

Problem Statement

Explicitly implement   long multiplication.
This is one possible approach to arbitrary-precision integer algebra.

For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: The output of   264 * 264   is   2128,   and is:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Long multiplication step by step in the Racket programming language

Source code in the racket programming language

#lang racket

(define (mult A B)
  (define nums
    (let loop ([B B] [zeros '()])
      (if (null? B)
        '()
        (cons (append zeros (let loop ([c 0] [A A])
                              (cond [(pair? A)
                                     (define-values [q r]
                                       (quotient/remainder
                                        (+ c (* (car A) (car B)))
                                        10))
                                     (cons r (loop q (cdr A)))]
                                    [(zero? c) '()]
                                    [else (list c)])))
              (loop (cdr B) (cons 0 zeros))))))
  (let loop ([c 0] [nums nums])
    (if (null? nums)
      '()
      (let-values ([(q r) (quotient/remainder (apply + c (map car nums)) 10)])
        (cons r (loop q (filter pair? (map cdr nums))))))))

(define (number->list n)
  (if (zero? n) '()
      (let-values ([(q r) (quotient/remainder n 10)])
        (cons r (number->list q)))))

(define 2^64 (number->list (expt 2 64)))
(for-each display (reverse (mult 2^64 2^64))) (newline)
;; for comparison
(* (expt 2 64) (expt 2 64))

;; Output:
;; 340282366920938463463374607431768211456
;; 340282366920938463463374607431768211456


  

You may also check:How to resolve the algorithm Arrays step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm OpenGL step by step in the C programming language
You may also check:How to resolve the algorithm Zsigmondy numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the VBScript programming language
You may also check:How to resolve the algorithm Convex hull step by step in the Julia programming language