Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 the sum of the sum of n integral squares
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
batman12




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
iker




PostPosted: Fri Jan 06, 2006 7:32 pm   Post subject: (No subject)

use for loop such as
code:

var n, x : int := 0
put "enter the ending value"
get n
for i : 1 .. n
    x := x + 2 ** i
end for
put x
Cervantes




PostPosted: Fri Jan 06, 2006 8:12 pm   Post subject: (No subject)

iker: Look at your for loop closely.
code:

for i : 1 .. n
    x := x + 2 ** i
end for

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:
code:

fcn sum_of_natural_squares (n : int) : int
    result round (n * (n + 1) * (2 * n + 1) / 6)
end sum_of_natural_squares

fcn sum_of_natural_squares_2 (n : int) : int
    var final := 0
    for i : 1 .. n
        final += i ** 2
    end for
    result final
end sum_of_natural_squares_2

for i : 1 .. maxrow - 1

    put i : 4, sum_of_natural_squares (i) : 10, sum_of_natural_squares_2 (i) : 10

end for
iker




PostPosted: Fri Jan 06, 2006 11:59 pm   Post subject: (No subject)

Cervantes wrote:
iker: Look at your for loop closely.
code:

for i : 1 .. n
    x := x + 2 ** i
end for

2 ** i is much different than i ** 2

Embarassed 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
code:

for i : 1 .. n
    x := x + i ** 2
end for

It could have
code:

for i : 1 .. n
    x += i ** 2
end for

which saves about 3 characters...
but it doesn't realy matter...
batman12




PostPosted: 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. Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy
person




PostPosted: Sat Jan 07, 2006 4:52 pm   Post subject: (No 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?
Avarita




PostPosted: Sat Jan 07, 2006 6:05 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: