Can anyone tell me if I'm dong this correctly.

S(10) = 1; S(n) = n + S(n + 1) for 0 < n < 10

I'm looking for the value of S(7)

S(10) = 1
S(9) = 9 + S(9 + 1) = 10
S(8) = 8 + S(8 + 1) = 18
S(7) = 7 + S(7 + 1) = 25

S(7) = 25

Dani AI

Generated

Good concise work by and the confirmation from . The recurrence you used is exactly the kind that unrolls into a finite arithmetic sum; that observation leads to a simple closed form and a safe, O(1) computation for any start index.

For a general setup with S(N) = a and S(n) = n + S(n+1) for integer n < N, the unrolling gives
S(k) = a + sum_{i=k}^{N-1} i.
Evaluate that arithmetic sum to obtain the closed form
S(k) = a + (k + N - 1) * (N - k) / 2.
A short inductive check verifies the formula: the base case k = N yields a, and substituting the expression for k+1 reproduces the expression for k.

Practical notes: use the closed form in code or compute iteratively from the top down to avoid recursion overhead in languages with small recursion limits. The formula above is exact for integer indices in the specified range; if you need to generalize to other domains, treat the recurrence and base case explicitly. Example implementation (Python) follows.

def S(k, N, a):
    # S(k,N,a) returns S(k) when S(N)=a and S(n)=n+S(n+1) for n < N
    return a + (k + N - 1) * (N - k) // 2

Recommended Answers

All 2 Replies

Yeah, that's correct.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.