would like some comments on how to make it faster,
I guess one straight approach is to do away with strings, will be trying to port it into your code format in the coming week,
most of the code is self explantory, i return the remainder of the division via n2$
Code: Select all
Procedure.s Divide (myn1$,myn2$)
Protected sizemyn2=Len(myn2$)
Protected result
;this procedure divides myn1$ by myn2$ i.e myn1$/myn2$ and returns the quotient
result=Compare (myn1$,myn2$)
myn1$=n1$;we need to reassign these to the padded values
myn2$=n2$
Protected result$=""
;Protected carry=0
Protected sizemyn1=Len(myn1$)
Protected temp_divident$=""
Protected temp_divisor_multiple$=""
Protected quotient$=""
Protected previous_remainder$=Left(myn1$,sizemyn2)
Protected temp_result
;PrintN ("myn2$:"+myn2$)
For i= sizemyn2 To sizemyn1
If i=sizemyn2
temp_divident$=previous_remainder$
Else
;we start with the part of myn1$ as long as myn2$
temp_divident$=previous_remainder$+Mid(myn1$,i,1)
EndIf
;PrintN ("divident:"+temp_divident$)
temp_result=Compare(temp_divident$,myn2$)
If (temp_result=0)Or (temp_result=-1) ; if temp_divident$>=myn2$
For m=1 To 9
temp_divisor_multiple$=Multiply (myn2$,Str(m))
temp_result1=Compare(temp_divident$,temp_divisor_multiple$)
;PrintN ("m="+Str(m))
If temp_result1=1
m=m-1
;PrintN ("breaking..")
Break
EndIf
Next m
Else
m=0
EndIf ; temp_result=0 or temp_result=-1 ends here
If m>9
m=9
EndIf
;PrintN ("m="+Str(m))
temp_divisor_multiple$=Multiply (myn2$,Str(m))
quotient$=quotient$+Str(m)
previous_remainder$=Subtract(temp_divident$,temp_divisor_multiple$)
;myn1$=LSet("",i,"0")+Right(myn1$,sizemyn1-i)
;PrintN ("temp_divisor"+temp_divisor_multiple$)
;PrintN("quotient$="+quotient$+";previous_remainder:"+previous_remainder$)
Next i
n2$=previous_remainder$
result$=quotient$
;remove leading zeros
For i=1 To Len(result$)
If Mid(result$,i,1)="0" And (flag=0)
cnt=cnt+1
Else
Break
EndIf
Next i
result$=Right(result$,Len(result$)-cnt)
ProcedureReturn result$
EndProcedure
;compare function returns 0 if myn1>n2 and 1 if n2>n1
;it pads the smaller number with leading 0s
;it removes any leading 0s in the original input
;the numbers obtained after a compare are suitable for any mathematical operation now
;num1$="1689087077234463463463474574575057957235732325357997563937563839566393745749395475748386339394756383937336383836363635715723233331243"
;num2$="500057383736353638340584638384739393030846363838373636363763636363636363638393930393735392847569475"
OpenConsole()
;PrintN ("num1$=" +num1$ + ";num2$=" + num2$)
;result=Compare (num1$,num2$)
;Result$= Add(n1$,n2$)
;PrintN ("Result::"+Str(result))
;result$=Multiply(num1$,num2$)
;PrintN ("Multiply::"+result$)
;Input()
;num2$=num1$
;num1$=result$
;num1=Len(num1$)
;num2=Len(num2$)
;PrintN (num1$+"::"+num2$)
;result$=Add(num1$,num2$)
;PrintN ("addition::"+result$)
;PrintN (Str(Val("00100")))
;fact$="1"
;For i=1 To 100
;PrintN(fact$)
;fact$=Multiply(fact$,Str(i))
;Next i
;PrintN("100!="+fact$)
num2$="53530583058305830581913467"
num1$="800970970593757057055000683553746232356"
result$=Divide(num1$,num2$)
PrintN ("Division:"+result$)
remainder$=n2$
PrintN ("Remainder:"+remainder$)
result1$=Multiply(result$,num2$)
result2$=Add(result1$,remainder$)
PrintN ("Multiply:"+result2$)
Input()
CloseConsole()