emperor: (Default)
Add MemoryShare This Entry
posted by [personal profile] emperor at 11:38am on 27/12/2005
Consider a piece of code that does:

while(N>=g){
N=N-(g-1);
g=g-1;
}



ETA: I should mention (doh) that g>3 and N<=(g)(g-1)(g-2)/6

It seems to me that given N and g you ought to be able to just calculate the resulting values of N and g, without having to iterate.

Alas, I am too stupid to see how. Can anyone help?
There are 10 comments on this entry. (Reply.)
pm215: (Default)
posted by [personal profile] pm215 at 11:45am on 27/12/2005
Are there boundary conditions you aren't telling us about? For example, with (N=9,g=3) it doesn't terminate...
emperor: (Default)
posted by [personal profile] emperor at 12:17pm on 27/12/2005
err, oops. Have edited post to reflect this.
ext_57795: (Default)
posted by [identity profile] hmmm-tea.livejournal.com at 11:59am on 27/12/2005
But it'll explode as soon as g becomes 0...
emperor: (Default)
posted by [personal profile] emperor at 12:17pm on 27/12/2005
Yes, I forgot the boundary conditions.
ext_57795: (Default)
posted by [identity profile] hmmm-tea.livejournal.com at 12:28pm on 27/12/2005
In which case you just need to find the first n for which

N-ng+1/2n(n+1) < g-n

Then the expressions either side of the < are your new values for N and g respectively
simont: A picture of me in 2016 (Default)
posted by [personal profile] simont at 12:12pm on 27/12/2005
I'm going to denote the initial value of N by N0, and that of g by g0, and their values in subsequent iterations by Ni and gi.

So:

N1 = N0 - (g0-1).
N2 = N0 - (2g0-3).
N3 = N0 - (3g0-6).
N4 = N0 - (4g0-10).

And so on. In general, Ni is equal to N0, minus i times g0, plus the ith triangular number. Now the ith triangular number is equal to i(i+1)/2 (most easily seen when you put two side-i triangles together into an i by i+1 rectangle, although there are more "mathsy" proofs available if you prefer), which means that

Ni = N0 - ig0 + i(i+1)/2

Also gi = g0 - i, but that one really should have been obvious :-)

If you're interested in finding out when the loop terminates, you can subtract the above formulae to find Ni-gi, which will be a quadratic in i which you can solve in the normal way to find out when it crosses zero. As other commenters have pointed out, it may not cross zero at all: the i2 coefficient of the quadratic is always positive, meaning it has a minimum value which might also be positive depending on the values of N0 and g0. In this situation your original loop will never terminate.
emperor: (Default)
posted by [personal profile] emperor at 12:20pm on 27/12/2005
Ah, OK. Since writing this post, I'd got to the Ni equation (err, by a rather less elegant route), and was concluding that you'd get a quadratic that you could solve.
ext_57795: (Default)
posted by [identity profile] hmmm-tea.livejournal.com at 12:34pm on 27/12/2005
ooo

i = g -1 +/- sqrt(g^2-2N+1)

that simplifies quite nicely :-)
ext_57795: (Default)
posted by [identity profile] hmmm-tea.livejournal.com at 12:44pm on 27/12/2005
or even

i = g - 3/2 +/- sqrt (g^2 -g -2N +1)

if I don't try and lose 1/2 a g...
ext_243: (mandrill)
posted by [identity profile] xlerb.livejournal.com at 05:31pm on 27/12/2005
Collecting some thoughts in a confused mix of notation from several languages:

param N, g;
var x;
gf=g-x;
Nf=N-(T(g-1)-T(gf-1))where T(n) = n*(n+1)/2; /* call it a trapezoid number */
subject to: Nf<gf and Nf+gf >= gf+1.

Hm...

Nf' = Nf - gf = N - T(g-1) + T(gf) /* since T(n) = T(n-1) + n; not sure if that matters */
subject to: Nf'<0 and Nf' >= 1 - gf

So... find the largest value of gf such that T(gf) < T(g-1) - N — and there's probably a constant-time way to do it, since it's mostly a square root — and that's the final value of g, and compute Nf by the equation in the first blob. Unless I've made an off-by-one (or worse) error somewhere, which is entirely likely.

And now I'll go peek at the other comments.

October

SunMonTueWedThuFriSat
      1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
26
 
27
 
28
 
29
 
30
 
31