Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: Summer Challenge #1

  1. #1
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default Summer Challenge #1

    summer is almost done and we have not had a challenge, so here goes the first one for whoever is not busy enough:

    sum all the numbers divisible by 3 and 5 less than an integer limit (say 1000).
    most efficient code wins. its open do it in whatever language you like (js,php... even Java)

    implement like
    Code:
    #include <iostream>
    
    /*function return the sum of all numbers n | [0,limit) and n is a multiple of 3 or 5 or both*/
    unsigned int ChallengeSolution(unsigned int limit)
    {
    ...
    }
    
    int main()
    {
        unsigned int limit;
    
        cin >> limit;
    
        cout << ChallengeSolution(limit);
    
        return 0;
    }
    Last edited by icymint3; Aug 15, 2006 at 05:25 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  2. #2
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Well the simplest solution I could think of without brain stroming this too hard. But you did say the most efficient so I did it with D inline assembly..

    Note: the argument and return values are in the EAX register.

    Code:
    public extern(D) uint ChallengeSolution(uint limit)
    {
    	asm
    	{
    		naked;
    		mov ECX, EAX;
    		xor EAX, EAX;
    		xor EDX, EDX;
    	     X0:
    		add EDX, 15;
    		cmp EDX, ECX;
    		ja X1;
    		add EAX, EDX;
    		jmp X0;
    	     X1:
    		ret;
    	}
    }
    I just made the assumption that all multiples of 15 are divisible by 3 and 5, so I just sum those up until I reach the limit. If that's wrong let me know.

  3. #3
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Mi tell you... as mi ready fi open mi compiler inna C#... I scrolled to post 2 and see the answer already. Granted my solution never included 15 in it, but I can't even dust of the compiler in C# no less.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #4
    Join Date
    Feb 2006
    Posts
    4,242
    Rep Power
    0

    Default

    Who else taking the challenge with a different language?
    |--- www.RealJamaicaEstate.com ™ ---|
    Invest small = small returns [micro enterprise] | Invest Big = returns Big [macro enterprise]
    --- www.fashionsJAMAICA.com ™ -|- www.ChampsJamaica.com

  5. #5
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    well i havent downloaded the D language tools yet, so you have to run it and tell us your result. but for starts let us say the limit was 19.
    the answer would be 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 = 78

    Code:
    public extern(D) uint ChallengeSolution(uint limit)
    {
    	asm
    	{
    		naked;
    		mov ECX, EAX; // store the limit which was in EAX somewhere else
    		xor EAX, EAX;  // set EAX to 0
    		xor EDX, EDX;  // set EDX to 0
    	     X0:                        // mark where loop should start
    		add EDX, 15;   // set EDX+= 15
    		cmp EDX, ECX;// some flag = EDX  limit
    		ja X1;            // if we can end then break out of the loop
    		add EAX, EDX;// set EAX+= EDX
    		jmp X0;         // goto the start of the loop
    	     X1:
    		ret;
    	}
    }
    is that equivalent? i dont think it right still.
    Last edited by icymint3; Aug 15, 2006 at 10:53 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  6. #6
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    hint : it can be done without looping ... its second form math.

    its a series, called an Arithmetic Progression, you might need to use some Set Theory to get the correct answer though.

    xwire post the code, Utech22 show us what U.T.E.C.H teach you (been ther done that, i wont give them credit for what you do, even though yu dun sign off pon di document already).
    Last edited by icymint3; Aug 15, 2006 at 11:05 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  7. #7
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Wink

    Code:
    #include <iostream.h>
      int rs=0,result=0;
      int num=0;
      int divby3(int aNum){rs=aNum+1;   if(rs<=1000&&rs%3==0&&rs%5==0){result+=rs; divby3(rs);}
      else{if(rs>=1000){return result;}else{divby3(rs);}}}
    
    int main(int argc, char* argv[])
    {
    
       cout<<divby3(0);
    
    	return 0;
    }
    //---------------------------------------------------------------------------
    not sure its a winner but, ,,,,,,cool

    some recursion
    Anything or Anyone that fails to grow will eventually die. {AI}
    -------------------------------------------------
    Tomorrow is the future!
    Today Is the Tomorrow you made Yesterday!{AI}

  8. #8
    Join Date
    Jul 2004
    Posts
    264
    Rep Power
    0

    Default

    Quote Originally Posted by icymint3
    well i havent downloaded the D language tools yet, so you have to run it and tell us your result. but for starts let us say the limit was 19.
    the answer would be 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 = 78
    ok,
    i think you should have said numbers divisible by 3 or 5 and not 3 and 5...

  9. #9
    Join Date
    Jul 2004
    Posts
    264
    Rep Power
    0

    Default

    Here is my solution

    Code:
    #include <iostream.h>
    
    unsigned int ChallengeSolution(unsigned int limit)
    {
    	if(limit < 3)return 0;
    
    	if(((limit%5)==0) || ((limit%3)==0)){
    		return limit + ChallengeSolution(limit-1);}
    	else{
    		return ChallengeSolution(limit-1);}
    }
    
    int main()
    {
        unsigned int limit;
        cin >> limit;
        cout << ChallengeSolution(limit) << endl;
        return 0;
    }

  10. #10
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by aonekilla
    ok,
    i think you should have said numbers divisible by 3 or 5 and not 3 and 5...
    you are right, sorry.

    the recursion thing, is it because i said it can be done without loops? recursion is normally less efficient than looping for procedural(imperative) languages, what i meant was it can be solved without any type of repetitive construct (including looping, jumping and recursion).

    anyway better you ignore the hint and give me your best solution, hands down.

    the A.P. : for first term a, and difference d
    we can find each term T(n) by :
    T(n) = a + (n-1) * d
    and its summation by :
    S(a,d) = n / 2 ( 2 * a + ( n - 1 ) * d )
    or
    S(a,d) = n / 2 ( a + l ) ... where l is the last term in the sequence.

    if we know the term n (or number of terms for the summation)

    the two series to be used in finding the sum are :
    a = 0, d = 3 ... i.
    and a = 0, d = 5 ... ii.
    Last edited by icymint3; Aug 16, 2006 at 11:46 AM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

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