June 21, 2005 at 9:59 am
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
June 21, 2005 at 10:01 am
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?????
June 21, 2005 at 10:02 am
Could you guys check out this thread, he badly needs help that I can't offer :
June 21, 2005 at 10:11 am
Ok, I am on it!
* Noel
June 21, 2005 at 10:13 am
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... .
June 21, 2005 at 10:20 am
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
June 21, 2005 at 10:22 am
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
Vasc
June 21, 2005 at 10:23 am
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
June 21, 2005 at 10:34 am
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
June 21, 2005 at 10:35 am
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
June 21, 2005 at 10:46 am
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
Vasc
June 21, 2005 at 10:53 am
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
June 21, 2005 at 11:19 am
Great I am modyfing the code will have it in a few min
Mike
June 21, 2005 at 11:24 am
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 .
June 21, 2005 at 12:01 pm
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