First, I understand I was unclear when stating my own goal:
I try to create a closed form solution, without the use of
any +/ summation and the likes.
> tricnt&> 54+i.6
> 47.75 55.75 51.75 61 55.75 65
Thanks again for pointing out the strange results of my first attempt!
Your comment pushed me to rework my J code, step-by-step
from my paper'n'pencil maths. I discovered a few tricky
to detect (for me) coding mistakes. Here is my update
tricnt =: 3 : 0
peri =. y
zmax =. <.-:<:peri
zmin =. >:>.peri%3
zcnt =. 1+zmax-zmin
zlst =. zmin+i.zcnt
corr =. (2|zcnt) * _1^(1-2|peri-zmax)
(zcnt>0) * -: (corr%2) - zcnt * (peri + 3r2 * 1 - zmax + zmin)
)
Furthermore, it was a really interesting exercise to write
a (near-)proof all in J! (see below) What a thrill to be
able to numericaly test each step of a proof!
> Here's an algebraic solution:
>
> telz=: 3 :0
> zx=. <.-:y
> zm=. 1+<.y%3
> +/>.-:(1&{-y-+/) (,:<:)zm+i.1+zx-zm
> )
It seems that your solution does not respect the "zmax < -:y" asked by the OP.
Eg. triangle-count(6) should be zero.
Otherwise, I haven't analysed your solution yet but I'm astonished to see that
despite using two +/, the memory-usage ratio between your solution and mine
seems nearly constant on a large range of values (tested between 2^10 and
2^20). On the other hand I probably ought to test memory usage with extended
precision inputs.
Regards,
Olivier
---
tricnt =: 3 : 0
peri =. y
NB. zmax < -: peri
zmax =. <.-:<:peri
NB. zmin + (zmin-1) + (zmin-2) >: peri
NB. 3*(zmin-1) >: peri
NB. zmin >: 1+peri%3
zmin =. >:>.peri%3
zcnt =. 1+zmax-zmin
NB. zmax >: zmin
NB. zcnt > 0
NB. zmin <: z <: zmax
zlst =. zmin+i.zcnt
NB. ymax =. z-1
NB. ymin + (ymin-1) >: peri-z
NB. 2*ymin >: 1+peri-z
NB. ymin >: -: 1+peri-z
NB. ymin =. >.-:1+peri-z
NB. ycnt =. 1+ymax-ymin
NB. ycnt =. 1+(z-1)-(>.-:1+peri-z)
NB. (zcnt>0) * +/ 1 + (zlst-1) - >.-:1+peri-zlst
NB. (zcnt>0) * +/ zlst - >.-:1+peri-zlst
NB. (zcnt>0) * +/ zlst - -: (tmp+2|tmp=:1+peri-zlst)
NB. (zcnt>0) * -: +/ (3*zlst) - peri + 1 + 2|1+peri-zlst
NB. (zcnt>0) * -: +/ (3*zlst) - peri + 2 - 2|peri-zlst
NB. (zcnt>0) * -: (3r2*zcnt*(zmax+zmin)) - +/ peri + 2 - 2|peri-zlst
NB. (zcnt>0) * -: (3r2*zcnt*(zmax+zmin)) - (zcnt*peri) + +/ 2 - 2|peri-zlst
NB. (zcnt>0) * -: (3r2*zcnt*(zmax+zmin)) - (zcnt*peri) + (2*zcnt) - +/
2|peri-zlst
NB. sump =. 3 : '(zcnt>0) * +/ 2|peri-zlst'
NB. sump =. 3 : 0
NB. if. 0=2|zcnt do. sumtmp =. -: znct
NB. elseif. 0=2|peri-zmax do. sumtmp =. -: znct-1
NB. elseif. do. sumtmp =. -: znct+1
NB. end.
NB. (zcnt>0) * sumtmp
NB. )
NB. sump =. 3 : 0
NB. if. 0=2|zcnt do. corr =. 0
NB. elseif. 0=2|peri-zmax do. corr =. _1
NB. elseif. do. corr =. 1
NB. end.
NB. (zcnt>0) * -: zcnt+corr
NB. )
NB. sump =. 3 : 0
NB. if. 0=2|zcnt do. corr =. 0
NB. else. do. corr =. _1^(1-2|peri-zmax)
NB. end.
NB. (zcnt>0) * -: zcnt+corr
NB. )
NB. sump =. 3 : '(zcnt>0) * -: zcnt + (2|zcnt) * _1^(1-2|peri-zmax)
corr =. (2|zcnt) * _1^(1-2|peri-zmax)
NB. (zcnt>0) * -: (3r2*zcnt*(zmax+zmin)) - (zcnt*peri) + (2*zcnt) - -: zcnt +
corr
NB. (zcnt>0) * (corr%4) + -: (3r2*zcnt*(zmax+zmin)) - (zcnt*peri) + (2*zcnt) -
-: zcnt
NB. (zcnt>0) * (corr%4) + -: zcnt * ((3r2*(zmax+zmin)) - peri + 1.5)
NB. (zcnt>0) * -: (corr%2) + zcnt * ((3r2*(zmax+zmin)) - peri + 1.5)
NB. (zcnt>0) * -: (corr%2) - zcnt * (peri + 1.5 - 3r2 * zmax + zmin)
(zcnt>0) * -: (corr%2) - zcnt * (peri + 3r2 * 1 - zmax + zmin)
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm