How to resolve the algorithm Babbage problem step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Babbage problem step by step in the Common Lisp programming language

Table of Contents

Problem Statement

Charles Babbage, looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example: He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain.

The task is to find out if Babbage had the right answer — and to do so, as far as your language allows it, in code that Babbage himself would have been able to read and understand. As Babbage evidently solved the task with pencil and paper, a similar efficient solution is preferred. For these purposes, Charles Babbage may be taken to be an intelligent person, familiar with mathematics and with the idea of a computer; he has written the first drafts of simple computer programmes in tabular form. [Babbage Archive Series L].

The aim of the task is to write a program that is sufficiently clear and well-documented for such a person to be able to read it and be confident that it does indeed solve the specified problem.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Babbage problem step by step in the Common Lisp programming language

Source code in the common programming language

(defun babbage-test (n)
 "A generic function for any ending of a number"
  (when (> n 0)
    (do* ((i 0 (1+ i))
          (d (expt 10 (1+ (truncate (log n) (log 10))))) )
      ((= (mod (* i i) d) n) i) )))


; Project : Babbage problem

(setq n 1)
(setq bab2 1)
(loop while (/= bab2 269696)
    do (setq n (+ n 1))
         (setf bab1 (expt n 2))
         (setf bab2 (mod bab1 1000000)))
(format t "~a" "The smallest number whose square ends in 269696 is: ")
(write n)
(terpri)
(format t "~a" "Its square is: ")
(write (* n n))


;; * The package definition
(defpackage :babbage
  (:use :common-lisp))
(in-package :babbage)

;; * The function
(defun babbage (end)
  "Returns the smallest number whose square ends in END."
  (loop
     :with digits = (ceiling (log end 10))     ; How many digits has end?
     :for num :from (isqrt end)                ; The start number
     :for square = (expt num 2)                ; The square of num
     :for ends = (mod square (expt 10 digits)) ; The last digits
     :until (= ends end)
     :finally
       (format t "The smallest number whose square ends in ~D is: ~D~%" end num)
       (format t "Its square is: ~D~%" square)
       (return num)))


  

You may also check:How to resolve the algorithm Simple database step by step in the Ruby programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm XML/Input step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Mouse position step by step in the ERRE programming language