English
Experimente

Exponentiation great positiver Integerzahlen: Chandah-sûtra Algorithmus

 

p.specht

unfortunately plenty too slow, but as demonstration one 2200 years middle-aged Algorithmus thoroughly funktionsfähig:
Window Title "Chandah-sûtra Algorithmus for Exponentiation (india, ca. 200 v.Chr)"
' (CL) CopyLeft, demonstration-code by P.woodpecker, Wien
' Q: https://de.wikipedia.org/wiki/be%C3%A4re_Exponentiation
randomize
declare x$,n$,p$
rpt:
Cls rnd(8^8)
Print " Please only natural numbers > 0 prompt: "
Print "\n    Base:",:input x$
print "\n Exponent:",:input n$
print "\n Berechnung runs ";
p$=Chandah(x$,n$)
beep:locate %csrlin,1
print " Chandah-sutra_X^N the length ";len(p$);" = \n"
print p$
waitinput
goto "rpt"

proc Chandah :parameters x$,n$

    case (val(n$)=0) and (val(x$)=0):return "-1.unbestimmt"
    case val(x$)=0:return "0"
    ' n in Binärdarstellung wandeln
    ' res=1 : of left to right
    ' 0 becomes as instruction "Quadriere res" interprets,
    ' 1 as "Quadriere res and multipliziere Result with X "
    var b$=d2b(n$)
    var res$ = "1"
    var l&=len(b$)

    whileloop l&

        res$=imult(res$,res$)

        if mid$(b$,&Loop,1)="1"

            res$=imult(res$,x$)

        endif

        locate %csrlin,22:print int(100*&Loop/l&);"%";

    endwhile

    return res$

endproc

proc d2b :parameters m$

    declare b$,c%,a%,r%,a$,anz%
    b$=""
    again:
    anz%=len(m$):m$=trim$(m$):r%=0:a$=""

    whileloop len(m$):c%=10*r%+val(mid$(m$,&loop,1))

        a%=c%\2:a$=a$+st$(a%):r%=c%-2*a%:endwhile
        a$=a$+":":a$=translate $(a$,"0"," "):a$=trim$(a$)
        a$=translate $(a$," ","0"):a$=left$(a$,len(a$)-1)

        if anz%>0:if r%/2=int(r%/2):b$="0"+b$:else :b$="1"+b$:endif

            m$=a$:goto "nochmal":endif :return b$

        endproc

        proc imult :parameters md$,mr$'Bauernmultiplikation

            Declare sum$:Set("decimals",18)

            if md$>mr$:sum$=md$:md$=mr$:mr$=sum$:sum$="":endif

                sum$="":case val(right$(md$,1)) mod 2:sum$=mr$

                Repeat

                    md$ = Halbiere(md$)
                    mr$ = Verdopple(mr$)
                    case val(right$(md$,1)) mod 2 : sum$ = add(sum$,mr$)

                Until val(left$(md$,2))=0

                return sum$

            endproc

            proc Verdopple

                parameters m$
                declare c$,c%,a%,r%,a$,tm&,vz%,anz%,manually%
                manually%=1
                anz%=len(m$)
                tm&=&gettickcount
                m$=trim$(m$)

                if left$(m$,1)="-"

                    vz%=-1
                    m$=mid$(m$,2,len(m$)-1)

                endif

                a$=""
                r%=0

                whileloop len(m$),1,-1

                    c$ = mid$(m$,&loop,1)
                    c%=val(c$)
                    a%=2*c%+r%
                    r%=0

                    if a%>9

                        a%=a%-10
                        r%=1

                    endif

                    a$=st$(a%)+a$

                endwhile

                case r%=1:a$="1"+a$
                a$=a$+":"
                a$=translate $(a$,"0"," ")
                a$=trim$(a$)
                a$=translate $(a$," ","0")
                a$=left$(a$,len(a$)-1)
                case vz%= -1:a$="-"+a$
                return a$

            endproc

            proc Halbiere : parameters m$

                declare c$,c%,a%,r%,a$,tm&,vz%,anz%,manually%
                anz%=len(m$)
                tm&=&gettickcount
                m$=trim$(m$)

                if left$(m$,1)="-"

                    vz%= -1
                    m$=mid$(m$,2,len(m$)-1)

                endif

                r%=0
                a$=""

                whileloop len(m$)

                    c$ = mid$(m$,&loop,1)
                    c%=10*r%+val(c$)
                    a%=c%\2
                    a$=a$+st$(a%)
                    r%=c%-2*a%

                endwhile

                a$=a$+":"
                a$=translate $(a$,"0"," ")
                a$=trim$(a$)
                a$=translate $(a$," ","0")
                a$=left$(a$,len(a$)-1)
                case vz%= -1:a$="-"+a$
                return a$

            endproc

            proc add :parameters a$,b$

                declare s$,la&,lb&,l&,a&,b&,c&,s&,i&,si$,carry&,lc&
                a&=0:b&=0:s$="":S&=0:c&=0:carry&=0:lc&=0'last carry
                la&=len(A $):lb&=len(B$):l&=la&

                If lb&>la&:l&=lb&:A$=Mkstr$(" ",lb&-la&)+A$

                    Else :B$=Mkstr$(" ",la&-lb&)+B$:Endif

                    Whileloop 0,l&-1

                        i&=l&-&Loop
                        a&=val(Mid$(A$,i&,1))
                        b&=val(Mid$(B$,i&,1))
                        c&=carry&+a&+b&
                        s&=c& mod 10
                        carry&= c& \ 10
                        lc&=carry&
                        si$=st$(s&):s$=si$+s$

                    EndWhile

                    s$=if(lc&,st$(lc&)," ")+s$
                    s$=s$+":"
                    s$=translate $(s$,"0"," ")
                    s$=trim$(s$)
                    s$=translate $(s$," ","0")
                    s$=left$(s$,len(s$)-1)
                    return s$

                ENDPROC

 
Computer: Gerät, daß es in Mikrosekunden erlaubt, 50.000 Fehler zu machen, zB 'daß' statt 'das'...
05/10/21  
 



Zum Experiment


Topictitle, max. 100 characters.
 

Systemprofile:

no Systemprofil laid out. [anlegen]

XProfan:

 Posting  Font  Smilies  ▼ 

Please register circa a Posting To verfassen.
 

Topic-Options

495 Views

Themeninformationen

this Topic has 1 subscriber:

p.specht (1x)


Admins  |  AGB  |  Applications  |  Authors  |  Chat  |  Privacy Policy  |  Download  |  Entrance  |  Help  |  Merchantportal  |  Imprint  |  Mart  |  Interfaces  |  SDK  |  Services  |  Games  |  Search  |  Support

One proposition all XProfan, The there's!


My XProfan
Private Messages
Own Storage Forum
Topics-Remember-List
Own Posts
Own Topics
Clipboard
Log off
 Deutsch English Français Español Italia
Translations

Privacy Policy


we use Cookies only as Session-Cookies because of the technical necessity and with us there no Cookies of Drittanbietern.

If you here on our Website click or navigate, stimmst You ours registration of Information in our Cookies on XProfan.Net To.

further Information To our Cookies and moreover, How You The control above keep, find You in ours nachfolgenden Datenschutzerklärung.


all rightDatenschutzerklärung
i want none Cookie