:firstnz(list)
:1+sum(floor(1/(1+cumSum(abs(list))))
This function finds the first non-zero element in a list, and returns its index.
I found this routine in this site. I made some test runs with this function and with my zero searching solution:
1->n
While lck1[n]=0
n+1->n
EndWhile
The elapsed time was measured with the startTmr() and checkTmr() commands (OS 2.09 in the 92+) and get the result in seconds.
Program modifications:
Prgm
Local n,m,lck1,stp,size,t
startTmr()->t
...
...
EndFor
checkTmr(t)->t
Disp t
EndPrgm
Though the firstnz() function is an elegant solution, the simple While…EndWhile loop in this case is faster a bit, because the number of the zeros in the list isn't too much.
Test results:
Lucky(1000(items),16(iterations)): While= 106 sec, firstnz= 113 sec.
Lucky(2000,16): While= 258 sec, firstnz= 270 sec.
Of course at Lucky(1000,iter) the items number is approx. 500 because the prg beginning with the odds list.
The number of iterations:
In my previous program the number of iterations was the second parameter. (iter) So, the user must be to guesstimate it. The iter's value is correct when tle last two list's size is equal, or the list's size is less than the step. I tried to check it from the program:
lucky2(element)
Prgm
Local n,m,lck1,stp,size
seq(n,n,1,element,2)->lck1
newMat(1,1)->mres
ClrIo
Disp string(lck1)
2->m © Loop variable.
Loop
dim(lck1)->size
lck1[m]->stp
If stp>size:Exit
seq(when(mod(n,stp)≠0,lck1[n],0),n,1,size)->lck1
SortA lck1
1->n
While lck1[n]=0
n+1->n
EndWhile
right(lck1,size-n+1)->lck1
Disp string(lck1)
m+1->m
EndLoop
string(lck1)->mres[1,1] © Only the final result is in the matrix.
EndPrgm
With this program if the number of elements is 1000, the number of needed iterations is 32 (+1) for the final result and the needed time is 178 sec. The row string(lck1)->mres[m,1] was removed from the loop because I got a Memory (Full) error message at approx. 90000 byte free RAM initial value.