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

Username:   Password: 
 RegisterRegister   
 One of the math challenge problems
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
coldmine16




PostPosted: Thu Aug 10, 2006 10:14 am   Post subject: One of the math challenge problems

hey im having difficulty doing this problem i am using as you can see i have the for loops counting for me and things like that however i forgot one detail to exclude the numbers from 3-999 that are multiplied by 3and5 so basicly im counting them twice in both my loops which i cant do because i can only count them once so for example

if i am counting to 9 by 3 it is 3 6 9 correct
and if i am by 5 to 9 it is 5 and thats it
but lets say 6 wus a multiple of 5 as well i would have it look like this

3- 3 5 6 9
5- 5

since there are 2 5's i only need to count one and add it so i am having trouble figurig out how to take the numbers 3-999 that were counted in my first for loop to not be involved in my second for loop


here is the question

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

here is the code
code:

var num3 : int := 0
var num5 : int := 0
var total : int := 0

for x : 3 .. 1000 by 3
    put x
    num3 := x + num3
end for

for y : 5 .. 1000 by 5
    put y
    num5 := y + num5
end for

cls
total := num3 + num5
put  total

Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Thu Aug 10, 2006 10:49 am   Post subject: (No subject)

Consider the number 15. It is a multiple of both 3 and 5. So num3 and num5 both include the number 15, as well as 30, 45, 60, and so on.

You're counting multiples of both 3 and 5 twice. You need to subtract all multiples of 15 below 1000 from your final answer.

Note that this can be done very easily without any programming language if you know something about sequences and series, or summation rules.

Moved to [General Programming]
cool dude




PostPosted: Thu Aug 10, 2006 11:21 am   Post subject: (No subject)

why don't u just do all that in one for loop.

like this:

code:

var sum : int := 0

for i : 1 .. 1000
    if i mod 3 = 0 or i mod 5 = 0 then
        sum := sum + i
    end if
end for

put sum
coldmine16




PostPosted: Thu Aug 10, 2006 1:08 pm   Post subject: (No subject)

yea i understand what u mean crevantes but i just dont know how to take all the multiples of 15 out like im not sure how to incorparate that in my program
cool dude




PostPosted: Thu Aug 10, 2006 1:37 pm   Post subject: (No subject)

coldmine16 wrote:
yea i understand what u mean crevantes but i just dont know how to take all the multiples of 15 out like im not sure how to incorparate that in my program


just make an if statement checking if its a multiple of 15.

here:

code:

var num3 : int := 0
var num5 : int := 0
var total : int := 0

for x : 3 .. 1000 by 3
    num3 := x + num3
end for

for y : 5 .. 1000 by 5
    if y mod 15 not= 0 then
        num5 := y + num5
    end if
end for

cls
total := num3 + num5
put total


however, i find it really stupid the way your doing. you can do all that in one for loop. my way is better.
wtd




PostPosted: Thu Aug 10, 2006 3:38 pm   Post subject: (No subject)

cool dude wrote:
however, i find it really stupid the way your doing. you can do all that in one for loop. my way is better.


Can you guess which word of that probably isn't very nice?
Cervantes




PostPosted: Thu Aug 10, 2006 4:39 pm   Post subject: (No subject)

cool dude wrote:
however, i find it really stupid the way your doing. you can do all that in one for loop. my way is better.

First, your way is wrong*.
Second, you need to define "better". Yours is 4 lines shorter, but his, after some fixing to incorporate the multiples of 15, ran over three times faster than yours on my computer. With that in mind, I'd say his is better, hands down.

The point is, coldmine16 is doing it the way he understands it. No need to insult the guy.

As for the modification of the original code:
cool dude wrote:
code:
var num3 : int := 0
var num5 : int := 0
var total : int := 0

for x : 3 .. 1000 by 3
    num3 := x + num3
end for

for y : 5 .. 1000 by 5
    if y mod 15 not= 0 then
        num5 := y + num5
    end if
end for

cls
total := num3 + num5
put total


This isn't being consistant with his approach. It's merging your approach with his, which then raises the question, "why not just use one or the other?"

What I was suggesting is to add another for loop to subtract the sum of all multiples of 15 lower than 1000:
code:

% Being consistant with coldmine16's program:

% Notice that I'm using 999, not 1000, because we're looking for
% the sum of the numbers BELOW 1000
for z : 1 .. 999 by 15
    num15 := z + num15
end for
total := num3 + num5 - num15


* - Numbers must be less than 1000. Your code includes 1000 in the sum, and will hence give an answer equal to the correct answer + 1000.

Actually, coldmine16's code has the exact same problem.
coldmine16




PostPosted: Thu Aug 10, 2006 4:55 pm   Post subject: (No subject)

cool dude if ur intrested heres my program to solve the problem

code:

var num3 : int := 0
var num5 : int := 0
var total : int := 0
var num15 : int := 0
for x : 3 .. 999 by 3
    num3 := x + num3
end for

for y : 5 .. 999 by 5
    num5 := y + num5
end for

for a : 15 .. 999 by 15
    num15 := a + num15
end for
cls
total := (num3 + num5) - num15
put total



the answer it gives is 233168 which when checked is correct thanks guys for some of the help
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Fri Aug 11, 2006 7:38 pm   Post subject: (No subject)

coldmine16 if u noticed i did the same mistake as u and made my for loop go up to 1000. if u change it to go up to 999 it will be the same answer. thus both are ways are correct. all i meant by "stupid" was that my way is shorter code and has one for loop instead of three and less variables and no need to subtract anything, but both ways work so use the way your more comfortable with. if i insulted u as cerventes puts it or offended u in any way my appoligies.

@cerventes: just interested how u determine which code runs faster. is it by the eye or did u design a program? and if so do u mind showing me the program just for interest?
Cervantes




PostPosted: Fri Aug 11, 2006 7:48 pm   Post subject: (No subject)

cool dude wrote:
@cerventes: just interested how u determine which code runs faster. is it by the eye or did u design a program? and if so do u mind showing me the program just for interest?


I just wrapped his code in a for loop to repeat it 1000 times and used Time.Elapsed to determine how long it takes for his code to solve the problem 1000 times. I then did the same with your code. His ran in ~700ms, yours in ~2200, as I recall. The reason being that your code requires the use of 2 mod's per iteration as well as a conditional with boolean logic. His is just straight math.
OneOffDriveByPoster




PostPosted: Fri Aug 11, 2006 10:32 pm   Post subject: (No subject)

Hopefully, I wrote this right...

code:
        I = 999 / 3
        J = 999 / 5
        K = 999 / 15
        I = (I * I + I) / 2 * 3
        J = (J * J + J) / 2 * 5
        K = (K * K + K) / 2 * 15
        PRINT *, (I + J - K)
        END
Catalyst




PostPosted: Fri Aug 11, 2006 10:40 pm   Post subject: (No subject)

straight math can be helpful in this situation
code:

fcn sumi (n : int) : int
    result (n * (n + 1)) div 2
end sumi
fcn sum15 (n : int) : int
    result 3 * sumi (floor (n / 3))
         + 5 * sumi (floor (n / 5))
        - 15 * sumi (floor (n / 15))
end sum15
put sum15 (999)


edit:took to long to post,guy above beat me to it with the same idea Very Happy
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: