Re: [amibroker] Finding all possible combinations of numbers to reach a given sum

 

Greetings --

We'll begin with an example solved by hand, which suggests how to write the code. 

Assume you will have up to 8 properties that you want to be set to either True of False.  Begin by assigning each property a number between 0 and 7.  Assume that you want to set the composite flag variable so that properties 0, 3, 5, 6, and 7 are True, the others False.

Compute the composite flag number using the positional number system. 
Flags = 2^7 *1 + 2^6 *1 + 2^5 *1 + 2^4 *0 + 2^3 *1 + 2^2 * 0 + 2^1 *0 + 2^0 *1; 
Flags = 128 + 64 + 32 + 8 + 1 == 233

----------------------

Later, in your program, you recall the value of the composite flag and you want to learn which properties are set to True.  Assume it is still 233.  You might have had your program change it, but we'll use 233 to show that it recovers the switches we set earlier.

Find all of the binary switches that are set in the number 233.

The largest number you could possibly code into an 8 bit integer is 255.  Which is 2^8 - 1.  The value associated with the highest position, 7, is 2^7, which is 128.  If all 8 bits are set, the flag variable will have a value of 255. 

The algorithm is a loop that counts down from 7 to 0, checking to see if a term of the size you are testing has a weight of 1 or 0.

Begin with bit position 7 and the original value of the number, 233.  2^7 == 128.  128 is less than or equal to 233, so the 128 term ( bit position 7), is present and set to 1.  Subtract 1 * 128 from 233, leaving 105.
Check bit position 6 against the remainder of 105.  2^6 == 64.  64 is less than or equal to 105, so the 64 term (bit position 6), is present and set to 1.  Subtract 1*64 from 105, leaving 41.
Check bit position 5 against the remainder of 41.  2^5 == 32.  32 is less than or equal to 41, so the 32 term (bit position 5), is present and set to 1.  Subtract 1*32 from 41, leaving 9.
Check bit position 4 against the remainder of 9.  2^4 == 16.  16 is Not less than or equal to 9, so the 16 term (bit position 4), is missing and set to 0.  Subtract 0*16 from 9, leaving 9.
Check bit position 3 against the remainder of 9.  2^3 == 8.  8 is less than or equal to 9, so the 8 term (bit position 3), is present and set to 1.  Subtract 1*8 from 9, leaving 1.
Check bit position 2 against the remainder of 1.  2^2 == 4.  4 is Not less than or equal to 1, so the 4 term is missing and set to 0.  Subtract 0*4 from 1, leaving 1.
Check bit position 1 against the remainder of 1.  2^1 == 2.  2 is Not less than or equal to 1, so the 2 term is missing and set to 0.  Subtract 0*2 from 1, leaving 1.
Check bit position 0 against the remainder of 1.  2^0 == 1.  1 is less than or equal to 1, so the 1 term is present and set to 1.  Subtract 1 from 1, leaving 0.
You have checked all 8 bit positions (0 through 7) and have a remainder of 0.

Flags 7, 6, 5, 3 and 0 are True.  The others are False.

Writing the afl for the loop is straight forward.  Deciding how to use that information depends on your specific program and left as an exercise.

Best regards,
Howard

On Wed, Feb 17, 2016 at 10:21 AM, l3456@excite.com [amibroker] <amibroker@yahoogroups.com> wrote:
 

Thanks so much for the explanation. you said
"So, to decode a flag, check to see which powers of 2 are present in the number."
How is this done? What is the logic?I need to code this in afl. Lets assume we have sum total of 21 for the flags that are active, how do I programmatically find the three flags that are on are positioned on 0, 1 and 4.

__._,_.___

Posted by: Howard B <howardbandy@gmail.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (9)
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/


.

__,_._,___

Related Posts


EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng
:lv