Wednesday, July 5, 2023

LISP and Intelligent Thinking

My 1981 book  LISP (ISBN 0-201-08329-9) taught me about recursive programming, and here below is an example (tower-of-hanoi), which I programmed into my Sinclair Spectrum 48k in about 1986, since when I have taken a great deal of interest in Artificial Intelligence, which was what LISP was designed as a Symbol Manipulation Language to do. There is an example in the book of visual recognition used in Industry, where Engineers have been able to shift one type of package from one route to another, thus avoiding mixing of many different types of package, and segregating them more usefully - like the Post Office used to do, before Royal Mail started up. Below is a page from that LISP book, showing one object that could be sorted on a conveyor belt of goods:


and the book shows several short LISP programmes for determining the Area (15 lines of code), Centre of Area (24), Orientation (of least inertia, 36), Perimeter Length (20), and Euler number (identifies whether holes exist in image, 22) for any image, not just this one, and thus to identify the object, whether it be a banana, a tomato, a mushroom, a hammer, a screwdriver, a small or a big box, a silicon chip, an address,.......

That LISP book also taught me about how I think, for it chose to display search trees on page 138 and I adopted diagram (c) which I show here below: I think Depth First - which is my (and Engineering's) normal pattern. That is from  Requirement A to think down design routes B C D and then B E and to ensure we have thought of everything, we would go down F G H and then F G J thus to complete a full in depth design alternatives with development timescales, resources and man-hours costs. This diagram assumes I have presented ALL of the alternatives of course! Commercial Department would translate this into £ notes required, and put their contract around our Definition Submission - like the MilSpec we did for the Tornado Engine, the RB199 in 1969.

Note that Politicians will normally be asked to remember many Policies, so will go on a Breadth First route for they cannot know the Depth First route - hence they will remember D H J perhaps, but will have no knowledge, or even idea, of how it got there, whilst I might remember the most important route, but not necessarily all the alternatives unless I take some time to explore my memory by logically remembering the evaluation.


 

This RECURSIVE programme (tower-of-hanoi) certainly works, as follows, from design to execution:

*(DEFUN tower-of-hanoi (N) (transfer   'A    'B   'C   N))

                                           ; N discs on A first, smaller disc always on top of bigger

                                           ; and throughout never have a bigger disc on top of a smaller disc

                                           ; the aim is to move all discs in order from column A to B

                                           ; this is a comment and takes no part in the programme

*(DEFUN move-disc (FROM TO)

                (LIST  (LIST   'move  'disc   'from   FROM   'to   TO))) 

*(DEFUN transfer (FROM    TO     SPARE   NUMBER)

                 (COND  ((EQUAL   NUMBER    1) (move-disc  FROM    TO)

                       (T  (APPEND  (transfer     FROM    SPARE   TO  (SUB1  NUMBER))  ;recursive

                                 (move-disc   FROM   TO)

                                 (transfer     SPARE    TO    FROM  (SUB1  NUMBER))))))           ;recursive

                                            ;The software will expect this type of instruction from the keyboard:

*(tower-of-hanoi 3)              ;and then will print:

((move disc from A to B)(move disc from A to C)(move disc from B to C)(move disc from A to B)*(move disc from C to A)(move disc from C to B)(move disc from A  to B))

                                            ;or, given a new move-disc function:

*(DEFUN move-disc (FROM TO)       

                (LIST     FROM     TO)) 

                                            ;the keyboard instruction: 

*(tower-of-hanoi 5)              ;will give column-column:

(AB AC BC AB CA CB AB AC BC BA CA BC AB AC BC AB CA CB AB CA BC BA CA CB AB AC BC AB CA CB AB)  

                   ; where the column of discs on A has now been entirely and correctly moved to B


*(TRACE transfer)                   ; this will trace each use of the function transfer

*(TRACE move-disc)                      ; this will trace each use of the function move, then:

*(tower-of-hanoi 3)                   ;is traced as follows:

0: (transfer A B C 3)

    1: (transfer A C B 2)

        2: (transfer A B C 1)

                3; (move-disc A B)

                3: (move-disc returned (AB))

        2: (transfer returned (AB))

        2: (move-disc A C)

        2: (move-disc returned (AC))

        2: (transfer B C A 1)

                3: (move-disc B C)

                3: (move-disc returned (BC))

        2: (transfer returned (BC))

    1: (transfer returned (AB AC BC))

    1: (move-disc A B)

    1: (move-disc returned (AB)

    1: (transfer C B A 2)

        2: (transfer C A B 1)

                3: (move-disc C A)

                3: (move-disc returned (CA))

        2: (transfer returned (CA))

        2: (move-disc C B)

        2: (move-disc returned (CB))

        2: (transfer A B C 1)

                3: (move-disc A B)

                3: (move-disc returned (AB))

        2: (transfer returned (AB))

    1: (transfer returned (CA CB AB))

0: (transfer returned (AB AC BC AB CA CB AB))

(AB AC BC AB CA CB AB)

*


Sunday, July 2, 2023

Software multiple digit fun

This is      https://jim-quinn8.blogspot.com

See also  https://jim-quinn4.blogspot.com 


The value of the natural logarithm e with 110 e terms in my Long Number Calculator with just 1510 digits for the calculation (the assigned limit to speed it up!) inside the

 .exe file (which took only 2 minutes 6 seconds on my desktop!) is:

2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738          this has 159 accurate digits

while

The value of the natural logarithm e with 2570 digits after the decimal point and it is accurate upon reading, and as a result of my Visual Basic Version 2.5 Long Number calculation, with 1100 e-terms is:

 1100 e terms v2.5 with an accurate 2570 digits after decimal point:

2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206955170276183860626133138458300075204493382656029760673711320070932870912744374704723069697720931014169283681902551510865746377211125238978442505695369677078544996996794686445490598793163688923009879312773617821542499922957635148220826989519366803318252886939849646510582093923982948879332036250944311730123819706841614039701983767932068328237646480429531180232878250981945581530175671736133206981125099618188159304169035159888851934580727386673858942287922849989208680582574927961048419844436346324496848756023362482704197862320900216099023530436994184914631409343173814364054625315209618369088870701676839642437814059271456354906130310720851038375051011574770417189861068739696552126715468895703503540212340784981933432106817012100562788023519303322474501585390473041995777709350366041699732972508868769664035557071622684471625607988265178713419512466520103059212366771943252786753985589448969709640975459185695638023637016211204774272283648961342251644507818244235294863637214174023889344124796357437026375529444833799801612549227850925778256209262264832627793338656648162772516401910590049164499828931505660472580277863186415519565324425869829469593080191529872117255634754639644791014590409058629849679128740687050489585867174798546677575732056812884592054133405392200011378630094556068816674001698420558040336379537645203040243225661352783695117788386387443966253224985065499588623428189970773327617178392803494650143455889707194258639877275471096295374152111513683506275260232648472870392076431005958411661205452970302364725492966693811513732275364509888903136020572481765851180630364428123149655070475102544650117272115551948668508003685322818315219600373562527944951582841882947876108526398139559900673764829224437528718462457803619298197139914756448826260390338144182326251509748279877799643730899703888677822713836057729788241256119071766394650706330452795466185509666618566470971134447401607046262156807174818778443714369882185596709591025968620023537185887485696522000503117343920732113908032936344797273559552773490717837934216370120500545132638354400018632399149070547977805669785335804896690629511943247309958765523681285904138324116072260299833053537087613893963917795745401613722361878936526053815584158718692553860616477983402543512843961294603529133259


1100 terms ends in: 133259428

1000 terms ends in: 133259179

The equation is    e = 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + 1/6! +.............+ 1/1100!

The screenshot below shows the first 160 digits - you have to copy to the right to obtain all digits, for there are actually 7010 in there, but as you have seen only 2570 count!

It took 7 hours 25 minutes to perform this calculation on my Windows XP desktop computer:

Intel(R) Core(TM)2 CPU     6300@1.86GHz      1.58GHz    1.97GB RAM

Physical Address Extension

Microsoft Windows XP Professional   Version 2002   Service Pack 3

Jim



and bigger to see better:








And for fun, this is the square root of 3 to 7010 digits from my Visual Basic long number calculator.exe:

1.732050807568877293527446341505872366942805253810380628055806979451933016908800037081146186757248575675626141415406703029969945094998952478811655512094373648528093231902305582067974820101084674923265015312343266903322886650672254668921837971227047131660367861588019049986537379859389467650347506576050756618348129606100947602187190325083145829523959832997789824508288714463832917347224163984587855397667958063818353666110843173780894378316102088305524901670023520711144288695990956365797087168498072899493296484283020786408603988738697537582317317831395992983007838702877053913369563312103707264019249106768231199288375641141422016742752102372994270831059898459475987664288897796147837958390228854852903576033852808064381972344661059689722872865264153822664698420021195484155278441181286534507035191650016689294415480846071277143999762926834629577438361895110127148638746976545982451788550975379013880664961911962222957110555242923723192197738262561631468842032853716682938649611917049738836395495938145757671853373633125910899655424624834787197605235997769192323570220305302840385915414971072429559206706202509520175963185872766359975283663431080150665853710647328538625922260582220510403680270297504798728079461658100417052681940019095733462175943893670249320422691034369812463720111185261084268910299720311202100063507176374582405203847555197279933797614906107894985544223326004018851303631561144886847281589288163245187265066645384877599162576642872111240842068016763517100102943180715515190961642460907039408129216903517492961364004139670431041253632327030922577327960292376597745537095469115742140424230781992327617401906424512454877516862696105333694216213605394604245654140128533007813633449856736406703977342229811961042925534501601405940479547154534548407271737656262366549166640233006013265744070107836858468452313160467754480500402240639911970362218602920238867150711017169400296868759663500040895316214233425227956834067013470185902028360716762147743493449563595808082130442586469468522610908263353008756612603460672195404055984128912975994810000772057440230047673258800091514371489475444879157191294659083570873961515537797640262068370848046072969382719585689759759626104159152657577790782334980567840022901532052138935373775536566427046826874289963441395743666073744445583086477893212985302148197395341478170516614952551763291993699565744522639112519093541386989366817430938226424736926202072990967831154131946484377915459915923928287714695149274026409213645654041644581490201945749419305269002613972646081065071439603206077510594187798284793986195249964165213139715293599421897416647075187235788629466108560170428869605798394052906407430811833388677881562635867156008396760245349229943938867059754315442943430957258470988215463111260766774067864571578060647447499750354544559313286549189849336572747626297414738235686914837831363361283627903824840163806671607179848728555842931349226093240565957553651136754644387834283313466644554180390821898983294626345016171122016929619460169320621033039744866231656003566781813443100896568706742965658177445572685015631594412507361198063472886466241327193669426712951450397518928313486192841303255188431169014671423794374728334344652482043796059732870616274910941596862625302558889768470417502003140262284691048046984032437906606737749627102512710602060862662843666050192890893838320006091228691881836842361693903523126930368613796811526946997891926635284389927016519825502252923579366281634568108913074112678378199449772765448294920438801301783546499213574032791577507786002296423504275996997626918738414847394773851563552091220963315839034567189415294357040653076085290174249768185502885276344949577449110422502453723156063618103504871759158032791798367510067136100066217566049573766777112925287302479926720158996418937762016114118289899682955902511656391422364995062850030696812973507251727918598301115277542298851885311763594786003123063726098369850308310852145595748359555715739356633680864821078791247051031631569581604348748829614894154585355561974999296877955099285527423590251810323766861609592552544319954907420679739157072614245328950428097351037967736660135917522627061232312589450697634813177754456066603576220483006904225389494867729521325719769245927670660765816558298675056572894658860919029644381285057859759437506402446062121456085608545249251368328754843438733720201692311630432705648060137587936856721535087816844078023292064623163811291772297752725531075161383682134604577799854625622081960631636019206330968516216231660683468863116621501323854287391304495123155216365621295187475530976570639955029880425647340557785191349533039888643071431785052007986242468481382976340802176297056457213162198306829364628682222279923816416353172598273213079714192676286915681181839966515100307010826703128664015918312719604038923540682591504595764918368552614803465109289896559961829716408498135702922729622140263750595519947501618105854474019764971593766342388569701982214045410209642908388248860987218876494800835944491934478931331080777740838035601892228537110093898091204853232990455598524096889180504497043838415045586150130902907091306156469685958969228276473764261083791700284689036668670497679170399954436330425855797794077269935288950837827114357125780631282729349883385045927110609526062879059715054945907279841681751687041505072957723365201003263831478479410794607656506369332663038206021719551471952041208479627248539111589302866641079696991524811276979149995102660574531123881393934919708926940735738491944291963007280950338654302137812339456142472424586825005490509782168419895154077663046648660253211821517058546481699010351020406040779053482395457081103662802419307833539518597508847118544793150345001580384709447854602092161934402798458912472052941776762506497961858234776556280824940544437650597779439080197924300154959936710407983913033119996866535017872308995874345981050410546853618716669008602651872709506307892209449850428619343629495429310972099408595585896796000425822959426943909574110166784798090792890754084338798997585569009471963248904323366036372887420477179897787319674789334640099238795928413280479076582258181183540591455395810029729132679714194354057353161415847980294411862031322519054091589019122255319653737392856310390787757920503439443610560728204865743914647280480723365927320176656223005701114685591214450995505364985643365509525473974996502828824922856959093188033794992264659862068823401174394496293131985985880254199347096201038949171977062258657266707618728353902334806925083435649537445628310757587186877538159397085400698899220166054425026312832880789794907045544329548492870187596100762027461311058672024649539970560477439601070677420574723587570759024726518532288910223630374462004719642400758541636765726892037163539219477428690685888815809704248358215149135699281075072307826447406718734809484544537264931062922580337258517145144805462076646086736427250483015812043421751869316708560008335295934426745872998735071625170300571157138175301302407011029660250955862422560289064674131908000682128984594


which was then squared to go back to "nearly" 3 !!

The Square of the Square Root of 3 to 7010 digits:

2.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

Note that if you enter -1 into the number 1 slot, and square it you will see 100 as the answer - so there are a couple of unrectified snags on the very simple stuff, but the big stuff works well.

And here is the Visual Basic long number calculator v2.5 programme - just a part:

Public Class fclsLongNumCalc

Inherits System.Windows.Forms.Form

Public Const maxdigits As Integer = 7010

Public Const limit As Integer = 7055

Public Const Pi As String = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631"

Public Const e_ As String = "2.71828182845904523536028747"

Public Const c_km_per_sec As String = "299792.458"

' Throughout I have used the arrays starting at 1,

' to avoid confusion with the text start point.

Public intNum1(limit + 1) As Integer ' Number1

Public intNum2(limit + 1) As Integer ' Number2

Public intAnswer(limit + 1) As Integer ' Answer

Public lenNum1 As Integer ' length of number1

Public lenNum2 As Integer ' length of number2

Public length As Integer ' length of answer

Public dp1posn As Integer ' dpposn points to Number1 array position after dp

Public dp2posn As Integer ' dpposn points to Number2 array position after dp

Public dpAposn As Integer ' dpposn points to Answer array position after dp

Public failure As String ' failure is "0" for success, "1" or "2" otherwise


Public sign1 As Integer ' sign will be -1 if number is negative

Public sign2 As Integer ' and sign = 0 if number is positive

Public signA As Integer


etc etc etc


Private Sub btn_e_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_e.Click

Call WarningText(0, "0") ' Reset warning

Call calculate_e()

Call AnswerText() ' e is the result

End Sub



Private Sub calculate_e() ' Calculates e - the base of natural logs


Dim PrevTerm(limit + 1) As Integer ' Previous term of the series

Dim PrevSum(limit + 1) As Integer ' Previous sum of the series

Dim counter As Integer

Dim counter2 As Integer

Dim dpPrevTerm As Integer ' DP of previous term

Dim lenPrevTerm As Integer ' Length of previous term

Dim dpPrevSum As Integer ' DP of previous sum

Dim lenPrevSum As Integer ' Length of previous sum

Dim strN2 As String

Dim lenN2 As Integer


PrevTerm(1) = 1 ' Set values of the first term to start

dpPrevTerm = 2 ' in e = 1+1/1!+1/2!+1/3!+.....+1/n!+...

lenPrevTerm = 1

PrevSum(1) = 1

dpPrevSum = 2

lenPrevSum = 1

sign1 = 0

sign2 = 0


For counter2 = 1 To 1100 ' 1100 terms required for 2000+ digits


    For counter = 1 To limit + 1 'Transfer prevterm to n1 array

       intNum1(counter) = PrevTerm(counter)

    Next counter


dp1posn = dpPrevTerm

lenNum1 = lenPrevTerm

strN2 = Trim(Str(counter2)) ' Now n2 array = counter

lenNum2 = Len(strN2)


Call transfer(lenNum2, strN2, failure, dp2posn, intNum2, sign2)


Call Divide() ' Divide n1 by n2 to obtain next term in series


       For counter = 1 To limit + 1 ' Transfer asnwer to n1 and prevterm

           intNum1(counter) = intAnswer(counter)

           PrevTerm(counter) = intAnswer(counter)

       Next counter


dp1posn = dpAposn

dpPrevTerm = dpAposn

lenNum1 = length

lenPrevTerm = length

        For counter = 1 To limit + 1 ' transfer prevsum to n2

            intNum2(counter) = PrevSum(counter)

        Next counter

dp2posn = dpPrevSum

lenNum2 = lenPrevSum


Call Padding() ' Add n1 and n2 to obtain new sum of series

Call Adding()


         For counter = 1 To limit + 1 ' Transfer sum to previous sum array

             PrevSum(counter) = intAnswer(counter) ' gives e on buildup

         Next counter


dpPrevSum = dpAposn

lenPrevSum = length


Next counter2 ' Now loop back for next term


End Sub


Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click

Dim sMessage As String


sMessage = "Long Number Calculator" & ControlChars.CrLf & _

"Designed by Jim Quinn BSc CEng(1985) FIMechE - I also have C&G C++, but I prefer Visual Basic for I learnt that on my Sinclair Soectrum 48k in the 1980's!" & ControlChars.CrLf & _

"Version 2.5, 1100 e terms, June 2023"

MessageBox.Show(sMessage, "About", MessageBoxButtons.OK, MessageBoxIcon.Information)


End Sub