Please help me write a program to find the sum of the first 300 even integers! I've been trying for hours and I can't figure it out. I know...I'm new to this
Offline
This is what I originally tried and it gave me a sum of 602, which is obviously wrong. Am I missing something simple here?
Offline
Set count to 0
Set number to 0
Repeat 301
Change number by count
Change count by 1
End repeat
Say number for 2 seconds
Offline
bbbeb was almost right:
bbbeb wrote:
[Set Count to 2]
[Repeat 300]
[][Set Sum to ((Count)+2)
[][Change count by 4]
[EndRepeat]
[Say [(Sum)]]
If you make the bolded one 2 it just uses a number twice, so you're using less high numbers.
You get 1202.
Last edited by Kileymeister (2011-02-12 16:46:17)
Offline
Just use this rule:
If you want to add up all the multiples of s up to n, then the formula is:
sum = (n+s)*(n/s)/2
in your case, s = 2 and n = 300, so:
sum = (300 + 2)*(300/2)/2 = 302*150/2 = 302 * 75 = 22,650
This is a lot quicker and shorter than repeated addition, and you can do it by hand too.
Does this help?
Offline
bbbeb wrote:
[Set Count to 2]
[Set Sum to 0]
[Repeat 300] You want all integers up to 300, not 300 integers. change this to '[Repeat until <Count > 300>]'.
[][Set Sum to ((Count)+2) It's 'Change sum by', not 'Set sum to'
[][Change count by 2]
[EndRepeat]
[Say [(Sum)]]
Here is the fix to this script.
Last edited by floppy_gunk (2011-02-12 17:20:47)
Offline
Kileymeister wrote:
bbbeb was almost right:
bbbeb wrote:
[Set Count to 2]
[Repeat 300]
[][Set Sum to ((Count)+2)
[][Change count by 4]
[EndRepeat]
[Say [(Sum)]]If you make the bolded one 2 it just uses a number twice, so you're using less high numbers.
You get 1202.
That 2 should be there, but it's the algorithm itself that's wrong.
Offline
floppy_gunk wrote:
bbbeb wrote:
[Set Count to 2]
[Set Sum to 0]
[Repeat 300] You want all integers up to 300, not 300 integers. change this to '[Repeat until <Count > 300>]'.
[][Set Sum to ((Count)+2) It's 'Change sum by', not 'Set sum to'
[][Change count by 2]
[EndRepeat]
[Say [(Sum)]]Here is the fix to this script.
He wants the first 300 even integers. Which is all even integers up to 600.
Last edited by Kileymeister (2011-02-12 19:30:12)
Offline
Woah woah, you all messed up. Even me.
[Set count to 2] (Sets the default to 2) [Set Sum to 0] (Sets sum to 0) [Repeat 150] (He wanted all even integers to 300, meaning 150 of them.) [][Set sum to ((sum)+(count))] (Adds the count to the sum) [][Change count by 2] (Adds 2 to the count) [EndRepeat] () [Say [(Sum)]] (Says the sum.)
Kiley's Error:
Kileymiester wrote:
bbbeb was almost right:
bbbeb wrote:
[Set Count to 2]
[Repeat 300] Thats 1000 quad numbers.
[][Set Sum to ((Count)+2) This was my messup.
[][Change count by 4] That gets all quad numbers.
[EndRepeat]
[Say [(Sum)]]If you make the bolded one 2 it just uses a number twice, so you're using less high numbers.
You get 1202.
Floppy's error:
floppy_gunk wrote:
bbbeb wrote:
[Set Count to 2]
[Set Sum to 0]
[Repeat 300] You want all integers up to 300, not 300 integers. change this to '[Repeat until <Count > 300>]'. Wrong. It should be Repeat 150.
[][Set Sum to ((Count)+2) It's 'Change sum by', not 'Set sum to' Also, the set sum is correct, but the count is wrong.
[][Change count by 2]
[EndRepeat]
[Say [(Sum)]]Here is the fix to this script.
So, we all made errors.
Last edited by bbbeb (2011-02-12 19:16:47)
Offline
Using a combination of many of your scripts I made one that worked. Floppy your algorithm worked too.
Thank you all for the help!!
Offline