Now I learn about using RSet() and Bool(). A well=reasoned response as well. But I'm thinking along the lines of crowding the big increments towards the earlier, lower steps. Let me play with that notion a bit. This approach of yours clusters around a 2-2-2.5 approach and gets you to the thousands divisor stage in one step. But what if you were to turn it around and do it 2.5-2-2 instead? And then recluster all the 2.5's at the beginning? I will have to look at what that would do to the results. Meanwhile, here is a slight rework of your excellent code. I just prefer to have numbers with thousands separators.
Code: Select all
Define.l n = 500, m = 1000000, i, j
Define.s aa, bb
While n<=m
i + 1
aa = RSet("$"+StrF(n,2),20)
For j = Len(aa)-6 To 2 Step -4
bb = Mid(aa,j,1)
If bb =" " Or bb = "$"
Break
EndIf
aa = Mid(aa,2,j-1)+","+Mid(aa,j+1)
Next
Debug RSet(Str(i),4)+aa
n * (2 + 0.5 * Bool(Mod(i,3)=0))
Wend
What really surprised me though is having to use a Step -4 instead of Step -3. With other Basics, you find that the Len(aa) becomes fixated at the start of the For...Next statement, meaning it is treated like a constant. But with PureBasic, it looks a the current Len(aa), and you have to factor in the increased length by having a comma inserted at given intervals. If I wanted PureBasic to behave the same way, I might have to do this:
Code: Select all
hLen.l = Len(aa)-6
For j = hLen To 2 Step -3
...
Next
What's different is that Len(aa) is focused on working from the Right. Using hLen, I shift the focus to working from the Left.
What's odd though is that Len(aa)-6 or hLen are the initial values for the loop, not the termination limits. It's more understandable if they were the termination values, where you do the test. It's like PureBasic calibrates a new value for j from scratch for each cycle through, rather than just applying the +/- Step value to what j is currently set to. If true, this raises all sorts of possibilities for how a For...Next statement can be made to work, based on the initial For statement and how it is declared.
Now you can always modify the variable used as principal inside a For...Next loop, but that means accounting to the effect of the Step which is yet to be applied. If done as part of the For statement, the effect would be after the Step is applied. It also means the Step may not act as you expect it to, because of this difference in behavior. As in this case where I had to move to using -4 in place of -3.
While it gives prospect of doing something highly unusual with For...Next statements, it diminishes its value with respect to more stable results using Repeat or While statements in its place. With those, variables are controlled by the programmer in code.
has-been wanna-be (You may not agree with what I say, but it will make you think).