Suppose there is number s=12 , now i want to make sequence with the element a1+a2+…..+an=12.
The criteria is as follows-
- n must be minimum.
- a1 and an must be 1;
- ai can differs a(i-1) by only 1,0 and -1.
for s=12 the result is 6.
So how to find the minimum value of n.
Algorithm for finding
nfrom givens:1.Find
q = FLOOR( SQRT(s-1) )2.Find
r = q^2 + q3.If
s <= rthenn = 2q, elsen = 2q + 1Example:
s = 12q = FLOOR( SQRT(12-1) ) = FLOOR(SQRT(11) = 3r = 3^2 + 3 = 1212 <= 12, thereforen = 2*3 = 6Example:
s = 160q = FLOOR( SQRT(160-1) ) = FLOOR(SQRT(159) = 12r = 12^2 + 12 = 156159 > 156, thereforen = 2*12 + 1 = 25and the 25-numbers sequence for