Two identical strings: Difference between revisions

Add Refal
(Add Refal)
 
(34 intermediate revisions by 22 users not shown)
Line 10:
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F bits(=n)
 
<lang 11l>F bits(=n)
‘Count the amount of bits required to represent n’
V r = 0
Line 26 ⟶ 25:
L concat(n) <= 1000
print(‘#.: #.’.format(concat(n), bin(concat(n))))
n++</langsyntaxhighlight>
 
{{out}}
Line 63 ⟶ 62:
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="8080asm"> ;;; Print positive integers whose base-2 representation
;;; is the concatenation of two identical binary strings,
;;; for 1 < n < 1000
Line 152 ⟶ 151:
db '***********'
outbuf: db 9,'$'
nl: db 13,10,'$'</langsyntaxhighlight>
 
{{out}}
Line 188 ⟶ 187:
 
=={{header|8086 Assembly}}==
<langsyntaxhighlight lang="asm">puts: equ 9
cpu 8086
org 100h
Line 241 ⟶ 240:
nl: db 13,10,'$'
db '****************'
outbuf: db 9,'$'</langsyntaxhighlight>
{{out}}
<pre>3 11
Line 273 ⟶ 272:
957 1110111101
990 1111011110</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC Encode(INT x CHAR ARRAY s)
BYTE len,i
CHAR tmp
 
len=0 tmp=x
WHILE tmp#0
DO
len==+1
s(len)='0+(tmp&1)
tmp==RSH 1
OD
FOR i=1 TO len RSH 1
DO
tmp=s(i) s(i)=s(len-i+1) s(len-i+1)=tmp
OD
FOR i=1 TO len
DO
s(i+len)=s(i)
OD
s(0)=len
RETURN (x LSH len+x)
 
PROC Main()
CHAR ARRAY s(20)
INT i=[1],res,count=[0]
 
DO
res=Encode(i,s)
IF res>=1000 THEN
EXIT
FI
Position((count&1)*15+2,count RSH 1+1)
PrintF("%I: %S",res,s)
i==+1 count==+1
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Two_identical_strings.png Screenshot from Atari 8-bit computer]
<pre>
3: 1 10: 10
15: 11 36: 100
45: 101 54: 110
63: 111 136: 1000
153: 1001 170: 1010
187: 1011 204: 1100
221: 1101 238: 1110
255: 1111 528: 10000
561: 10001 594: 10010
627: 10011 660: 10100
693: 10101 726: 10110
759: 10111 792: 11000
825: 11001 858: 11010
891: 11011 924: 11100
957: 11101 990: 11110
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io; use Ada.Text_Io;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
 
Line 296 ⟶ 352:
end if;
end loop;
end Two_Identical;</langsyntaxhighlight>
{{out}}
<pre> 3 2#11#
Line 330 ⟶ 386:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # show the decimal and binary representations of numbers that are of the concatenation of #
# two identical binary strings #
# returns a binary representation of v #
Line 353 ⟶ 409:
print( ( whole( cat value, -4 ), ": ", TOBINSTRING cat value, newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 389 ⟶ 445:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
integer function concatself(n);
integer n;
Line 421 ⟶ 477:
m := concatself(n);
end;
end</langsyntaxhighlight>
{{out}}
<pre> 3: 11
Line 456 ⟶ 512:
=={{header|ALGOL W}}==
{{Trans|MAD}}
<langsyntaxhighlight algolwlang="oberon2">BEGIN
INTEGER PROCEDURE BITSP ( INTEGER VALUE BT ) ;
BEGIN
Line 493 ⟶ 549:
END DO WRITE( S_W := 0, I_W := 3, DPLBIT(N), ": ", I_W := 10, BITSP(DPLBIT(N)) )
END
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 530 ⟶ 586:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">↑(((⊂2∘⊥),⊂)(,⍨2∘⊥⍣¯1))¨⍳30</langsyntaxhighlight>
{{out}}
<pre> 3 1 1
Line 567 ⟶ 623:
Drawing members of the sequence from a non-finite list,
up to a given limit.
<langsyntaxhighlight lang="applescript">------ CONCATENATION OF TWO IDENTICAL BINARY STRINGS -----
 
-- binaryTwin :: Int -> (Int, String)
Line 752 ⟶ 808:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>3 -> 11
Line 786 ⟶ 842:
----
===Idiomatic===
<langsyntaxhighlight lang="applescript">on task(maxN)
set startWith0 to false -- Change to true to start with 0 and "00".
set rhv to -(startWith0 as integer) -- Start value of "right hand" string.
Line 820 ⟶ 876:
end task
 
task(999)</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"3: 11
10: 1010
15: 1111
Line 852 ⟶ 908:
924: 1110011100
957: 1110111101
990: 1111011110"</langsyntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">loop 0..1000 'i [
bin: as.binary i
if even? size bin [
half: (size bin)/2
if equal? slice bin 0 dec half
slice bin half dec size bin ->
print [pad to :string i 4 ":" bin]
]
]</syntaxhighlight>
 
{{out}}
 
<pre> 3 : 11
10 : 1010
15 : 1111
36 : 100100
45 : 101101
54 : 110110
63 : 111111
136 : 10001000
153 : 10011001
170 : 10101010
187 : 10111011
204 : 11001100
221 : 11011101
238 : 11101110
255 : 11111111
528 : 1000010000
561 : 1000110001
594 : 1001010010
627 : 1001110011
660 : 1010010100
693 : 1010110101
726 : 1011010110
759 : 1011110111
792 : 1100011000
825 : 1100111001
858 : 1101011010
891 : 1101111011
924 : 1110011100
957 : 1110111101
990 : 1111011110</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">n:=0
while (n++<=1000)
{
bin := LTrim(dec2bin(n), "0")
l := Strlen(bin)
if (l/2 <> Floor(l/2))
continue
if (SubStr(bin, 1, l/2) = SubStr(bin, l/2+1))
result .= n "`t" bin "`n"
}
MsgBox % result
return
 
Dec2Bin(i, s=0, c=0) {
l := StrLen(i := Abs(i + u := i < 0))
Loop, % Abs(s) + !s * l << 2
b := u ^ 1 & i // (1 << c++) . b
Return, b
}</syntaxhighlight>
{{out}}
<pre>3 11
10 1010
15 1111
36 100100
45 101101
54 110110
63 111111
136 10001000
153 10011001
170 10101010
187 10111011
204 11001100
221 11011101
238 11101110
255 11111111
528 1000010000
561 1000110001
594 1001010010
627 1001110011
660 1010010100
693 1010110101
726 1011010110
759 1011110111
792 1100011000
825 1100111001
858 1101011010
891 1101111011
924 1110011100
957 1110111101
990 1111011110</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f TWO_IDENTICAL_STRINGS.AWK
BEGIN {
Line 881 ⟶ 1,033:
return(str)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 916 ⟶ 1,068:
count: 30
</pre>
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT A-Z: DIM B(15)
20 N=0
30 N=N+1
Line 935 ⟶ 1,088:
170 NEXT I
180 PRINT
190 GOTO 30</langsyntaxhighlight>
{{out}}
<pre> 3 11
Line 967 ⟶ 1,120:
957 1110111101
990 1111011110</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">n = 1
k = 0
p = 2
 
while True
if n >= p then p += p
k = n + n * p
if k < 1000 then print k; " = "; tobinary(k) else end
n += 1
end while</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FUNCTION tobin$ (d)
s$ = ""
DO WHILE d <> 0
r = d MOD 2
s$ = STR$(r) + s$
d = d \ 2
LOOP
tobin$ = s$
END FUNCTION
 
k = 0 : n = 1 : p = 2
DO
IF n >= p THEN p = p + p
k = n + n * p
IF k < 1000 THEN
PRINT k; " = "; tobin$(k)
ELSE
EXIT DO
END IF
n = n + 1
LOOP</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
n.i = 1
k.i = 0
p.i= 2
 
While #True
If n >= p
p + p
EndIf
k = n + n * p
If k < 1000
PrintN(Str(k) + " = " + Bin(k))
Else
Break
EndIf
n + 1
Wend
 
Input()
CloseConsole()
</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION tobin$(d)
LET s$ = ""
DO WHILE d <> 0
LET r = REMAINDER(ROUND(d),2)
LET s$ = STR$(r) & s$
LET d = IP(ROUND(d)/2)
LOOP
LET tobin$ = s$
END FUNCTION
 
LET n = 1
LET k = 0
LET p = 2
DO
IF n >= p THEN LET p = p+p
LET k = n+n*p
IF k < 1000 THEN
PRINT k; " = "; tobin$(k)
ELSE
EXIT DO
END IF
LET n = n+1
LOOP
END</syntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let bitlength(n) = n=0 -> 0, 1 + bitlength(n >> 1)
Line 988 ⟶ 1,227:
conc := concat(n,n)
$)
$)</langsyntaxhighlight>
{{out}}
<pre> 3: 11
Line 1,022 ⟶ 1,261:
 
=={{header|Befunge}}==
<langsyntaxhighlight lang="befunge">1>:::>:#v_++:91+v
>^ / >\vv**::<
^2\*2<>`#@_v
Line 1,030 ⟶ 1,269:
1:^
$!,
^_^</langsyntaxhighlight>
{{out}}
<pre>3 11
Line 1,064 ⟶ 1,303:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdint.h>
 
Line 1,093 ⟶ 1,332:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,130 ⟶ 1,369:
=={{header|C#|CSharp}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="csharp">using System; using static System.Console;
class Program { static void Main() { int c = 0, lmt = 1000;
for (int n = 1, p = 2, k; n <= lmt; n++)
Line 1,137 ⟶ 1,376:
Convert.ToString(k, 2), ++c % 5 == 0 ? "\n" : "");
Write("\nFound {0} numbers whose base 2 representation is the " +
"concatenation of two identical binary strings.", c); } }</langsyntaxhighlight>
{{out}}
Same as Visual Basic. NET
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <string>
 
Line 1,169 ⟶ 1,408:
base2_increment(s);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,205 ⟶ 1,444:
990 1111011110
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">nth_ident = proc (n: int) returns (int)
h: int := n
l: int := n
while l>0 do h, l := h*2, l/2 end
return(h + n)
end nth_ident
 
bits = proc (n: int) returns (string)
p: string := ""
if n>1 then p := bits(n/2) end
return(p || int$unparse(n//2))
end bits
 
start_up = proc ()
po: stream := stream$primary_output()
n: int := 0
while true do
n := n+1
ident: int := nth_ident(n)
if ident>=1000 then break end
stream$putright(po, int$unparse(ident), 3)
stream$putl(po, ": " || bits(ident))
end
end start_up</syntaxhighlight>
{{out}}
<pre> 3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. IDENTICAL-STRINGS.
 
Line 1,271 ⟶ 1,567:
COMPUTE BIT-VALUE = 2 ** (10 - CURRENT-BIT)
MULTIPLY BIT(CURRENT-BIT) BY BIT-VALUE.
ADD BIT-VALUE TO OUTPUT-NUMBER.</langsyntaxhighlight>
{{out}}
<pre> 3: 11
Line 1,303 ⟶ 1,599:
957: 1110111101
990: 1111011110</pre>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub bitLength(n: uint32): (l: uint8) is
Line 1,340 ⟶ 1,637:
print_nl();
n := n + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 1,374 ⟶ 1,671:
957: 1110111101
990: 1111011110</pre>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">(0..1000).each do |i|
bin = i.to_s(2)
if bin.size.even?
half = bin.size // 2
if bin[0..half-1] == bin[half..]
print "%3d: %10s\n" % [i, bin]
end
end
end</syntaxhighlight>
{{out}}
<pre> 3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function IntToBinStr(IValue: Int64) : string;
{Convert integer to binary string, with no leading zero}
var I: integer;
begin
Result:='';
I:=IntPower2(32-1);
while I <> 0 do
begin
if (IValue and I)<>0 then Result:=Result + '1'
else if Length(Result)>0 then Result:=Result + '0';
I:=I shr 1;
end;
if Result='' then Result:='0';
end;
 
 
procedure IdenticalBinaryStrings(Memo: TMemo);
var S,S1,S2: string;
var Len,I: integer;
begin
for I:=2 to 1000-1 do
begin
{Get binary String}
S:=IntToBinStr(I);
{Only look at string evenly divisible by 2}
Len:=Length(S);
if (Len and 1)=0 then
begin
{Split string into equal pieces}
S1:=LeftStr(S,Len div 2);
S2:=RightStr(S,Len div 2);
{Each half should be the same}
if S1=S2 then Memo.Lines.Add(IntToStr(I)+': '+S);
end;
end;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110
 
Elapsed Time: 58.641 ms.
 
</pre>
 
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec concat_self(word n) word:
word rslt, k;
k := n;
rslt := n;
while k ~= 0 do
k := k >> 1;
rslt := rslt << 1
od;
rslt | n
corp
 
proc nonrec main() void:
word n, conc;
n := 1;
while
conc := concat_self(n);
conc < 1000
do
writeln(conc:3, ": ", conc:b:10);
n := n + 1
od
corp</syntaxhighlight>
{{out}}
<pre> 3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110</pre>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
func$ tobin k .
if k > 0
return tobin (k div 2) & k mod 2
.
.
p = 2
repeat
n += 1
if n >= p
p += p
.
k = n + n * p
until k >= 1000
print k & ": " & tobin k
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Nigel Galloway. April 5th., 2021
let fN g=let rec fN g=function n when n<2->(char(n+48))::g |n->fN((char(n%2+48))::g)(n/2) in fN [] g|>Array.ofList|>System.String
Seq.unfold(fun(n,g,l)->Some((n<<<l)+n,if n=g-1 then (n+1,g*2,l+1) else (n+1,g,l)))(1,2,1)|>Seq.takeWhile((>)1000)|>Seq.iter(fun g->printfn "%3d %s" g (fN g))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,414 ⟶ 1,914:
990 1111011110
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: formatting kernel lists lists.lazy math math.parser
sequences ;
 
1 lfrom [ >bin dup append bin> ] lmap-lazy [ 1000 < ] lwhile
[ dup "%d %b\n" printf ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 1,456 ⟶ 1,957:
 
=={{header|FALSE}}==
<langsyntaxhighlight FALSElang="false">1[$$$[$][2/\2*\]#%|$1000>~][
$.": "
0\10\[$1&'0+\2/$][]#%
[$][,]#%
1+
]#%%</langsyntaxhighlight>
 
{{out}}
Line 1,496 ⟶ 1,997:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.10 S N=0
01.20 S N=N+1
01.30 D 3
Line 1,523 ⟶ 2,024:
04.30 I (B(I))4.4,4.5,4.4
04.40 T "1"
04.50 T "0"</langsyntaxhighlight>
 
{{out}}
Line 1,559 ⟶ 2,060:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: concat-self
dup dup
begin dup while
Line 1,586 ⟶ 2,087:
;
 
to1000 bye</langsyntaxhighlight>
 
{{out}}
Line 1,622 ⟶ 2,123:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran"> program IdentStr
implicit none
integer n, concat, bits
Line 1,660 ⟶ 2,161:
goto 100
end if
end</langsyntaxhighlight>
{{out}}
<pre> 3 11
Line 1,694 ⟶ 2,195:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as uinteger n=1, k=0
do
k = n + 2*n*2^int(log(n)/log(2))
if k<1000 then print k, bin(k) else end
n=n+1
loop</langsyntaxhighlight>
{{out}}
<pre>3 11
Line 1,734 ⟶ 2,235:
===Alternate===
No ''log()'' function required.
<langsyntaxhighlight lang="freebasic">dim as uinteger n = 1, k = 0, p = 2
do
if n >= p then p = p + p
Line 1,740 ⟶ 2,241:
if k < 1000 then print k, bin(k) else end
n = n + 1
loop</langsyntaxhighlight>
{{out}}
Same as ''log()'' version.
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">for n = 1 to 999
if base2[n] =~ %r/^(\d+)\1$/
println["$n\t" + base2[n]]</syntaxhighlight>
{{out}}
<pre>
3 11
10 1010
15 1111
36 100100
45 101101
54 110110
63 111111
136 10001000
153 10011001
170 10101010
187 10111011
204 11001100
221 11011101
238 11101110
255 11111111
528 1000010000
561 1000110001
594 1001010010
627 1001110011
660 1010010100
693 1010110101
726 1011010110
759 1011110111
792 1100011000
825 1100111001
858 1101011010
891 1101111011
924 1110011100
957 1110111101
990 1111011110
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
defstr byte
 
NSUInteger n = 1, k = 0, p = 2
 
while (1)
if n >= p then p += p
k = n + n * p
if k < 1000 then printf @"%4lu %@", k, bin(k) else exit while
n++
wend
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
3 00000011
10 00001010
15 00001111
36 00100100
45 00101101
54 00110110
63 00111111
136 10001000
153 10011001
170 10101010
187 10111011
204 11001100
221 11011101
238 11101110
255 11111111
528 00010000
561 00110001
594 01010010
627 01110011
660 10010100
693 10110101
726 11010110
759 11110111
792 00011000
825 00111001
858 01011010
891 01111011
924 10011100
957 10111101
990 11011110
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,766 ⟶ 2,354:
}
fmt.Println("\nFound", i-1, "numbers.")
}</langsyntaxhighlight>
 
{{out}}
Line 1,806 ⟶ 2,394:
=={{header|Haskell}}==
===Data.Bits===
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bits
( countLeadingZeros,
Line 1,834 ⟶ 2,422:
map (join $ printf "%d: %b") to1000
where
to1000 = takeWhile (<= 1000) identStrInts</langsyntaxhighlight>
 
{{out}}
Line 1,871 ⟶ 2,459:
===Data.Char===
As an alternative to Data.Bits, we could also express this in terms of Data.Char
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Char (digitToInt, intToDigit)
import Numeric (showIntAtBase)
Line 1,899 ⟶ 2,487:
(\c (e, n) -> (2 * e, digitToInt c * e + n))
(1, 0)
s</langsyntaxhighlight>
<pre>(3,"11")
(10,"1010")
Line 1,932 ⟶ 2,520:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">(":,': ',":@#:)@(,~&.#:)"0 (>:i.30)</langsyntaxhighlight>
{{out}}
<pre>3: 1 1
Line 1,966 ⟶ 2,554:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class TwoIdenticalStrings {
public static void main(String[] args) {
System.out.println("Decimal Binary");
Line 1,979 ⟶ 2,567:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Decimal Binary
Line 2,016 ⟶ 2,604:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
def binary_digits:
if . == 0 then 0
Line 2,032 ⟶ 2,620:
| select(.[$half:] == .[:$half])
| [$i, .]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,065 ⟶ 2,653:
[957,"1110111101"]
[990,"1111011110"]</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function twoidenticalstringsinbase(base, maxnum, verbose=true)
found = Int[]
for i in 1:maxnum
Line 2,084 ⟶ 2,673:
twoidenticalstringsinbase(2, 999)
twoidenticalstringsinbase(16, 999)
</langsyntaxhighlight>{{out}}
<pre>
Decimal Base 2
Line 2,137 ⟶ 2,726:
 
=== Generator version ===
<langsyntaxhighlight lang="julia">function twoidenticalstringsinbase(base, mx, verbose = true)
gen = filter(x -> x < mx,
reduce(vcat, [[i * (base^d + 1) for i in base^(d-1):base^d-1] for d in 1:ndigits(mx; base) ÷ 2]))
Line 2,149 ⟶ 2,738:
twoidenticalstringsinbase(2, 999)
twoidenticalstringsinbase(16, 999)
</langsyntaxhighlight>{{out}}<pre>Same as filter version above.</pre>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang ="liberty basic">'Not testing 1 since it only has onemaxNumber binary= digit1000
maxN = Int(Len(DecToBin$(maxNumber))/ 2)
For i = 2 To 1000
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
'Only test those binary numbers evenly
Print (((2^Len(bin$))*i) + i),Space$((maxN * 2) - Len(bin$;bin$));bin$;bin$
'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
Line 2,169 ⟶ 2,756:
Function DecToBin$(decNum)
While decNum
RDecToBin$ = (decNum Mod 2);DecToBin$
DecToBin$ = R;DecToBin$
decNum = Int(decNum/ 2)
Wend
If (DecToBin$ = "") Then DecToBin$ = "0"
End Function</langsyntaxhighlight>
 
{{out}}
 
<pre>3Value - 11 Binary
3 11
10 - 1010
10 1010
15 - 1111
15 1111
36 - 100100
36 100100
45 - 101101
45 101101
54 - 110110
54 110110
63 - 111111
63 111111
136 - 10001000
136 10001000
153 - 10011001
153 10011001
170 - 10101010
170 10101010
187 - 10111011
187 10111011
204 - 11001100
204 11001100
221 - 11011101
221 11011101
238 - 11101110
238 11101110
255 - 11111111
255 11111111
528 - 1000010000
528 1000010000
561 - 1000110001
561 1000110001
594 - 1001010010
594 1001010010
627 - 1001110011
627 1001110011
660 - 1010010100
660 1010010100
693 - 1010110101
693 1010110101
726 - 1011010110
726 1011010110
759 - 1011110111
759 1011110111
792 - 1100011000
792 1100011000
825 - 1100111001
825 1100111001
858 - 1101011010
858 1101011010
891 - 1101111011
891 1101111011
924 - 1110011100
924 1110011100
957 - 1110111101
957 1110111101
990 - 1111011110</pre>
990 1111011110</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE IDNSTR
.MCALL .TTYOUT,.EXIT
IDNSTR::CLR R4
BR 2$
1$: MOV R3,R0
JSR PC,PRDEC
MOV R3,R0
JSR PC,PRBIN
2$: INC R4
JSR PC,IDENT
CMP R3,#^D1000
BLT 1$
.EXIT
 
; LET R3 BE R4'TH IDENTICAL NUMBER
IDENT: MOV R4,R3
MOV R4,R2
1$: ASL R3
ASR R2
BNE 1$
BIS R4,R3
RTS PC
 
; PRINT NUMBER IN R0 AS DECIMAL
PRDEC: MOV #4$,R1
1$: MOV #-1,R2
2$: INC R2
SUB #12,R0
BCC 2$
ADD #72,R0
MOVB R0,-(R1)
MOV R2,R0
BNE 1$
3$: MOVB (R1)+,R0
.TTYOUT
BNE 3$
RTS PC
.ASCII /...../
4$: .BYTE 11,0
.EVEN
 
; PRINT NUMBER IN R0 AS BINARY
PRBIN: MOV #3$,R1
1$: MOV #60,R2
ASR R0
ADC R2
MOVB R2,-(R1)
TST R0
BNE 1$
2$: MOVB (R1)+,R0
.TTYOUT
BNE 2$
RTS PC
.BLKB 20
3$: .BYTE 15,12,0
.END IDNSTR</syntaxhighlight>
{{out}}
<pre>3 11
10 1010
15 1111
36 100100
45 101101
54 110110
63 111111
136 10001000
153 10011001
170 10101010
187 10111011
204 11001100
221 11011101
238 11101110
255 11111111
528 1000010000
561 1000110001
594 1001010010
627 1001110011
660 1010010100
693 1010110101
726 1011010110
759 1011110111
792 1100011000
825 1100111001
858 1101011010
891 1101111011
924 1110011100
957 1110111101
990 1111011110</pre>
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(BT)
Line 2,243 ⟶ 2,918:
 
VECTOR VALUES NFMT = $I3,2H: ,I10*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre> 3: 11
Line 2,260 ⟶ 2,935:
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">max = 1000;
maxbin = Ceiling[Ceiling[Log2[max]]/2];
s = Table[
id = IntegerDigits[i, 2];
s = Join[id, id];
{FromDigits[s, 2], StringJoin[ToString /@ s]}
,
{i, 2^maxbin - 1}
];
Select[s, First/*LessThan[1000]] // Grid</syntaxhighlight>
{{out}}
<pre>3 11
10 1010
15 1111
36 100100
45 101101
54 110110
63 111111
136 10001000
153 10011001
170 10101010
187 10111011
204 11001100
221 11011101
238 11101110
255 11111111
528 1000010000
561 1000110001
594 1001010010
627 1001110011
660 1010010100
693 1010110101
726 1011010110
759 1011110111
792 1100011000
825 1100111001
858 1101011010
891 1101111011
924 1110011100
957 1110111101
990 1111011110</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map display (takewhile (< 1000) identicals)))]
where display n = shownum n ++ ": " ++ bits n
 
bits :: num->[char]
bits = bits' []
where bits' acc 0 = acc
bits' acc n = bits' (decode (48 + n mod 2) : acc) (n div 2)
 
identicals :: [num]
identicals = map identical [1..]
 
identical :: num->num
identical n = n + n * 2^size n
 
size :: num->num
size 0 = 0
size n = 1 + size (n div 2)</syntaxhighlight>
{{out}}
<pre>3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE IdenticalStrings;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
 
VAR n: CARDINAL;
 
PROCEDURE identical(n: CARDINAL): CARDINAL;
VAR shiftL, shiftR: CARDINAL;
BEGIN
shiftL := n;
shiftR := n;
WHILE shiftR > 0 DO
shiftL := shiftL * 2;
shiftR := shiftR DIV 2;
END;
RETURN shiftL + n;
END identical;
 
PROCEDURE WriteBits(n: CARDINAL);
BEGIN
IF n>1 THEN WriteBits(n DIV 2); END;
WriteCard(n MOD 2, 1);
END WriteBits;
 
BEGIN
n := 1;
WHILE identical(n) < 1000 DO
WriteCard(identical(n), 3);
WriteString(": ");
WriteBits(identical(n));
WriteLn;
INC(n);
END;
END IdenticalStrings.</syntaxhighlight>
{{out}}
<pre> 3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
Line 2,277 ⟶ 3,112:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
func isConcat(s: string): bool =
Line 2,286 ⟶ 3,121:
for n in 0..999:
let b = &"{n:b}"
if b.isConcat: echo &"{n:3} {b}"</langsyntaxhighlight>
 
{{out}}
Line 2,319 ⟶ 3,154:
957 1110111101
990 1111011110</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec bin_of_int = function
| n when n < 2 -> string_of_int n
| n -> Printf.sprintf "%s%u" (bin_of_int (n lsr 1)) (n land 1)
 
let seq_task =
let rec next n l m () =
if n = l
then next n (l + l) (succ (l + l)) ()
else Seq.Cons (n * m, next (succ n) l m)
in next 1 2 3
 
let () =
let show n = Printf.printf "%u: %s\n" n (bin_of_int n) in
seq_task |> Seq.take_while ((>) 1000) |> Seq.iter show</syntaxhighlight>
{{out}}
<pre>
3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110
</pre>
 
=={{header|Pascal}}==
{{works with|Turbo Pascal}}
<langsyntaxhighlight lang="pascal">program IdenticalStrings;
const
LIMIT = 1000;
Line 2,367 ⟶ 3,251:
n := n + 1;
end;
end.</langsyntaxhighlight>
 
{{out}}
Line 2,403 ⟶ 3,287:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">identstr: procedure options(main);
bitLength: procedure(nn) returns(fixed);
declare (n, nn, r) fixed;
Line 2,440 ⟶ 3,324:
call printBits(concat(n,n));
end;
end identstr;</langsyntaxhighlight>
{{out}}
<pre> 3 11
Line 2,474 ⟶ 3,358:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
 
/* BINARY CONCATENATION OF A NUMBER WITH ITSELF */
Line 2,525 ⟶ 3,409:
 
CALL BDOS(0,0);
EOF</langsyntaxhighlight>
{{out}}
<pre>3 11
Line 2,560 ⟶ 3,444:
=={{header|Python}}==
===Python: Procedural===
<langsyntaxhighlight lang="python">def bits(n):
"""Count the amount of bits required to represent n"""
r = 0
Line 2,575 ⟶ 3,459:
while concat(n) <= 1000:
print("{0}: {0:b}".format(concat(n)))
n += 1</langsyntaxhighlight>
 
{{out}}
Line 2,614 ⟶ 3,498:
 
Values are drawn, up to a limit, from a non-finite list.
<langsyntaxhighlight lang="python">'''Two identical strings'''
 
from itertools import count, takewhile
Line 2,647 ⟶ 3,531:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>(3, '11')
Line 2,681 ⟶ 3,565:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Two_identical_strings
Line 2,691 ⟶ 3,575:
(my $decimal = oct "b$binary") >= 1000 and last;
printf "%4d %s\n", $decimal, $binary;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,727 ⟶ 3,611:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 2,738 ⟶ 3,622:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d numbers:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->{{out}}
<pre>
Found 30 numbers:
Line 2,747 ⟶ 3,631:
45 101101 170 10101010 255 11111111 660 1010010100 825 1100111001 990 1111011110
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">main =>
bp.length(_L,I),
I > 0,
B = to_binary_string(I),
BB = B++B,
parse_radix_string(BB,2) = Dec,
( Dec < 1000 -> printf("%4w %10w\n",Dec,BB), fail ; true),
nl.</syntaxhighlight>
 
{{out}}
<pre> 3 11
10 1010
15 1111
36 100100
45 101101
54 110110
63 111111
136 10001000
153 10011001
170 10101010
187 10111011
204 11001100
221 11011101
238 11101110
255 11111111
528 1000010000
561 1000110001
594 1001010010
627 1001110011
660 1010010100
693 1010110101
726 1011010110
759 1011110111
792 1100011000
825 1100111001
858 1101011010
891 1101111011
924 1110011100
957 1110111101
990 1111011110</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">main:-
writeln('Decimal\tBinary'),
main(1, 1000).
Line 2,763 ⟶ 3,689:
N1 is N + 1,
main(N1, Limit).
main(_, _).</langsyntaxhighlight>
 
{{out}}
Line 2,801 ⟶ 3,727:
 
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ 2 base put
 
<lang Quackery> [ 2 base put
number$ dup size 2 / split =
base release ] is 2identical ( n --> b )
Line 2,811 ⟶ 3,736:
2 base put
i^ echo cr
base release ] ] </langsyntaxhighlight>
 
{{out}}
Line 2,848 ⟶ 3,773:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my @cat = (1..*).map: { :2([~] .base(2) xx 2) };
say "{+$_} matching numbers\n{.batch(5)».map({$_ ~ .base(2).fmt('(%s)')})».fmt('%15s').join: "\n"}\n"
given @cat[^(@cat.first: * > 1000, :k)];</langsyntaxhighlight>
{{out}}
<pre>30 matching numbers
Line 2,860 ⟶ 3,785:
858(1101011010) 891(1101111011) 924(1110011100) 957(1110111101) 990(1111011110)</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <IdentUpTo 1000>;
};
 
IdentUpTo {
s.M = <IdentUpTo s.M 1>;
s.M s.I, <Ident s.I>: s.Id, <Compare s.Id s.M>: {
'-' = <Prout <Symb s.Id> ': ' <Bin s.Id>>
<IdentUpTo s.M <+ s.I 1>>;
s.X = ;
};
};
 
Ident {
s.N = <Ident s.N s.N s.N>;
s.N s.R 0 = <+ s.N s.R>;
s.N s.R s.K = <Ident s.N <* s.R 2> <Div s.K 2>>;
};
 
Bin {
0 = ;
s.N, <Divmod s.N 2>: (s.X) s.Y = <Bin s.X> <Symb s.Y>;
};</syntaxhighlight>
{{out}}
<pre>3: 11
10: 1010
15: 1111
36: 100100
45: 101101
54: 110110
63: 111111
136: 10001000
153: 10011001
170: 10101010
187: 10111011
204: 11001100
221: 11011101
238: 11101110
255: 11111111
528: 1000010000
561: 1000110001
594: 1001010010
627: 1001110011
660: 1010010100
693: 1010110101
726: 1011010110
759: 1011110111
792: 1100011000
825: 1100111001
858: 1101011010
891: 1101111011
924: 1110011100
957: 1110111101
990: 1111011110</pre>
=={{header|REXX}}==
<syntaxhighlight lang="rexx">
=== sans formatting ===
/*REXX
<lang>/*REXX program finds/displays decimal numbers whose binary version is a doubled literal.*/
numeric * program finds/displays decimal numbers digits 20 /*ensure 'nuff dec. digs for conversion*/
* whose binary version is a doubled literal.
do #=1 for 1000-1; b= x2b( d2x(#) ) + 0 /*find binary values that can be split.*/
*/
L= length(b); if L//2 then iterate /*get length of binary; if odd, skip. */
numeric digits 20 /*ensure 'nuff dec. digs for conversion*/
if left(b, L%2)\==right(b, L%2) then iterate /*Left half ≡ right half? No, skip it.*/
do i=1 to 1000
say right(#, 4)':' right(b, 12) /*display number in decimal and binary.*/
b= x2b( d2x(i) ) + 0 /*find binary values that can be split.*/
end /*#*/ /*stick a fork in it, we're all done. */</lang>
L= length(b)
if L//2 then iterate /*get length of binary; if odd, skip. */
if left(b, L%2)\==right(b, L%2) then iterate /*Left half ≡ right half?*/
say right(i, 4)':' right(b, 12) /*display number in dec and bin */
end /*i*/ /*stick a fork in it, we're all done. */
*/</syntaxhighlight>
{{out|output|text=&nbsp; (shown at three-quarter size.)}}
<pre style="font-size:75%">
Line 2,904 ⟶ 3,890:
 
=== with formatting ===
<langsyntaxhighlight lang="rexx">/*REXX program finds/displays decimal numbers whose binary version is a doubled literal.*/
numeric digits 100 /*ensure hangling of larger integers. */
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 2,935 ⟶ 3,921:
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,954 ⟶ 3,940:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">load "stdlib.ring"
decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
Line 3,007 ⟶ 3,993:
s = string(x) l = len(s)
if l > y y = l ok
return substr(" ", 11 - y + l) + s</langsyntaxhighlight>
{{out}}
<pre>working...
Line 3,020 ⟶ 4,006:
Found 30 numbers whose base 2 representation is the juxtaposition of two identical strings
done...</pre>
 
=={{header|RPL}}==
===Slow version===
Binary versions of numbers from 1 to 999 are converted into strings to check the half-half identity.
{{works with|Halcyon Calc|4.2.8}}
≪ R→B →STR 3 OVER SIZE 1 - SUB
'''IF''' DUP SIZE 2 MOD '''THEN''' DROP 0
'''ELSE'''
1 OVER SIZE 2 / SUB
LAST SWAP DROP 1 + OVER SIZE SUB ==
'''END'''
≫ ‘'''TWIN?'''' STO
≪ BIN { } 1 999 FOR n
'''IF''' n '''TWIN?'''
'''THEN''' n →STR "=" +
n R→B →STR 2 OVER SIZE 1 - SUB + + '''END NEXT'''
≫ ''''TASK'''' STO
{{out}}
<pre>
1: { "3= 11" "10= 1010" "15= 1111" "36= 100100" "45= 101101" "54= 110110" "63= 111111" "136= 10001000" "153= 10011001" "170= 10101010" "187= 10111011" "204= 11001100" "221= 11011101" "238= 11101110" "255= 11111111" "528= 1000010000" "561= 1000110001" "594= 1001010010" "627= 1001110011" "660= 1010010100" "693= 1010110101" "726= 1011010110" "759= 1011110111" "792= 1100011000" "825= 1100111001" "858= 1101011010" "891= 1101111011" "924= 1110011100" "957= 1110111101" "990= 1111011110" }
</pre>
Runs on an HP-28S in 130 seconds.
 
=== Optimized version===
{{trans|FreeBASIC}}
40% of the code is dedicated to the display of the results.
{{works with|Halcyon Calc|4.2.8}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ BIN { }
2 1
'''DO'''
'''IF''' DUP2 ≤ '''THEN''' OVER ROT + SWAP '''END'''
DUP2 * OVER +
4 ROLL OVER DUP →STR "=" +
SWAP R→B →STR 2 OVER SIZE 1 - SUB + + 4 ROLLD
SWAP 1 +
'''UNTIL''' SWAP 1000 ≥ '''END''' DROP2
≫ ''''TASK'''' STO
|
'''TASK''' ''( -- { "results" } )''
n = 1, p = 2
do
if n >= p then p = p + p
k = n + n * p
print "k= ";
print "bin(k)"
n = n + 1
loop until k ≥ 1000
|}
Runs on an HP-28S in 6 seconds.
 
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">for i in 0 .. 1000
bin = i.to_s(2)
if bin.length % 2 == 0 then
Line 3,031 ⟶ 4,073:
end
end
end</langsyntaxhighlight>
{{out}}
<pre> 3: 11
Line 3,063 ⟶ 4,105:
957: 1110111101
990: 1111011110</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program identical_strings;
loop init i := 0; doing n := ident(i +:= 1); while n<1000 do
print(lpad(str n, 4) + lpad(binary(n), 15));
end loop;
 
proc ident(n);
ns := n;
loop init t := n; doing t div:= 2; until t = 0 do
ns *:= 2;
end loop;
return ns + n;
end proc;
 
proc binary(n);
return {[0,""]}(n) ? binary(n div 2) + str (n mod 2);
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> 3 11
10 1010
15 1111
36 100100
45 101101
54 110110
63 111111
136 10001000
153 10011001
170 10101010
187 10111011
204 11001100
221 11011101
238 11101110
255 11111111
528 1000010000
561 1000110001
594 1001010010
627 1001110011
660 1010010100
693 1010110101
726 1011010110
759 1011110111
792 1100011000
825 1100111001
858 1101011010
891 1101111011
924 1110011100
957 1110111101
990 1111011110</pre>
 
=={{header|Snobol}}==
<langsyntaxhighlight lang="snobol"> define("bits(n)") :(bits_end)
bits bits = gt(n,0) remdr(n,2) bits :f(return)
n = n / 2 :(bits)
Line 3,082 ⟶ 4,174:
m = concat(n)
output = lt(m,1000) m ": " bits(m) :s(loop)
end</langsyntaxhighlight>
{{out}}
<pre>3: 11
Line 3,116 ⟶ 4,208:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">print("Decimal\tBinary")
var n = 1
while (true) {
Line 3,126 ⟶ 4,218:
print("\(i)\t\(binary)\(binary)")
n += 1
}</langsyntaxhighlight>
 
{{out}}
Line 3,165 ⟶ 4,257:
=={{header|Visual Basic .NET}}==
{{trans|FreeBASIC}}Based on the Alternate version.
<langsyntaxhighlight lang="vbnet">Imports System.Console
Module Module1
Sub Main()
Line 3,176 ⟶ 4,268:
Next : WriteLine(vbLf + "Found {0} numbers whose base 2 representation is the concatenation of two identical binary strings.", c)
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre> 3 (11 ) 10 (1010 ) 15 (1111 ) 36 (100100 ) 45 (101101 )
Line 3,186 ⟶ 4,278:
 
Found 30 numbers whose base 2 representation is the concatenation of two identical binary strings.</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import strconv
fn main() {
mut i := i64(1)
for {
mut b2 := '${i:b}'
b2 += b2
d := strconv.parse_int(b2,2,16) ?
if d >= 1000 {
break
}
println("$d : $b2")
i++
}
println("\nFound ${i-1} numbers.")
}</syntaxhighlight>
 
{{out}}
<pre>
3 : 11
10 : 1010
15 : 1111
36 : 100100
45 : 101101
54 : 110110
63 : 111111
136 : 10001000
153 : 10011001
170 : 10101010
187 : 10111011
204 : 11001100
221 : 11011101
238 : 11101110
255 : 11111111
528 : 1000010000
561 : 1000110001
594 : 1001010010
627 : 1001110011
660 : 1010010100
693 : 1010110101
726 : 1011010110
759 : 1011110111
792 : 1100011000
825 : 1100111001
858 : 1101011010
891 : 1101111011
924 : 1110011100
957 : 1110111101
990 : 1111011110
 
Found 30 numbers.
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
var i = 1
Line 3,200 ⟶ 4,347:
i = i + 1
}
System.print("\nFound %(i-1) numbers.")</langsyntaxhighlight>
 
{{out}}
Line 3,239 ⟶ 4,386:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc BinOut(N); \Output N in binary
int N, Rem;
[Rem:= N&1;
Line 3,260 ⟶ 4,407:
];
];
]</langsyntaxhighlight>
 
{{out}}
Line 3,295 ⟶ 4,442:
990: 1111011110
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Two_identical_strings
// by Galileo, 04/2022
 
for n = 1 to 1000
n$ = bin$(n)
if not mod(len(n$), 2) then
k = len(n$) / 2
if left$(n$, k) = right$(n$, k) print n, " = ", n$
endif
next</syntaxhighlight>
{{out}}
<pre>3 = 11
10 = 1010
15 = 1111
36 = 100100
45 = 101101
54 = 110110
63 = 111111
136 = 10001000
153 = 10011001
170 = 10101010
187 = 10111011
204 = 11001100
221 = 11011101
238 = 11101110
255 = 11111111
528 = 1000010000
561 = 1000110001
594 = 1001010010
627 = 1001110011
660 = 1010010100
693 = 1010110101
726 = 1011010110
759 = 1011110111
792 = 1100011000
825 = 1100111001
858 = 1101011010
891 = 1101111011
924 = 1110011100
957 = 1110111101
990 = 1111011110
---Program done, press RETURN---</pre>
2,094

edits