• Create Account

VERY VERY VERY tough math problem --> wonder who can solve this one???

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

#1sephiroth03  Members

122
Like
Likes
Like

Posted 03 August 2001 - 11:40 PM

Hi you guys. I don't know if this is the correct forum, but I have a math problem, (at least i think it is a math problem, maybe it's a programming problem, anyways i think this problem is one for math/programming experts). A friend of mine asked me to solve this problem. So here it is: Suppose you have a set of checkboxes, which all have his own value: 1 - Checkbox 1 2 - Checkbox 2 4 - Checkbox 3 8 - Checkbox 4 16 - Checkbox 5 32 - Checkbox 6 64 - Checkbox 7 128 - Checkbox 8 256 - Checkbox 9 512 - Checkbox 10 1024 - Checkbox 11 2048 - Checkbox 12 4096 - Checkbox 13 8192 - Checkbox 14 16384 - Checkbox 15 32768 - Checkbox 16 131072 - Checkbox 17 262144 - Checkbox 18 524288 - Checkbox 19 As you can see, the value of one checkbox is the double of the value of the previous checkbox (ie: The value of Checkbox 6 is 32 and the value of Checkbox 7 is 64 and so on) All of these Checkboxes have by default been turned of, and there is a command line where a user can enter a number which corresponds to one of these numbers and that number turns the corresponding checkbox on (ie: when the user enters the number 4096, Checkbox 13 is turned on). The idea is, if the user enters the number 768(256+512) then the checkboxes 9 and 10 should be turned on. Now my question is, if the user enters a number, ie 3072(1024+2048), is there any way for the program to know which checkboxes should be turned on??? In this case the chechboxes 11+12 should be turned on. BTW, If you post code, i would prefer it in VB (cause my friend's programming in VB). Bu C++ would do just fine (cause i can convert that to VB). Gosh i SURE hope this is the correct forum !!! And guys PLEASE PLEASE PLEASE reply !!! If you prefer answering me via e-mail: riteshov@hotmail.com Greetz, Sephiroth The Third "Some that live deserve to die and some that are dead deserve to live" Edited by - sephiroth03 on August 4, 2001 6:49:09 AM

#2brettporter  Members

122
Like
Likes
Like

Posted 03 August 2001 - 11:48 PM

  for (int i = 0; i < numCheckboxes; i++ ) { if (( number & ( 1 << i )) != 0 ) { // turn on checkbox i }}

Remember & is the bitwise and operator. There''s a tonne of ways to do this. No idea what the VB equivalents are, probably BITWISE_AND or something (definitely not & - that''s string concatenation).

Hope this helps.

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL

#3sephiroth03  Members

122
Like
Likes
Like

Posted 04 August 2001 - 12:03 AM

brettporter: are you sure ONLY the correct checkboxes are turned on and nothing but those??? ie: that when the user enters the number 3072 that the checkboxes 11 AND 12 are turned on and not just 11 or just 12??? Of course this code should also work if the user wants to turn on MORE than 2 checkboxes (ie: checkboxes 1,2,3 (value=7))???

If this code works then very much thanks from me and my friend !!!

Greetz,
Sephiroth The Third

"Some that live deserve to die and some that are dead deserve to live"

Edited by - sephiroth03 on August 4, 2001 7:04:16 AM

#4brettporter  Members

122
Like
Likes
Like

Posted 04 August 2001 - 12:23 AM

its late, I may be wrong

let''s see... say they entered 10
in binary:
num = 1010

  num & ( 1 << 0 ) = num & 1 = 0num & ( 1 << 1 ) = num & 2 = 1num & ( 1 << 2 ) = num & 4 = 0num & ( 1 << 3 ) = num & 8 = 1

make sense for this small example?

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL

#5sephiroth03  Members

122
Like
Likes
Like

Posted 04 August 2001 - 12:30 AM

I don't really understand your last post, plz explain. What do you reach by using that code; so you now know the user entered 10, the checkboxes 4+2 should be turned on, but how does the program finds that out??? And what if the user enters something else as 10 ??? I mean what if the user enters 3072, would that code STILL work ???
Greetz,

Sephiroth The Third

"Some that live deserve to die and some that are dead deserve to live"

Edited by - sephiroth03 on August 4, 2001 7:37:56 AM

#6sephiroth03  Members

122
Like
Likes
Like

Posted 04 August 2001 - 12:41 AM

Oh and if you post code again, plz give me a working fuction, not some loose lines of code i don''t know how to use

Greetz,

Sephiroth The Third

"Some that live deserve to die and some that are dead deserve to live"

#7 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 04 August 2001 - 03:42 AM

brettporter''s code is correct. Your problem is just basic binary arithmetic. Your checkbox numbers are following powers of two. Imagine them as a bitstring, a 1-bit is a set checkbox, a 0-bit is an unset box. Take the binary representation of the number you entered and turn on the checkboxes where the bits are 1. Brettporters code does exactly that.

#8Akura  Members

130
Like
Likes
Like

Posted 04 August 2001 - 05:36 AM

k, it can go like this

since each checkbox number has a value of 2^(CheckBoxNumber - 1)
you can use any number to check the checkboxes what you have to do is:

Go throu every bit in the number entered (prf from left to right) and if that bit is 1, then checkbox number: bitorder-1 is set

this is even easier if checkboxes are an array, the following PDL assumes so

for i=max_n_bits_in_number to 0 ''(prob 32)
if number_bit == 1
checkbox [i-1].checked = true
end if
subtract 1 to i
end for

number_bit [i] means the bit order, for ex:

10010101
is:
76543210

cehck MSDN for info on how to do this in VB

#9FXO  Members

122
Like
Likes
Like

Posted 04 August 2001 - 06:00 AM

Brett porters code is totally correct,
here''s some VB code that does the same

  Dim i As IntegerDim val As IntegerConst NumCheckBoxes = 12''checkbox 0=1, 1=2, 2=4, 3=8 val = InputBox("Set val") For i = 0 To NumCheckBoxes - 1 If (val And (2 ^ i)) Then '' turn on checkbox i MsgBox i End If Next i

#10Beer Hunter  Members

712
Like
Likes
Like

Posted 04 August 2001 - 01:58 PM

This is basic arithmetic. Not even basic binary arithmetic. In any case, it''s not a VERY VERY UBER-IMPOSSIBLE UNGODLY MATHS PROBLEM.

Sorry, had to get that out.

By the way, checkboxes 17, 18 and 19 all need their values halved; you jumped from 32768 to 131072.

Dim num As Long, max As Long, x As Integernum = Text1.Textmax = 262144For x = 19 To 1 Step -1    If num >= max Then        chkThing(x).Value = 1        num = num - max    Else        chkThing(x).Value = 0    End If    max = max / 2Next

#11brettporter  Members

122
Like
Likes
Like

Posted 04 August 2001 - 10:01 PM

"I mean what if the user enters 3072, would that code STILL work ???"

Yes.

"plz give me a working fuction, not some loose lines of code i don''t know how to use"

It''s nobody''s job around here to write your code for you. I showed more than enough to solve what you originally asked. If you are struggling understanding a for loop, the Maths and Physics Forum is probably not the best place to hang around.

Just for a point of reference of how hard this problem actually should be - I learnt that in grade 9 math - and surprisingly I still remember it! :D

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.