|
Post by Brandon Parker on Oct 29, 2021 16:52:49 GMT -5
Here is a simple example for the Rosetta Code task titled Two identical strings. Rosetta Code - Two identical stringsHere is the code: 'Not testing 1 since it only has one binary digit For i = 2 To 1000 bin$ = DecToBin$(i) 'Only test those binary numbers where the number 'of digits is evenly divisible by 2 If Not(Len(bin$) Mod 2) Then leftBin$ = Left$(bin$, (Len(bin$)/2)) rightBin$ = Right$(bin$, (Len(bin$)/2)) If (leftBin$ = rightBin$) Then Print i;" - ";bin$ End If End If Next i End
Function DecToBin$(decNum) While decNum R = (decNum Mod 2) DecToBin$ = R;DecToBin$ decNum = Int(decNum/ 2) Wend If (DecToBin$ = "") Then DecToBin$ = "0" End Function {:0) Brandon Parker
|
|
|
Post by tsh73 on Oct 30, 2021 2:41:29 GMT -5
Other way round
'[RC] Two identical strings 'https://rosettacode.org/wiki/Two_identical_strings 'tsh73 maxBin$ = num2bin$(1000) print "Biggest number is ";maxBin$ print "with length ";len(maxBin$) 'so 10 is max N=int(len(maxBin$)/2) '5 print "So we just should check pieces by max ";N;" binary digits" for i = 1 to 2^N-1 bin$= num2bin$(i) d$ = bin$;bin$ d = bin2Num(d$) if d > 1000 then exit for print i, d, space$(10-len(d$));d$ next
function bin2Num(s$) h = 0 For I = 1 To Len(s$) h = h * 2 If Mid$(s$, I, 1) = "1" Then h = h + 1 Next I bin2Num = h End Function
function num2bin$(n) num2bin$="" while n>0 d=n mod 2 if d then num2bin$="1"+num2bin$ else num2bin$="0"+num2bin$ n=int(n/2) wend if num2bin$="" then num2bin$="0" End Function
|
|
|
Post by Brandon Parker on Oct 30, 2021 22:01:36 GMT -5
I like it...
You can also just do the first part like this if you state that ((2^10) - 1) is known to be > 1000, so we will use ((2^maxN) - 2). Then, we can just calculate the value of the two bin$ stacked together.
For i = 1 To ((2^maxN) - 2) bin$ = DecToBin$(i) Print i;" : ";(((2^Len(bin$))*i) + i);" - ";bin$;bin$ Next i
You can even shorten the decimal to binary function like this...
Function DecToBin$(decNum) While decNum DecToBin$ = (decNum Mod 2);DecToBin$ decNum = Int(decNum/ 2) Wend If (DecToBin$ = "") Then DecToBin$ = "0" End Function
I always take the cheap way out and copy the previous one I posted from an old program that had it set up specifically to do certain things inside the function. But, if you don't need any control, this is definitely the way to do it...
I did update the entry.
{:0)
Brandon Parker
|
|
|
Post by Brandon Parker on Oct 30, 2021 22:54:47 GMT -5
So, what about this...?
maxNumber = 1000 maxN = Int(Len(DecToBin$(maxNumber))/ 2) Print "Value"," Binary" 'Since 1 is obviously not applicable, 'just count to ((2^maxN) - 2); Using "- 2" because 'we know that ((2^5) - 1) = 1023 which is > 1000 For i = 1 To ((2^maxN) - 2) bin$ = DecToBin$(i) 'Let's format the output nicely Print (((2^Len(bin$))*i) + i),Space$((maxN * 2) - Len(bin$;bin$));bin$;bin$ Next i End
Function DecToBin$(decNum) While decNum DecToBin$ = (decNum Mod 2);DecToBin$ decNum = Int(decNum/ 2) Wend If (DecToBin$ = "") Then DecToBin$ = "0" End Function
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Oct 31, 2021 1:40:19 GMT -5
((2^Len(bin$))*i) + i is clever part that escaped me. But bin2Num(bin$;bin$) definitely easier to understand
|
|
|
Post by Brandon Parker on Oct 31, 2021 17:32:07 GMT -5
Yes, I agree... I fried a few brain cells reckoning with myself trying to make sure I understood what I was trying to do. {:0) Brandon Parker
|
|