Computer Science Canada the sum of the sum of n integral squares |
Author: | batman12 [ Fri Jan 06, 2006 1:30 pm ] |
Post subject: | the sum of the sum of n integral squares |
hi , i am using the this forumla for the sum of n integreal number. i am trying to find the sum by using this 1**2+2**2+3**2+4**2+5**2.....n**2=x where the user enters the n value. please give me suggestions how to do this in turing. i am stuck.anyhelp is aprreicated. thank you.thanks. |
Author: | iker [ Fri Jan 06, 2006 7:32 pm ] | ||
Post subject: | |||
use for loop such as
|
Author: | Cervantes [ Fri Jan 06, 2006 8:12 pm ] | ||||
Post subject: | |||||
iker: Look at your for loop closely.
2 ** i is much different than i ** 2 A better way to solve this problem is to use the formula for the sum of the squares of the natural numbers, which is n(n + 1)(2n + 1) / 6 Allow me to demonstrate:
|
Author: | iker [ Fri Jan 06, 2006 11:59 pm ] | ||||||
Post subject: | |||||||
Cervantes wrote: iker: Look at your for loop closely.
2 ** i is much different than i ** 2 thats embarassing, the one thing I noticed is that your code is basically the same as my code, but expressed in a different way also instead of having
It could have
which saves about 3 characters... but it doesn't realy matter... |
Author: | batman12 [ Sat Jan 07, 2006 11:40 am ] |
Post subject: | thnaks very much iker and cervantes for you help |
hey guys, thanks for the help. i got it. thanks for the suggestions. it really helped me out. thanks. |
Author: | person [ Sat Jan 07, 2006 4:52 pm ] |
Post subject: | |
Quote: n(n + 1)(2n + 1) / 6
can u post a proof of y this works, or is this one of those math thngs thats not currently proovable? |
Author: | Avarita [ Sat Jan 07, 2006 6:05 pm ] |
Post subject: | |
ok... let's prove it. The first 5 terms (with the 1st term at 0) of the sequence are: 0, 1, 5, 14, 30 first difference (the difference between each term) = 1, 4, 9, 16 ... second difference (the difference between each term of the first difference) = 3, 6, 9 third difference (the difference between each term of the second difference) = 3, 3, 3 Because the third difference terms are all equal (ie, all being 3), the relationship is cubic, and the standard form for a cubic equation is... y = ax**3 + bx**2 + cx + d Subbing in x = 0 and y = 0 gives us d = 0. Subbing in (1, 1), (2, 5), (3, 14) gives us the following: 1. a + b + c = 1 -> a = 1 - b - c 2. 8a + 4b + 2c = 5 3. 27a + 9b + 3c = 14 Subbing equation #1 in both #2 and #3 gives us the following 2 equations: 4. 8(1 - b - c) + 4b + 2c = 5 -> 4b + 6c = 3 5. 27(1 - b - c) + 9b + 3c = 14 -> 18b + 24b = 13 Multipling equation #4 by 4 then subtract from equation #5: 5. 2b = 1 -> b = 1/2 So now sub b = 1/2 into equation #4: 6. 4(1/2) + 6c = 3 -> c = 1/6 Sub c = 1/6 and b = 1/2 into equation #1: 7. a = 1 - 1/2 - 1/6 -> a = 1/3 So now we have this equation: y = (x**3) / 3 + (x**2) / 2 + x/6 Factor out x/6 and we have: y = x/6 (2x**2 + 3x + 1) Factor this equation and we have: y = x/6 (2x + 1)(x + 1) Which is the same as f(n) = n(n + 1)(2n + 1) / 6 |