Results 1 to 11 of 11

Thread: find all multiples? .:Resolved:.

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Question find all multiples? .:Resolved:.

    Hello everyone, I again ran into a simple algebra 1 concept that i can't figure out. I have to write a program to add up all the multiples of 3 between 1 and 100; This should be cake once i find out how you find a mutliple of a number. Anyone care to give me the run down on how that works? I'm sure I did it before, I got a final grade of 98 in calc which is sad because I forgot basic algebra concepts. Maybe thats why i scored so low on my SAT's heh anyways, thanks for listening
    Last edited by voidflux; Aug 7th, 2003 at 12:45 PM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    A loop like this maybe?

    VB Code:
    1. Dim Total As Integer
    2. Dim i As Integer
    3.  
    4. Total = 0
    5. For i = 1 To 100
    6.     If i Mod 3 = 0 Then
    7.         Total = Total + i
    8.     End If
    9. Next
    10.  
    11. MsgBox Total


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    I didn't even look at ur code because I want to do it on my own (coding) I don't want you to tell me how to code it, I just need the logic behind finding multiplies of 3, what does find a multiple mean? Like I need an example, what is a multiple? Thanks for the responce.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  4. #4
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    A multiple of a number m is a number, n, such that n = m * k where k is some integer.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  5. #5
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    I'll explain his source code...

    The Mod operator returns the remainder of a division.

    So any number that is a multiple of 3(i.e any integer * 3) divided by 3 will return a remainder of 0, thus no remainder.

    Simple huh.

  6. #6
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142
    Finding multiples goes like this...
    9 is a multiple of 3 because when you multiply 3 × 3 you get 9
    10 is not a multiple of 3 because you cannot multiply any whole number with 3 and get 10...

    now if you divide 9 into 3 your answer will be 3 with a remainder of 0
    if you divide 10 into 3 your answer will be 3 with a remainder of 1

    all multiples of a number when divided by that number always have a remainder of 0... so thats what manavo11 was using in his code, he was checking all the numbers from 1 to 100 to see if they had a remainder of 0 when they are divided into 3...

    hope this helps
    ؊Ϯϊ

  7. #7

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Thumbs up

    Excellent! Thank you everyone for your replies!
    Here is what I came up with:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int main(void)
    {
    	//write a for statement to add all multiples 
    	//of 3 between 1 and 100.
    	int sum = 0;
    	for(int i = 1; i <= 100; i++)
    	{
    		if((i%3) == 0)
    		{
    			sum += i;
    			cout << setw(5) << i;
    		}
    
    	}
    	cout <<"\nThe sum of the multiplies is: "
    		 << sum << endl;
    	return 0;
    }
    Code:
        3    6    9   12   15   18   21   24   27   30   33   36   39   42   45   48
       51   54   57   60   63   66   69   72   75   78   81   84   87   90   93   96
       99
    The sum of the multiplies is: 1683
    Press any key to continue
    Last edited by voidflux; Aug 7th, 2003 at 11:44 AM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  8. #8
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    You could make the loop faster if you counted off by 3's until your number is greater than 100.

    Code:
     
    for(int i = 3; i <= 100; i+=3)
    {
         sum += i;
         cout << setw(5) << i;
    }
    Just a suggestion.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  9. #9

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    That is true, and a good idea but it says It wanted you to test numbers 1-100, that loop would be testing numbers 3-100 even though you really don't need to test 1 and 2 i'm just following directions!
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  10. #10
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Actually, its testing every third number from 3 to 100.

    It makes me cry to see wasted processor cycles.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  11. #11
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Actually, since you are looking for the sum of the sequence:
    a0=0
    an=an-1+3
    for n=1...floor(100/3)=33
    Which is:
    (first+last)*number_of_terms/2 = (99+3)*33/2 = 1683

    A program would look like:
    VB Code:
    1. Dim max As Long
    2. max = 100
    3. MsgBox (3+((max\3)*3))*max\6

    This is the maths forum, after all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width