## [solved] grouping dec numbers by some qualities of their binary equivalents Topic is solved

Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

### [solved] grouping dec numbers by some qualities of their binary equivalents

I am sorry if my question is too stupid, however, placing it in off-topic section I think gives me a right to ask it.

Here is a table (made in a Word): If I wanted to find a quality that is unique for such numbers as 2, 4, 6, 8, 10, 12, etc., that is for all the lines (in the table above) that contain "1" in their D boxes, then that quality would be quite obvious to me: they are all even numbers.

However, what's the unique quality for such numbers as 2, 3, 6, 7, 10, 11, 14, 15, 18, etc. (that is for all the lines that contain "1" in their C boxes)? Is there any formula that would unite all these numbers and set them aside from all other numbers?

What's the unique quality for such numbers as 4, 5, 6, 7, 12, 13, 14, 15, 20, etc. (that is for all the lines that contain "1" in their B boxes)?
Last edited by Benny-D on 18 Mar 2017, 09:13, edited 2 times in total.
joedf
Posts: 8550
Joined: 29 Sep 2013, 17:08
GitHub: joedf
Contact:

### Re: grouping dec numbers by some qualities of their binary equivalents

I'm not sure of the question here.

Every digit place in binary is a power of 2 greater.
Say first is 2^0 (the D group)
Then 2^1 (C)
Then 2^2 (B)
Etc

Using this base, so binary.
There is only one possibility for a number say 2
It would be a 1 in C, because in binary the "letter space" or number space is only 1 or 0. Therefore you cannot simply say 2 times the one in D. it is 0010 and not 0002. Say eg, 0001 + 0001 = 0010.

EDIT:
I guess an answer to your question would be... They all in their respective "number groups", they all have the same remainder. Eg. 15mod2 = 1
So if there's a 1 in D, it's going to an uneven number.

Not sure I'm helping here Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

### Re: grouping dec numbers by some qualities of their binary equivalents

What I need is following:

First group:

Here the answer is clear: in the following group of numbers: 2,4,6,8,10,12,14,16,18,20, etc. their unique quality is that they all can be divided by 2 without leaving any remainder. 2:2=1, 4:2=2, 6:2=3, 8:2=4 and so on.
Thus, if I take any of these decimal numbers, divide it by two and get a result with a remainder, then I can be sure that that decimal number's digital equivalent has 1 in D position:
1 - 0001, 3 - 0011, 5- 0101, 7 - 0111

Second group:

Here I have no idea what the unique quality be for the following group of numbers: 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, etc. (all of them have 1 in C position)

Third group:

Same problem - have no idea what the unique quality be for the following group of numbers: 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31, 36, etc. (all of them have 1 in B position)
guest3456
Posts: 3329
Joined: 09 Oct 2013, 10:31

### Re: grouping dec numbers by some qualities of their binary equivalents

why are you assuming there is any unique quality to begin with?

joedf
Posts: 8550
Joined: 29 Sep 2013, 17:08
GitHub: joedf
Contact:

### Re: grouping dec numbers by some qualities of their binary equivalents

That's a good question. Thanks, I was actually going to ask that. Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

### Re: grouping dec numbers by some qualities of their binary equivalents

guest3456 wrote:why are you assuming there is any unique quality to begin with?
I thought that since their binary equivalents have such qualities, and since each decimal can be easily converted into its unique binary equivalent and vice versa, then the decimals themselves should also have such qualities.
joedf
Posts: 8550
Joined: 29 Sep 2013, 17:08
GitHub: joedf
Contact:

### Re: grouping dec numbers by some qualities of their binary equivalents

Ahh ok, curiousity is a great thing. Posts: 2876
Joined: 17 Oct 2015, 20:28

### Re: grouping dec numbers by some qualities of their binary equivalents  Topic is solved

D position: Nmod2>=1 means D is 1.

C position: Nmod4>=2 means C is 1.

B position: Nmod8>=4 means B is 1.

A position: Nmod16>=8 means A is 1.

For D, we obviously have the names of Even/Odd assigned to numbers. But I do not know if there are names for the groups of numbers categorized by the value in certain place in their binary representation, bar the least significant one (even/odd).

And thanks to you asking this question and helping me think on this issue, I came up with this (although someone probably has better) function to convert numbers into binary:

Code: Select all

``````^0::
Loop, 25
list.=A_Index ":`t" ConvertToBinaryStringRedux(A_Index) "`n"
MsgBox %list%
return

ConvertToBinaryStringRedux(number){
If number is not integer
return
Loop
string:=((Mod(number,x:=(2**A_Index))>=x/2)?1:0) string
Until x>number
return string
}
``````
Edit, here's a bit of a roundabout way to show the numbers with this property using the function above:

Code: Select all

``````^0::
Loop, 25
list.=ConvertToBinaryStringRedux(A_Index) "`n"
MsgBox %list%
StringSplit, array, list, `n
Loop % array0
btruelist.=x:=SubStr(000 . array%A_Index%,-2,1)?A_Index ":`t" array%A_Index% "`n":""
MsgBox % btruelist
return
``````
The -2 there in the second parameter of the SubStr() pulls the third to last digit (in your table in the original post, this would be the B column). If it's a 1, then it's part of this group whose binary has a 1 in the B column, so it's added to the list "btruelist". It also keeps track of the decimal values added to the list so you can see the pattern emerging. The pattern is that you have 2^(k-1) consecutive numbers when you look at the kth digit from the right in the binary representation. That is, when k=1, we're looking in column D and find that the longest streak of consecutive numbers is 1. 2^(1-1) = 2^0 = 1. When you look in column C, the longest streak of consecutive numbers is 2. 2^(2-1) = 2^1 = 2. When you look in column B, the streak is 4. Column A, the streak is 8. In fact, that's exactly the value of the column itself - the right most is 1, the second right most is 2, the third right most is 4, the fourth right most is 8.
Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

### Re: grouping dec numbers by some qualities of their binary equivalents

WOOOOW!!!! Thank you sooo much!!!!!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

### Re: grouping dec numbers by some qualities of their binary equivalents

@Exaskryz nice explanation. If you're interested, here are some functions similar to your ConvertToBinaryStringRedux. NToBin and NToBin2 are pretty close to what you have done. NToBin3 uses a DllCall and it should be the fastest.

Code: Select all

``````List := "N`tF1`tF2`tF3`n"
Loop, 25
List .= A_Index ":`t" NToBin(A_Index) "`t"
. NToBin2(A_Index) "`t" NToBin3(A_Index) "`n"
MsgBox %List%
return
;----------------------------------------------------------------------------
; Similar to NToBin2.
NToBin(n) {
while (n)
result := Mod(n, 0x2) . result
, n := n // 0x2
return, result
}
;----------------------------------------------------------------------------
; Check if the rightmost (least significant) bit is 1 or 0. Shift bits right
; by one. Repeat until n=0.
NToBin2(n) {
while (n)
result := (n & 0x1) . result
, n >>= 0x1
return, result
}
;----------------------------------------------------------------------------
; See https://autohotkey.com/boards/viewtopic.php?p=17835#p17835 for a more
; general purpose base conversion function.
NToBin3(n) {
static u := A_IsUnicode ? "w" : "a"
VarSetCapacity(s,65,0)
DllCall("msvcrt\_i64to" . u, Int64,n, Str,s, Int,2)
return, s
}
;----------------------------------------------------------------------------``````
joedf
Posts: 8550
Joined: 29 Sep 2013, 17:08
GitHub: joedf
Contact:

### Re: grouping dec numbers by some qualities of their binary equivalents

It actually all comes down to what the basis of binary is. joedf
Posts: 8550
Joined: 29 Sep 2013, 17:08
GitHub: joedf
Contact:

### Re: grouping dec numbers by some qualities of their binary equivalents

Oh yes, I meant the 2^power for each cell
:p

Edit: where did kon's post go??!?!? kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

### Re: grouping dec numbers by some qualities of their binary equivalents

kon wrote:Can you elaborate?
Are you referring to the difference between how numbers are represented in memory vs. a string of 1s and 0s?
joedf wrote:Oh yes, I meant the 2^power for each cell
:p

Edit: where did kon's post go??!?!? Thanks.

Sorry about that, I thought I might have been quick enough that no one saw it I deleted my post after about a 1 min. because I thought about it some more and eventually, I think, understood what you meant.
joedf
Posts: 8550
Joined: 29 Sep 2013, 17:08
GitHub: joedf
Contact:

### Re: [solved] grouping dec numbers by some qualities of their binary equivalents

ahhh ok :3
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: [solved] grouping dec numbers by some qualities of their binary equivalents

I was browsing through forums I hadn't much browsed through, noticed this topic, and thought I'd post some code.

@Benny-D: I like this question.
@kon: Cheers for vNum >>= 1, equivalent to vNum := vNum >> 1.

Code: Select all

``````q:: ;checking the nth-to-last digit of numbers written in binary form
;numbers (<= 20) which in binary have '1' as the last digit
;1,3,5,7,9,11,13,15,17,19
;numbers (<= 20) which in binary have '1' as the 2nd-to-last digit
;2,3,6,7,10,11,14,15,18,19
;numbers (<= 20) which in binary have '1' as the 3rd-to-last digit
;4,5,6,7,12,13,14,15,20

;PART 1 - list numbers which in binary have '1' as the nth-to-last digit
Loop, 5
vList%A_Index% := ""
Loop, 40
{
vNum := A_Index
Loop, 5
if (vNum & 2**(A_Index-1))
vList%A_Index% .= vNum ","
}
vOutput := ""
Loop, 5
vOutput .= RTrim(vList%A_Index%, ",") "`r`n"

vOutput .= "`r`n"

;PART 2 - list all numbers and mark with an 'x' those which in binary have '1' as the nth-to-last digit
Loop, 5
{
vList := vList%A_Index%
Loop, 40
{
vNum := A_Index
if vNum in %vList%
vOutput .= "x`t" vNum "`t" JEE_Dec2Bin(vNum, 5) "`r`n"
else
vOutput .= "`t" vNum "`t" JEE_Dec2Bin(vNum, 5) "`r`n"
}
vOutput .= "`r`n"
}

vOutput := SubStr(vOutput, 1, -2)
Clipboard := vOutput
MsgBox % "done"
Return

;==================================================

;where vLen is the minimum length of the number to return (i.e. pad it with zeros if necessary)
JEE_Dec2Bin(vNum, vLen=0)
{
if !RegExMatch(vNum, "^\d+\$")
Return ""
if (vNum = 0)
Return 0
while vNum
vBin := (vNum & 1) vBin, vNum >>= 1
if (StrLen(vBin) < vLen)
Loop, % vLen - StrLen(vBin)
vBin := "0" vBin
Return vBin
}
``````
Btw does anyone have the image for this:
http://mytaiwan.xyz/table_2.jpg

Or for this:
Math Computing Challenge - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=17&t=3762
http://kubli-verlag.com/challengesum.png

[EDIT: Think about every normal (base 10) number. Which numbers have 9 as their last digit/2nd-to-last digit/3rd-to-last digit etc. That is basically the same question, they appear periodically/cyclically in a predictable fashion: '*9' appears every 10 integers, '*9?' appears 10 in a row every 100 integers, '*9??' appears 100 in a row every 1000 integers.]
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

### Re: [solved] grouping dec numbers by some qualities of their binary equivalents

jeeswg wrote:Btw does anyone have the image for this...
The image was this one (I have also re-uploaded it on the first message in this thread): jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: [solved] grouping dec numbers by some qualities of their binary equivalents

Ah very nice! I was confused by references to column D etc. First thinking it meant D for 'digit'. Then thinking well ABCD assumes all binary numbers have 4 digits. Then realising there must have been an image!

Yeah, if you look down each column you can see the repeating pattern. 0 has the same pattern (as 1), and any digit should have a similar pattern in any base (not that I realised that straightaway, but after some thinking).

Thanks so much!
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA