Matching Values

  • With Brute force, it will all depend on how soon you find the match

    Think of it as a Dictionary Attack,  sometimes they are successful

     


    * Noel

  • I always work with the worst case scenario in those situation... so let's assume it takes all numbers to arrive at the sum...

    also is it possible to use the same number more than once?????

  • Could you guys check out this thread, he badly needs help that I can't offer :

    ACCIDENTALLY REWROTE THE PRODUCTION DATABASE

  • Ok, I am on it!


    * Noel

  • Thanx. The most I can say is you need a backup or a log explorer to rollback the changes... that's not much help when you're in that much troubles... .

  • I would be happy to make if it takes only 3 Loops in Client side. I cannot think of that logic. Can you help on this.

    Regards,
    gova

  • Your problem SHOULD be solved at the client side since it will require loops and math calc...

    Actually you don't have so many combinations...

    An sample RECURSIVE alg is

    1--arrange numbers in order

    2--pick the next number (start with biggest possible )

    3 --if picked No> MyNo go to 2

    4 --if pickedNo=MyNo ---Success Stop

    5 -- else go to step 2 with No=No-pickedNO

     

    This can be improved by adding conditions like :

    -- stop search if sum (remaining no is < MyNo)

    -- stop search if MIN(Numbers)>MyNo


    Kindest Regards,

    Vasc

  • If you meant the values yes Same values can repeat.

    Out of 20 we can remove all the values that exceeds the Input value so that would reduce 20 to some lesser value in many cases.

    Regards,
    gova

  • Govinn if you need to only find the first occurance where the values in the table = the passed value I have a Vb program that will do this client side. If you need all the possible values that is a horse of a different color. Let me know if you can do this on the client.

    Mike

  • Vasc I did that Algoritham First (See the code) It Fails if the values are apart in scatered 3 places. It works for consecutive #s great

     

    IF (SELECT SUM(Value) FROM @Values) < @myAmount

     PRINT 'Sum of all less than Input'

    ELSE IF EXISTS (SELECT Value FROM @Values WHERE Value = @myAmount)

     SELECT Value FROM @Values WHERE Value = @myAmount

    ELSE

    BEGIN

     DECLARE @myValues TABLE

     (

      Ctr INT IDENTITY,

      Value   NUMERIC(10,2))

     DECLARE @Checker TABLE

     (

      IDS INT)

     DECLARE @Ctr INT

     DECLARE @CtrLp INT

     DECLARE @CtrMin INT

     DECLARE @CtrMax INT

     DECLARE @Found BIT SET @Found = 0

     /* GET ALL THE VALUES LESS THAN THE MAIN AMOUNT */

     INSERT INTO @myValues (Value)

     SELECT Value FROM @Values WHERE Value < @myAmount

     

     WHILE EXISTS (SELECT * FROM @myValues) AND @Found = 0

     BEGIN

      DELETE @Checker

      SELECT @CtrMin = MIN(Ctr), @CtrMax = 0 FROM @myValues

      WHILE (@CtrMin <= (SELECT MAX(Ctr) FROM @myValues)) AND (@Found = 0)

      BEGIN

       SET @CtrLp = @CtrMin

     

       WHILE (@CtrLp <= (SELECT MAX(Ctr) FROM @myValues)) AND (@Found = 0)

       BEGIN

       

        SET @Ctr  = 0

        

        WHILE (@Ctr <= (SELECT MAX(Ctr) FROM @myValues)) AND (@Found = 0)

        BEGIN

        

         --SELECT @CtrMin, @CtrLp, @CtrMax

         SET @CtrMax = @CtrMin + @Ctr

         IF (SELECT SUM(Value) FROM @myValues WHERE Ctr = @CtrMin OR Ctr BETWEEN @CtrLp AND @CtrMax) = @myAmount

         BEGIN

          SELECT Value

          FROM

           @myValues

           WHERE Ctr = @CtrMin OR Ctr BETWEEN @CtrLp AND @CtrMax

      

          SET @Found = 1

      

         END

         SET @Ctr = @Ctr + 1

        END

     

        DELETE @Checker WHERE IDS = @Ctr

        

        SET @CtrLp = @CtrLp + 1

       END

       SET @CtrMin = @CtrMin + 1

      END

      DELETE @myValues WHERE Ctr = (SELECT MIN(Ctr) FROM @myValues)

     END

     END

     

    Regards,
    gova

  • RECURSIVE ...

    Your alg is tottally different ...

    I ll explain more:

    bool MyFunction(NoOfElemInList,MyNumb,List)

    {

    while (NoOfElemInList>0) and List(NoOfElemInList)>MyNumb

         NoOfElemInList--;

    if List(NoOfElemInList)=MyNumb return true

    else 

    While (NoOfEleminList>0) and NOT MyFunction(NoOfElemInNewList,MyNumb-List(NoOfElemInList),NewLIst)

         NoOfElemInList--;

    return false;

    }

     

    smthing like that you need

     

    OBSERVE the RECURSIVE call of MyFunc with new values


    Kindest Regards,

    Vasc

  • Micael if you can post te VB code that would be great. I need the first matched value not all of them. I can do that at client side.

    If I can see the logic of VB I think I can make a C# out of it.

    Regards,
    gova

  • Great I am modyfing the code will have it in a few min

    Mike

  • One thing that me be self evident at this point... maybe you should forbid the number 1 since 1+1+1 n times will always eventually equal any integer you want .

  • looks like in the end you are listening and hope fully do this client side.

    The three loops I was mentioning were:

    1. for the number of roots

    2.for the spacing of them

    3.for the combination of the two above to determine pointers position:

    Best language for this: Any that handles Pointer arithmetics (C, C++, C#, and maybe others that I don't know of)

    Once more I would like to suggest not to use Brute force and use what mathematicians have done for us during many many years. If you do that the number of operations will be reduced drastically and you will get what you want.

     


    * Noel

Viewing 15 posts - 16 through 30 (of 101 total)

You must be logged in to reply to this topic. Login to reply