This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-02-12 14:44:02

Twizzler14
New Scratcher
Registered: 2011-01-14
Posts: 3

Sum of integers

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   sad

Offline

 

#2 2011-02-12 15:08:22

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Sum of integers

[Set Count to 2]
[Repeat 300]
[][Set Sum to ((Count)+2)
[][Change count by 2]
[EndRepeat]
[Say [(Sum)]]


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#3 2011-02-12 15:20:59

Twizzler14
New Scratcher
Registered: 2011-01-14
Posts: 3

Re: Sum of integers

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

 

#4 2011-02-12 16:03:31

colorfusion
Scratcher
Registered: 2009-10-03
Posts: 500+

Re: Sum of integers

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

 

#5 2011-02-12 16:45:52

Kileymeister
Scratcher
Registered: 2008-04-17
Posts: 1000+

Re: Sum of integers

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)


I'm back, and showcasing two new* projects!  Click left or right on the image below to see!
http://img109.imageshack.us/img109/7905/part1l.pnghttp://img859.imageshack.us/img859/6417/part2bf.png

Offline

 

#6 2011-02-12 17:10:54

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: Sum of integers

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?


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#7 2011-02-12 17:18:24

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: Sum of integers

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)


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#8 2011-02-12 17:24:05

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: Sum of integers

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.


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#9 2011-02-12 18:34:02

Kileymeister
Scratcher
Registered: 2008-04-17
Posts: 1000+

Re: Sum of integers

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)


I'm back, and showcasing two new* projects!  Click left or right on the image below to see!
http://img109.imageshack.us/img109/7905/part1l.pnghttp://img859.imageshack.us/img859/6417/part2bf.png

Offline

 

#10 2011-02-12 19:12:24

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Sum of integers

Woah woah, you all messed up. Even me.

Code:

[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.  tongue

Last edited by bbbeb (2011-02-12 19:16:47)


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#11 2011-02-12 21:59:34

Twizzler14
New Scratcher
Registered: 2011-01-14
Posts: 3

Re: Sum of integers

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

 

Board footer