So here is my first X11-Basic program which I got to work (more or less) as it should. I am not yet quite happy with it because I had to use workarounds for several bugs or missing features in X11-Basic, but I arrived finally at a working version.
This program calculates Pi, e, and square roots, to as many digits as you wish (if you have time to wait...), and lets you search for any numeric string - for example your phone number - within them.
(Note: The next post contains the program as an attached *.bas file. I did not find any option for attaching files to the first post of a thread.)
'"Where is your phone number in Pi?"'-----------------------------------'ProgrammingLanguage:X11-Basic''Demonstratessomeofthe"big integer"capacitiesofX11-Basic,'appliedalsotofractionalnumbers.'Maybeusedandextendedfreely.''Thisprogramletsyousearchforyourphonenumber(oranyothernumeric'string)inasmanydigitsofPiasyouwish.'InsteadofPiyoucanalsocalculateandsearche,thesquarerootof2,'or(almost)anyothersquareroot.''Theprogramwriteseverynumberitcalculatestoatextfile,'soyoucanlateropenoredititinatexteditor.'Thenamesofthesefilesare"bn_pi.txt","bn_e.txt",and"bn_sqrt2.txt".'Theseareinitializedto10000digitsthefirsttimetheprogramisrun.'Thentheycanbeextendedbymeansofthe"Recalculate"option.'Thefilesforothersquarerootsareaccordinglycallede.g."bn_sqrt7"for'thesquarerootof7,etc.''IMPORTANT:Thisprogramworksonlywhencompiledtobytecode,'butNOTintheX11-Basicinterpreter(atleastnotintheWindowsversion),'becauseofseveralbugsintheinterpreter.''Also,ifyouwishtocalculatemillionsofdigits,theinterpreter'wouldbefartooslow.Rememberthatyouwouldhavetocalculateabout'tenmilliondigitsifyouwanttohaveareasonablechancetofinda'seven-digitphonenumberwithinthem.'OnaPentiumIV2.66GHzrunningWindowsXP,calculating100,000digitsofPi'took37secondsforthebytecodefile.(Mostofthistimewasusedforconver-'tingthecalculatednumberintoastring.)Sincethecomputingtimeincreases'approximatelyproportionaltothesquareofthenumberofdigits,calculating'tenmilliondigitswouldtakeapproximately100hours.''Note:The"Blink"mechanismforthewaitscreencouldbeimplementedina'moreelegantwayusingEVERY,butIfoundthisfeaturetobestillunreliable.''-SomePRINTstatementswhichgivefeedbackaboutprogramprogressareleft'inthecode;thesemightbeannoyingwhenrunningunderAndroidwhereconsole'andgraphicsscreenareidentical,sojustcommentthemout.Ifandroid?baseDir$="/mnt/sdcard/bas/"ElsebaseDir$=""EndIfDimfixedNum$(5)'Dimfilename$()=[" ","bn_pi.txt","bn_e.txt","bn_sqrt2.txt"," "]'DimnumberName$()=[" ","Pi","e","Sqrt(2)"," "]'Abovelineswererejectedbythecompiler/interpreter.Dimfilename$(5)filename$(1)="bn_pi.txt"filename$(2)="bn_e.txt"filename$(3)="bn_sqrt2.txt"DimnumberName$(5)numberName$(1)="Pi"numberName$(2)="e"numberName$(3)="Sqrt(2)"searchNumber$=" 234"squareNumber$=" 3"waitX1%=0waitY1%=0scrsave$=" "openw1get_geometry1,scrx%,scry%,scrw%,scrh%textW=max(min(min(scrw%,scrh%)/30,16),8)linelength%=Int(scrw%/textW)Colorcolor_rgb(0,0,0)pbox0,0,scrw%-1,scrh%-1DimWaitText$(2)WaitText$(0)="Initializing Numbers"WaitText$(1)="Please Wait"ret=@OpenWaitScreen(WaitText$())Fori%=1To3Ifexist(baseDir$+filename$(i%))!LoadsPi,e,andsqrt(2)fromexistingfiles.fixedNum$(i%)=@MyBLoad$(baseDir$+filename$(i%))Else!InitializePi,e,andsqrt(2)to10000digits.Selecti%Case1fixedNum$(1)=@PiString$(10000)Case2fixedNum$(2)=@EString$(10000)Case3fixedNum$(3)=@SqrtString$(2,10000)endselectret%=@myBSave%(baseDir$+filename$(i%),fixedNum$(i%))EndIfNexti%ret=@CloseWaitScreen()DimwaitText$(3)'Startofmainloop:DostartAgain:'Dialogforchoosingnumber:'===========================txt$="Choose a number where to search:"btns$="Pi|e|sqrt(2)|sqrt(n)|EXIT"alert0,txt$,0,btns$,selection%ExitIfselection%=5Ifselection%=4!Arbitrarysquareroot!=====================txt$="Search square root of: "+Chr$(27)+squareNumber$alert0,txt$,0,"OK|Cancel",sele%,reTxt$Ifsele%=2GoTostartAgainEndIfsquareNumber$=word$(reTxt$,1,Chr$(13))sqN%=Val(Trim$(squareNumber$))IfsqN%<0sqN%=0EndIffilename$(4)="bn_sqrt"+Str$(sqN%)+".txt"numberName$(4)="Sqrt("+Str$(sqN%)+")"Ifexist(baseDir$+filename$(4))fixedNum$(4)=@MyBLoad$(baseDir$+filename$(4))Else!Initializesqrt(sqN%)to10000digits.WaitText$(0)="Recalculating "+numberName$(selection%)WaitText$(1)="to 10000 digits."WaitText$(2)="Please wait."ret=@OpenWaitScreen(WaitText$())fixedNum$(4)=@SqrtString$(sqN%,10000)ret=@CloseWaitScreen()ret%=@myBSave%(baseDir$+filename$(4),fixedNum$(4))EndIfEndIf'Search/RecalculateDialog:'==========================digits%=Len(fixedNum$(selection%))-InStr(fixedNum$(selection%),".")txt$=numberName$(selection%)+" is calculated with "txt$=txt$+Chr$(27)+Str$(digits%)+" |"txt$=txt$+"digits. (Edit to recalculate.)|Input your phone number|or any other number|(without spaces):"+Chr$(27)+searchNumber$btns$="Search|Recalculate|Cancel"alert0,txt$,0,btns$,sele2%,reTxt$'Printselection%;" - ";sele2%;" |";reTxt$;"|"newDigits%=Val(Trim$(word$(reTxt$,1,Chr$(13))))searchNumber$=word$(reTxt$,2,Chr$(13))'Note:weshouldnotTRIMsearchNumber$itself,'becauseweuseitasthenewdefaulttextinthenextALERT,'andALERTdoesnotallowtoinputastringlongerthanthedefaulttext.Selectsele2%Case1!Searchforanumber!===================posi%=InStr(fixedNum$(selection%),Trim$(searchNumber$))Colorcolor_rgb(1,1,0.4)Ifposi%ltext0,0,"Found "+Trim$(searchNumber$)+" in "+numberName$(selection%)ltext0,textW*2,"at position "+Str$(posi%-2)+":"ret=@PrintFoundNumber(fixedNum$(selection%))'Print"Found at ";posi%-2Elseltext0,0,Trim$(searchNumber$)+" was not found in "+Str$(Len(fixedNum$(selection%))-2)+" digits"ltext0,textW*2,"of "+numberName$(selection%)+"."'Print"Not found"EndIfltext0,textW*16,"Press any key"ltext0,textW*18,"or click mouse to continue."showpagex$=@WaitMouseOrKey$()Colorcolor_rgb(0,0,0)pbox0,0,scrw%-1,scrh%-1showpageCase2!Recalculateanumber!====================IfnewDigits%<=digits%txt$="New number of digits is not greater|than the current one.|"txt$=txt$+"Recalculating makes no sense."alert1,txt$,1,"OK",s%GoTostartAgainEndIfdigits%=newDigits%WaitText$(0)="Recalculating "+numberName$(selection%)WaitText$(1)="to "+Str$(digits%)+" digits."WaitText$(2)="Please wait."ret=@OpenWaitScreen(WaitText$())tt=TimerSelectselection%Case1fixedNum$(1)=@PiString$(digits%)Case2fixedNum$(2)=@EString$(digits%)Case3fixedNum$(3)=@SqrtString$(2,digits%)Case4fixedNum$(4)=@SqrtString$(sqN%,digits%)EndSelectPrint"Calculation ";selection%;" = ";Timer-tt;" seconds."ret%=@myBSave%(baseDir$+filename$(selection%),fixedNum$(selection%))ret=@CloseWaitScreen()Case3'Cancelled(nothingtodo)endselectLoopEndFunctionPrintFoundNumber(nString$)'Followingisaworkaroundforprintingtextwithbackgroundcolor,'whichdoesnotyetworkproperly.LocalstartString%,startX%,startString%=max(posi%-(lineLength%-Len(Trim$(searchNumber$)))/2,1)startX%=ltextlen(Mid$(nString$,startString%,posi%-startString%))Colorcolor_rgb(1,0,0)pboxstartX%,textW*4,startX%+ltextlen(Trim$(searchNumber$)),textW*6Colorcolor_rgb(1,1,0.4)ltext0,textW*4,Mid$(nString$,startString%,lineLength%)Return0EndFunctionFunctionMyBLoad$(file$)'WorkaroundbecauseofabugintheWindowsversion.Localf%,temp$,bytes%Ifexist(file$)bytes%=size(file$)temp$=Space$(bytes%)f%=FreeFile()Open"I",f%,file$bgetf%,VarPtr(temp$),bytes%Closef%Returntemp$ElseReturn""EndIfEndFunctionFunctionMyBSave%(file$,s$)'WorkaroundbecauseofabugintheWindowsversion.Localf%,bytes%bytes%=Len(s$)f%=FreeFile()Open"O",f%,file$bputf%,VarPtr(s$),bytes%Closef%Returnbytes%EndFunctionFunctionPiString$(digits%)Localexpon%,myPi&,denom&expon%=(digits%+26)/9!Usemoredigitsbecauseofroundingerrors.Locate1,1Print"Preparing denominator..."denom&=@bn_power&(1000000000,expon%)Locate1,1Print"Calculating Pi... "myPi&=@bnx_pi&(denom&)ret=@BlinkWaitScreen(waitText$())Locate1,1Print"Generating string... "ReturnLeft$(@bnx_radixPower$(myPi&,9*expon%,10),digits%+2)EndFunctionFunctionEString$(digits%)Localexpon%,denom&,e&expon%=(digits%+26)/9Locate1,1Print"Preparing denominator..."denom&=@bn_power&(1000000000,expon%)Locate1,1Print"Calculating e... "e&=@bnx_e&(denom&)Locate1,1Print"Generating string... "ReturnLeft$(@bnx_radixPower$(e&,9*expon%,10),digits%+2)EndFunctionFunctionSqrtString$(n%,digits%)Localexpon%,denom&,root&expon%=(digits%+17)/9denom&=@bn_power&(1000000000,expon%)Locate1,1Print"Calculating square root..."root&=sqrt(n%*denom&*denom&)Locate1,1Print"Generating string... "ReturnLeft$(@bnx_radixPower$(root&,9*expon%,10),digits%+4)EndFunctionFunctionbnx_pi&(denom&)'CalculatesPi(Gauss-LegendreAlgorithm),returnsnumerator.Locala&,olda&,b&,t&,x&a&=denom&b&=sqrt(denom&*denom&div2)t&=denom&div4x&=1Repeatolda&=a&a&=(a&+b&)div2ret=@BlinkWaitScreen(waitText$())b&=Sqrt(b&*olda&)Subolda&,a&ret=@BlinkWaitScreen(waitText$())t&=t&-((x&*olda&*olda&)divdenom&)Addx&,x&ret=@BlinkWaitScreen(waitText$())Untilolda&=0Return(a&*a&)divt&EndFunctionFunctionbnx_e&(denom&)'Returnvalue/denom&=e.Localdivisor&,diff&,num&num&=denom&diff&=denom&divisor&=1repeatdivdiff&,divisor&incdivisor&Addnum&,diff&ret=@BlinkWaitScreen(waitText$())Untildiff&=0Returnnum&EndFunctionFunctionbn_power&(a&,b%)'(Returns1ifb%negative-notquitelogical...)Localc&,i%c&=1Fori%=1Tob%c&=c&*a&ret=@BlinkWaitScreen(waitText$())Nexti%Returnc&EndFunctionFunctionbn_radix$(a&,base%)'forarbitraryprecisionintegersLocaltemp2$,sign$Ifbase%>64Orbase%<2Return""EndIfsign$=""!Initializelocalvariablestemp2$=""Ifa&<0Thensign$="-"a&=-a&EndIfrepeattemp2$=@bn_makedigit$(a&Modbase%)+temp2$diva&,base%Untila&=0Returnsign$+temp2$EndFunctionFunctionbnx_radix$(a&,b&,base%,maxlen%)'Returnsa&/b&inbasebase%,max.maxlen%digits.'Isactuallynotusedbythisprogramsincewecanusethefunction'bnx_radixPower$()below,whichisabouttwiceasfast.Localtemp$,n&Ifb&=0Return""EndIftemp$=""IfSgn(a&)<>Sgn(b&)temp$="-"EndIfa&=Abs(a&)b&=Abs(b&)n&=a&divb&!Integerparttemp$=temp$+@bn_radix$(n&,base%)+"."Iftemp$="."Return""!Invalidbase%endifn&=a&Modb&!Residuerepeatmuln&,base%temp$=temp$+@bn_makedigit$(n&divb&)n&=n&Modb&ret=@BlinkWaitScreen(waitText$())Until(Len(temp$)>=maxlen%)Or(n&=0)Returntemp$EndFunctionFunctionbnx_radixPower$(a&,b%,base%)'Returnsa&/(base%^b%)inbasebase%,b%"decimal"digits.Localtemp$,sign&,i%Ifbase%>64Orbase%<2!InvalidbaseReturn""EndIfsign$=""!Initializelocalvariablestemp$=""Ifa&<0Thensign$="-"a&=-a&EndIfFori%=1Tob%temp$=@bn_makedigit$(a&Modbase%)+temp$diva&,base%ret=@BlinkWaitScreen(waitText$())Nexti%temp$="."+temp$repeattemp$=@bn_makedigit$(a&Modbase%)+temp$diva&,base%Untila&=0Returnsign$+temp$EndFunctionFunctionbn_makedigit$(dig&)'"else..if"mustbenested,becauseofabugintheWindowsversion.'dig&couldbeanormalintegeriftheresultofabigintegerdivision'orofbigintegerMODcouldbeassignedtoanormalinteger.'Inthebytecodefilethisworks,buttheinterpreteryieldswrongresults'whendoingthis;soIdecidedtouseabigintegerhere.'However,intheWindowsversion,theinterpreternowcrasheswhenusingthisfunction!Localdigi%'Print"Entered MakeDigit."digi%=dig&If(digi%<10)ReturnChr$(digi%+48)ElseIf(digi%<36)ReturnChr$(digi%+55)ElseIf(digi%<62)ReturnChr$(digi%+61)ElseIf(digi%=62)Return"@"ElseIf(digi%=63)Return"$"ElseReturn"!"EndIfEndIfEndIfEndIfEndIfEndFunctionFunctionOpenWaitScreen(t$())LocalmaxLen%,i%,y%deftext0,textW/100,textW/75,0maxLen%=0Fori%=0ToDim?(t$())-1maxLen%=max(ltextlen(t$(i)),maxLen%)Nexti%waitW%=maxLen%+2*textWwaitH%=(Dim?(t$())+1)*textW*2waitX1%=(scrw%-waitW%)/2waitY1%=(scrh%-waitH%)/2Colorcolor_rgb(0.3,0.3,0.3)pboxwaitX1%,waitY1%,waitX1%+waitW%-1,waitY1%+waitH%-1Colorcolor_rgb(1,1,0.4)y%=waitY1%+textWFori%=0ToDim?(t$())-1ltextwaitX1%+textW,y%,t$(i%)Addy%,(textW*2)Nexti%showpagewaitT=TimerReturn0EndFunctionFunctionBlinkWaitScreen(t$())Locali%,y%IfTimer<waitT+1Return0EndIfwaitT=Timerblink%=blink%Xor1Ifblink%Colorcolor_rgb(0.3,0.7,0.7)ElseColorcolor_rgb(0.3,0.3,0.3)EndIfpboxwaitX1%,waitY1%,waitX1%+waitW%-1,waitY1%+waitH%-1Colorcolor_rgb(1,1,0.4)y%=waitY1%+textWFori%=0ToDim?(t$())-1ltextwaitX1%+textW,y%,t$(i%)Addy%,(textW*2)Nexti%showpageReturn1EndFunctionFunctionCloseWaitScreen()Colorcolor_rgb(0,0,0)pboxwaitX1%,waitY1%,waitX1%+waitW%-1,waitY1%+waitH%-1showpageReturn0EndFunctionFunctionWaitMouseOrKey$()WhileInKey$<>""Wendrepeatm$=InKey$Until(m$<>"")Or((mousekAnd7)<>0)Returnm$EndFunction
Last edit: Wanderer 2015-04-12
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here the program attached as a *.bas file.
(Funny thing in this forum: There is no option for attaching a file to the first post of a thread, but the following posts support attachments...)
So here is my first X11-Basic program which I got to work (more or less) as it should. I am not yet quite happy with it because I had to use workarounds for several bugs or missing features in X11-Basic, but I arrived finally at a working version.
This program calculates Pi, e, and square roots, to as many digits as you wish (if you have time to wait...), and lets you search for any numeric string - for example your phone number - within them.
(Note: The next post contains the program as an attached *.bas file. I did not find any option for attaching files to the first post of a thread.)
Last edit: Wanderer 2015-04-12
Here the program attached as a *.bas file.
(Funny thing in this forum: There is no option for attaching a file to the first post of a thread, but the following posts support attachments...)