Calculating the sum of a handful of odd numbers in a specific sequence

Andrey

New member
Joined
May 15, 2020
Messages
4
Hello,

I am trying to create a custom object mesh directly in some C# code from scratch for a personal project of mine, and I've come across an issue that I can boil down to a simple maths problem as goes the following:

f(0) = (1 + 3 + 1) + (1)
f(1) = (1 + 3 + 5 + 3 + 1) + (1 + 3 + 1)
f(2) = (1 + 3 + 5 + 7 + 5 + 3 + 1) + (1 + 3 + 5 + 3 + 1)
f(3) = (1 + 3 + 5 + 7 + 9 + 7 + 5 + 3 + 1) + (1 + 3 + 5 + 7 + 5 + 3 + 1)

Or I've noticed an alternate form of the exact same problem:
f(0) = 3*1 + 3
f(1) = 4*1 + 3*3 + 5
f(2) = 4*1 + 4*3 + 3*5 + 7
f(3) = 4*1 + 4*3 + 4*5 + 3*7 + 9

Can anyone think of some formula that could help? I've tried looking up the sum of odd numbers and flipping it to get the following:
f(x) = (2^x + 1)^2 + 4^x

This formula works for the first half of those above formulas. But for some reason the second half is being elusive to me and I'm too sleep deprived from work to put more brain power into this at this point in time.

I'm certain it's probably a fairly simple formula involving the sum of odd numbers that I've just missed.

Any help would be greatly appreciated!

P.S. I wasn't quite sure where such a question would go, so I figured the intermediate level maths would be suitable? Feel free to correct me :)
 
Hello,

I am trying to create a custom object mesh directly in some C# code from scratch for a personal project of mine, and I've come across an issue that I can boil down to a simple maths problem as goes the following:

f(0) = (1 + 3 + 1) + (1)
f(1) = (1 + 3 + 5 + 3 + 1) + (1 + 3 + 1)
f(2) = (1 + 3 + 5 + 7 + 5 + 3 + 1) + (1 + 3 + 5 + 3 + 1)
f(3) = (1 + 3 + 5 + 7 + 9 + 7 + 5 + 3 + 1) + (1 + 3 + 5 + 7 + 5 + 3 + 1)

Or I've noticed an alternate form of the exact same problem:
f(0) = 3*1 + 3
f(1) = 4*1 + 3*3 + 5
f(2) = 4*1 + 4*3 + 3*5 + 7
f(3) = 4*1 + 4*3 + 4*5 + 3*7 + 9

Can anyone think of some formula that could help? I've tried looking up the sum of odd numbers and flipping it to get the following:
f(x) = (2^x + 1)^2 + 4^x

This formula works for the first half of those above formulas. But for some reason the second half is being elusive to me and I'm too sleep deprived from work to put more brain power into this at this point in time.

I'm certain it's probably a fairly simple formula involving the sum of odd numbers that I've just missed.

Any help would be greatly appreciated!

P.S. I wasn't quite sure where such a question would go, so I figured the intermediate level maths would be suitable? Feel free to correct me :)
Do you only care about the sum? Or do you also want to generate the addends l?
 
Looks like [imath]4(n+1)^2+2[/imath] to me.
Thankyou thankyou thankyou! I have absolutely no clue how that formula works or why, but it totally gives me the output sequence I'm looking for!

You are spot on. Thankyou so much for your assistance!
 
I have absolutely no clue how that formula works or why [...]

I think this is worth exploring. There are, of course, many ways to approach it (so don't @ me, all you actual-mathematicians out there). The purpose of this post is to demonstrate one way that this problem could be solved. The more tools you have in your toolbox, the better off you'll be in the future.

The way I interpret this problem is that there are three layers of functions at play. By deconstructing the final formula into its constituent parts, an algebraic expression can be rebuilt to solve the problem.
__________

The first function is to find the sum of a sequence of consecutive odd numbers given [imath]x[/imath] is the largest odd number in the sequence:

[imath]h(x) = 1 + 3 + 5 + ... + x[/imath]​

Consider the following sequence:

[imath]h(11) = 1 + 3 + 5 + 7 + 9 + 11[/imath]​

The key observation is the following:

[imath](1 + 11) + (3 + 9) + (5 + 7) = 12 + 12 + 12[/imath]​

By reversing the latter half of the sequence and producing pairs of numbers, the sum for any one of those individual pairs ([imath]12[/imath] in this example) is the same no matter which pair you're looking at. This behavior holds true no matter how many terms there are.

From this, we can see that the sum of the sequence looks like this:

[imath]h(x) = (x + 1) * NumberOfPairs[/imath]​

In this example, that makes [imath](11+1)*3=36[/imath], which you'll find is correct if you add up the six numbers manually. The number of pairs is of course half the number of terms, which still works even if there's an odd number of terms. If we wanted [imath]h(13)[/imath] (which contains seven terms), it works the same way: [imath](13+1)*\frac{7}{2}=49[/imath], which is the same as [imath]36+13[/imath].

The number of terms in such a sequence, given [imath]x[/imath], is [imath]\frac{x+1}{2}[/imath]. The reason for the division is that consecutive odd numbers differ by [imath]2[/imath], and since the first odd number is not itself a multiple of [imath]2[/imath], we need to add [imath]1[/imath] before dividing. The number of pairs, therefore, is half that at [imath]\frac{x+1}{4}[/imath].

The full formula for the sum of the sequence in the general case is this:

[imath]h(x) = (x+1) * \frac{x + 1}{4}[/imath]

[imath]h(x) = \frac{(x + 1)^2}{4}[/imath]​

It's worth mentioning that [imath]h(-1)=0[/imath].
__________

The second function is to find the sum of a sort of back-and-forth sequence of odd numbers where it starts at 1, increases to its largest value, then decreases back to 1. Something like this:

[imath]g(x) = 1 + 3 + 5 + ... + x + ... + 5 + 3 + 1[/imath]​

We already have a tool we can use to implement this: the [imath]h(x)[/imath] function that calculates the sum of the strictly-ascending sequence. Consider the following:

[imath]g(7) = 1 + 3 + 5 + 7 + 5 + 3 + 1[/imath]
[imath]h(7-2) = 1 + 3 + 5[/imath]
[imath]2h(7-2) = 1 + 3 + 5 + 5 + 3 + 1[/imath]
[imath]g(7) = 2h(7 - 2) + 7[/imath]​

Or in the general case:

[imath]g(x) = 2h(x - 2) + x[/imath]​
__________

The third function--the one we're interested in--is two of these back-and-forth sequences added together. Consider this requirement from the original post:

[imath]f(1) = (1 + 3 + 5 + 3 + 1) + (1 + 3 + 1)[/imath]​

We can express this in terms of [imath]g(x)[/imath]:

[imath]5 = 2((1) + 1) + 1[/imath]
[imath]3 = 2(1) + 1[/imath]
[imath]f(1) = g(2((1) + 1) + 1) + g(2(1) + 1)[/imath]​

Or in the general case:

[imath]f(x) = g(2(x + 1) + 1) + g(2x + 1)[/imath]​

From here, we can substitute the definition of [imath]g(x)[/imath] to flesh it out a bit:

[imath]f(x)= (2h(2(x + 1) + 1 - 2) + 2(x + 1) + 1) + (2h(2x + 1 - 2) + 2x + 1)[/imath]
[imath]f(x)= (2h(2x + 1) + 2x + 3) + (2h(2x - 1) + 2x + 1)[/imath]
[imath]f(x)= 2h(2x + 1) + 2h(2x - 1) + 4x + 4[/imath]
[imath]f(x)= 2(h(2x + 1) + h(2x - 1) + 2x + 2)[/imath]​

Then substitute the definition of [imath]h(x)[/imath]:

[imath]f(x)= 2\left(\frac{(2x + 1 + 1)^2}{4} + \frac{(2x - 1 + 1)^2}{4} + 2x + 2\right)[/imath]
[imath]f(x)= 2\left(\frac{4x^2+8x+4}{4} + \frac{4x^2}{4} + 2x + 2\right)[/imath]
[imath]f(x)= 2(x^2+2x+1 + x^2 + 2x + 2)[/imath]
[imath]f(x)= 2(2x^2+4x+3)[/imath]
[imath]f(x)= 4x^2+8x+6[/imath]
[imath]f(x)=[/imath] [imath](2x+2)^2 - 4[/imath] [imath]+6[/imath]
[imath]f(x)= 4(x+1)^2+2[/imath]​

And this is the same as @blamocur's answer, so I guess I did it right...
 
I think this is worth exploring. There are, of course, many ways to approach it (so don't @ me, all you actual-mathematicians out there). The purpose of this post is to demonstrate one way that this problem could be solved. The more tools you have in your toolbox, the better off you'll be in the future.

The way I interpret this problem is that there are three layers of functions at play. By deconstructing the final formula into its constituent parts, an algebraic expression can be rebuilt to solve the problem.
__________

The first function is to find the sum of a sequence of consecutive odd numbers given [imath]x[/imath] is the largest odd number in the sequence:

[imath]h(x) = 1 + 3 + 5 + ... + x[/imath]​

Consider the following sequence:

[imath]h(11) = 1 + 3 + 5 + 7 + 9 + 11[/imath]​

The key observation is the following:

[imath](1 + 11) + (3 + 9) + (5 + 7) = 12 + 12 + 12[/imath]​

By reversing the latter half of the sequence and producing pairs of numbers, the sum for any one of those individual pairs ([imath]12[/imath] in this example) is the same no matter which pair you're looking at. This behavior holds true no matter how many terms there are.

From this, we can see that the sum of the sequence looks like this:

[imath]h(x) = (x + 1) * NumberOfPairs[/imath]​

In this example, that makes [imath](11+1)*3=36[/imath], which you'll find is correct if you add up the six numbers manually. The number of pairs is of course half the number of terms, which still works even if there's an odd number of terms. If we wanted [imath]h(13)[/imath] (which contains seven terms), it works the same way: [imath](13+1)*\frac{7}{2}=49[/imath], which is the same as [imath]36+13[/imath].

The number of terms in such a sequence, given [imath]x[/imath], is [imath]\frac{x+1}{2}[/imath]. The reason for the division is that consecutive odd numbers differ by [imath]2[/imath], and since the first odd number is not itself a multiple of [imath]2[/imath], we need to add [imath]1[/imath] before dividing. The number of pairs, therefore, is half that at [imath]\frac{x+1}{4}[/imath].

The full formula for the sum of the sequence in the general case is this:

[imath]h(x) = (x+1) * \frac{x + 1}{4}[/imath]​
[imath]h(x) = \frac{(x + 1)^2}{4}[/imath]​

It's worth mentioning that [imath]h(-1)=0[/imath].
__________

The second function is to find the sum of a sort of back-and-forth sequence of odd numbers where it starts at 1, increases to its largest value, then decreases back to 1. Something like this:

[imath]g(x) = 1 + 3 + 5 + ... + x + ... + 5 + 3 + 1[/imath]​

We already have a tool we can use to implement this: the [imath]h(x)[/imath] function that calculates the sum of the strictly-ascending sequence. Consider the following:

[imath]g(7) = 1 + 3 + 5 + 7 + 5 + 3 + 1[/imath]​
[imath]h(7-2) = 1 + 3 + 5[/imath]​
[imath]2h(7-2) = 1 + 3 + 5 + 5 + 3 + 1[/imath]​
[imath]g(7) = 2h(7 - 2) + 7[/imath]​

Or in the general case:

[imath]g(x) = 2h(x - 2) + x[/imath]​
__________

The third function--the one we're interested in--is two of these back-and-forth sequences added together. Consider this requirement from the original post:

[imath]f(1) = (1 + 3 + 5 + 3 + 1) + (1 + 3 + 1)[/imath]​

We can express this in terms of [imath]g(x)[/imath]:

[imath]5 = 2((1) + 1) + 1[/imath]​
[imath]3 = 2(1) + 1[/imath]​
[imath]f(1) = g(2((1) + 1) + 1) + g(2(1) + 1)[/imath]​

Or in the general case:

[imath]f(x) = g(2(x + 1) + 1) + g(2x + 1)[/imath]​

From here, we can substitute the definition of [imath]g(x)[/imath] to flesh it out a bit:

[imath]f(x)= (2h(2(x + 1) + 1 - 2) + 2(x + 1) + 1) + (2h(2x + 1 - 2) + 2x + 1)[/imath]​
[imath]f(x)= (2h(2x + 1) + 2x + 3) + (2h(2x - 1) + 2x + 1)[/imath]​
[imath]f(x)= 2h(2x + 1) + 2h(2x - 1) + 4x + 4[/imath]​
[imath]f(x)= 2(h(2x + 1) + h(2x - 1) + 2x + 2)[/imath]​

Then substitute the definition of [imath]h(x)[/imath]:

[imath]f(x)= 2\left(\frac{(2x + 1 + 1)^2}{4} + \frac{(2x - 1 + 1)^2}{4} + 2x + 2\right)[/imath]​
[imath]f(x)= 2\left(\frac{4x^2+8x+4}{4} + \frac{4x^2}{4} + 2x + 2\right)[/imath]​
[imath]f(x)= 2(x^2+2x+1 + x^2 + 2x + 2)[/imath]​
[imath]f(x)= 2(2x^2+4x+3)[/imath]​
[imath]f(x)= 4x^2+8x+6[/imath]​
[imath]f(x)=[/imath] [imath](2x+2)^2 - 4[/imath] [imath]+6[/imath]​
[imath]f(x)= 4(x+1)^2+2[/imath]​

And this is the same as @blamocur's answer, so I guess I did it right...
Wow
 
Top