Two identical strings: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Visual Basic .NET version, translation of FreeBASIC Alternate version)
Line 1,260: Line 1,260:
957: 1110111101
957: 1110111101
990: 1111011110</pre>
990: 1111011110</pre>

=={{header|Perl}}==
<lang perl>#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Two_identical_strings
use warnings;

while( 1 )
{
my $binary = ( sprintf "%b", ++$- ) x 2;
(my $decimal = oct "b$binary") >= 1000 and last;
printf "%4d %s\n", $decimal, $binary;
}</lang>
{{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|Phix}}==
=={{header|Phix}}==

Revision as of 19:37, 4 April 2021

Two identical strings is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Find and display   (here on this page)   positive integers whose base 2 representation is the concatenation of two identical binary strings,
where   n   (in base ten)   <   1,00010       (one thousand).

For each decimal number,   show its decimal form and also its binary form.

8080 Assembly

<lang 8080asm> ;;; Print positive integers whose base-2 representation ;;; is the concatenation of two identical binary strings, ;;; for 1 < n < 1000 puts: equ 9 ; CP/M syscall to print a string org 100h lxi b,1 ; Counter loop: mov h,b ; HL = counter mov l,c call concat ; Get current concatenated number lxi d,1000 ; Reached the end yet? call cmp16 rnc ; Stop when >1000 push b ; Keep the counter push h ; And the concatenated number call hldec ; Print decimal value pop h ; Restore number call hlbin ; Print binary value lxi d,nl ; Print newline mvi c,puts call 5 pop b ; Restore counter inx b ; Increment counter jmp loop ;;; 16-bit compare HL to DE cmp16: mov a,h cmp d rnz mov a,l cmp e ret ;;; Concatenate HL with itself concat: push h ; Keep a copy of HL on the stack mov d,h ; DE = copy of HL mov e,l ctloop: mov a,d ; When DE=0, we are done ora e jz ctdone mov a,d ; Rotate DE left rar mov d,a mov a,e rar mov e,a dad h ; And rotate HL right (add to itself) jmp ctloop ctdone: pop d ; Retrieve old HL dad d ; Add to shifted version (same as OR) ret ;;; Print HL as a decimal value hldec: lxi d,outbuf push d ; Output pointer on the stack lxi b,-10 ; Divisor decdgt: lxi d,-1 ; Quotient div10: inx d ; Divide HL by 10 using trial subtraction dad b jc div10 mvi a,'0'+10 add l ; L contains remainder - 10 pop h ; Retrieve output pointer dcx h ; Store digit mov m,a push h xchg ; Continue with quotient mov a,h ; If any digits left ora l jnz decdgt ; Find the next digits pop d ; Otherwise, retrieve pointer mvi c,puts ; And print result using CP/M jmp 5 ;;; Print HL as a binary value hlbin: lxi d,outbuf ora a ; Zero the carry flag bindgt: mov a,h ; Rotate HL right rar mov h,a mov a,l rar mov l,a mvi a,0 ; A = '0' + carry flag (i.e. lowest bit) aci '0' dcx d ; Store digit stax d mov a,h ; Any more digits? ora l jnz bindgt ; If so, find next digits mvi c,puts ; Otherwise, print the result jmp 5 db '***********' outbuf: db 9,'$' nl: db 13,10,'$'</lang>

Output:
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

8086 Assembly

<lang asm>puts: equ 9 cpu 8086 org 100h section .text main: mov di,1 ; Counter .loop: mov ax,di call concat ; Concatenate current number to itself cmp ax,1000 jge .done ; Stop when >= 1000 mov si,ax ; Keep a copy of AX call pdec ; Print decimal value mov ax,si call pbin ; Print binary value mov bx,nl ; Print newline call pstr inc di ; Next number jmp .loop .done: ret ;;; Concatenate AX to itself concat: mov bx,ax ; Store a copy of AX in BP mov cx,ax ; Store a copy of AX in CX .loop: shl ax,1 ; Shift AX left shr cx,1 ; Shift CX right jnz .loop ; Keep going until CX is zero or ax,bx ; OR original AX with shifted AX ret ;;; Print AX as decimal pdec: mov bp,10 ; Divisor mov bx,outbuf ; Buffer pointer .loop: xor dx,dx div bp add dl,'0' ; Add '0' to remainder dec bx ; Store digit mov [bx],dl test ax,ax ; Any more digits? jnz .loop jmp pstr ; When done, print the result ;;; Print AX as binary pbin: mov bx,outbuf ; Buffer pointer .loop: shr ax,1 ; Shift AX mov dl,'0' ; ASCII 0 or 1 adc dl,0 dec bx mov [bx],dl ; Store digit test ax,ax jnz .loop pstr: mov ah,puts ; When done, print the result mov dx,bx int 21h ret section .data nl: db 13,10,'$' db '****************' outbuf: db 9,'$'</lang>

Output:
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

ALGOL 68

<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 #
   OP TOBINSTRING = ( INT v )STRING:
      IF v = 0 THEN "0"
      ELSE
           STRING result := "";
           INT rest := v;
           WHILE rest > 0 DO
               IF ODD rest THEN "1" ELSE "0" FI +=: result;
               rest OVERAB 2
           OD;
           result
      FI # TOBINSTRING # ;
   INT power of 2 := 1;
   FOR b WHILE IF b = power of 2 THEN
                   power of 2 *:= 2
               FI;
               INT cat value = ( b * power of 2 ) + b;
               cat value < 1000
   DO
       print( ( whole( cat value, -4 ), ": ", TOBINSTRING cat value, newline ) )
   OD

END</lang>

Output:
   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

ALGOL W

Translation of: MAD

<lang algolw>BEGIN

   INTEGER PROCEDURE BITSP ( INTEGER VALUE BT ) ;
   BEGIN
       INTEGER BITN, BITRSL, BITIDX;
       BITN   := BT;
       BITRSL := 0;
       BITIDX := 1;
       WHILE BITN > 0 DO BEGIN
           INTEGER BITNX;
           BITNX  := BITN DIV 2;
           BITRSL := BITRSL + BITIDX*(BITN-BITNX*2);
           BITN   := BITNX;
           BITIDX := BITIDX*10
       END;
       BITRSL
   END BITSP ;
   INTEGER PROCEDURE DPLBIT ( INTEGER VALUE DVAL ) ;
   BEGIN
       INTEGER DTEMP, DSHFT;
       DTEMP := DVAL;
       DSHFT := DVAL;
       WHILE DTEMP > 0 DO BEGIN
           DSHFT := DSHFT  *  2;
           DTEMP := DTEMP DIV 2;
       END;
       DSHFT + DVAL
   END DPLBIT ;
   BEGIN
       INTEGER N;
       N := 0;
       WHILE BEGIN
             N := N + 1;
             DPLBIT(N) < 1000
       END DO WRITE( S_W := 0, I_W := 3, DPLBIT(N), ": ", I_W := 10, BITSP(DPLBIT(N)) )
   END

END.</lang>

Output:
  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

APL

Works with: Dyalog APL

<lang APL>↑(((⊂2∘⊥),⊂)(,⍨2∘⊥⍣¯1))¨⍳30</lang>

Output:
  3  1 1                 
 10  1 0 1 0             
 15  1 1 1 1             
 36  1 0 0 1 0 0         
 45  1 0 1 1 0 1         
 54  1 1 0 1 1 0         
 63  1 1 1 1 1 1         
136  1 0 0 0 1 0 0 0     
153  1 0 0 1 1 0 0 1     
170  1 0 1 0 1 0 1 0     
187  1 0 1 1 1 0 1 1     
204  1 1 0 0 1 1 0 0     
221  1 1 0 1 1 1 0 1     
238  1 1 1 0 1 1 1 0     
255  1 1 1 1 1 1 1 1     
528  1 0 0 0 0 1 0 0 0 0 
561  1 0 0 0 1 1 0 0 0 1 
594  1 0 0 1 0 1 0 0 1 0 
627  1 0 0 1 1 1 0 0 1 1 
660  1 0 1 0 0 1 0 1 0 0 
693  1 0 1 0 1 1 0 1 0 1 
726  1 0 1 1 0 1 0 1 1 0 
759  1 0 1 1 1 1 0 1 1 1 
792  1 1 0 0 0 1 1 0 0 0 
825  1 1 0 0 1 1 1 0 0 1 
858  1 1 0 1 0 1 1 0 1 0 
891  1 1 0 1 1 1 1 0 1 1 
924  1 1 1 0 0 1 1 1 0 0 
957  1 1 1 0 1 1 1 1 0 1 
990  1 1 1 1 0 1 1 1 1 0

BASIC

<lang BASIC>10 DEFINT A-Z: DIM B(15) 20 N=0 30 N=N+1 40 C=0: X=N 50 C=C+1 60 X=X\2 70 IF X>0 THEN 50 80 K=N+2^C*N 90 IF K>1000 THEN END 100 PRINT K, 110 FOR I=C*2 TO 1 STEP -1 120 B(I)=K AND 1 130 K=K\2 140 NEXT I 150 FOR I=1 TO C*2 160 PRINT USING "#";B(I); 170 NEXT I 180 PRINT 190 GOTO 30</lang>

Output:
 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

C

<lang c>#include <stdio.h>

  1. include <stdint.h>

uint8_t bit_length(uint32_t n) {

   uint8_t r;
   for (r=0; n; r++) n >>= 1;
   return r;

}

uint32_t concat_bits(uint32_t n) {

   return (n << bit_length(n)) | n;

}

char *bits(uint32_t n) {

   static char buf[33];
   char *ptr = &buf[33];
   *--ptr = 0;
   do {
       *--ptr = '0' + (n & 1);
   } while (n >>= 1);
   return ptr;

}

int main() {

   uint32_t n, r;
   for (n=1; (r = concat_bits(n)) < 1000; n++) {
       printf("%d: %s\n", r, bits(r));
   }
   return 0;

}</lang>

Output:
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

C++

<lang cpp>#include <iostream>

  1. include <string>

// Given the base 2 representation of a number n, transform it into the base 2 // representation of n + 1. void base2_increment(std::string& s) {

   size_t z = s.rfind('0');
   if (z != std::string::npos) {
       s[z] = '1';
       size_t count = s.size() - (z + 1);
       s.replace(z + 1, count, count, '0');
   } else {
       s.assign(s.size() + 1, '0');
       s[0] = '1';
   }

}

int main() {

   std::cout << "Decimal\tBinary\n";
   std::string s("1");
   for (unsigned int n = 1; ; ++n) {
       unsigned int i = n + (n << s.size());
       if (i >= 1000)
           break;
       std::cout << i << '\t' << s << s << '\n';
       base2_increment(s);
   }

}</lang>

Output:
Decimal	Binary
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

Cowgol

<lang cowgol>include "cowgol.coh";

sub bitLength(n: uint32): (l: uint8) is

   l := 0;
   while n != 0 loop
       n := n >> 1;
       l := l + 1;
   end loop;

end sub;

sub concatBits(n: uint32): (r: uint32) is

   r := (n << bitLength(n)) | n;

end sub;

sub printBits(n: uint32) is

   var buf: uint8[33];
   var ptr := &buf[32];
   [ptr] := 0;
   loop
       ptr := @prev ptr;
       [ptr] := '0' + (n as uint8 & 1);
       n := n >> 1;
       if n == 0 then break; end if;
   end loop;
   print(ptr);

end sub;

var n: uint32 := 1; loop

   var r := concatBits(n);
   if r > 1000 then break; end if;
   print_i32(r);
   print(": ");
   printBits(r);
   print_nl();
   n := n + 1;

end loop;</lang>

Output:
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

Factor

Works with: Factor version 0.99 2021-02-05

<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</lang>

Output:
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

FALSE

<lang FALSE>1[$$$[$][2/\2*\]#%|$1000>~][

   $.": "
   0\10\[$1&'0+\2/$][]#%
   [$][,]#%
   1+

]#%%</lang>

Output:
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

FOCAL

<lang FOCAL>01.10 S N=0 01.20 S N=N+1 01.30 D 3 01.40 I (K-1000)1.5;Q 01.50 T %3,K,": " 01.60 D 4 01.70 G 1.2

02.10 S BC=0;S BT=N 02.20 S BC=BC+1 02.30 S BT=FITR(BT/2) 02.40 I (-BT)2.2

03.10 D 2;S I=BC;S BT=N 03.20 S BX=FITR(BT/2) 03.30 S I=I-1 03.40 S B(I)=BT-BX*2 03.50 S BT=BX 03.60 I (-I)3.2,3.2 03.70 F I=0,BC-1;S B(BC+I)=B(I) 03.80 S BC=BC*2;S K=0 03.90 F I=0,BC-1;S K=K*2+B(I)

04.10 F I=0,BC-1;D 4.3 04.20 T !;R 04.30 I (B(I))4.4,4.5,4.4 04.40 T "1" 04.50 T "0"</lang>

Output:
=   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

Forth

<lang forth>: concat-self

   dup dup
   begin dup while
       1 rshift
       swap 1 lshift swap 
   repeat
   drop or
print-bits
   0 swap
   begin
       dup 1 and '0 +
       swap 1 rshift 
   dup 0= until drop
   begin dup while emit repeat drop
to1000
   1 
   begin dup concat-self dup 1000 < while
       dup . 9 emit print-bits cr
       1+
   repeat
   2drop

to1000 bye</lang>

Output:
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

Fortran

<lang fortran> program IdentStr

         implicit none
         integer n, concat, bits
         
         n = 1
 100     if (concat(n) .lt. 1000) then
             write (*,'(I3,2X,I11)') concat(n), bits(concat(n))
             n = n + 1
             goto 100
         end if
         stop
     end

C Concatenate binary representation of number with itself

     integer function concat(num)
         integer num, sl, sr
         sl = num
         sr = num
 100     if (sr .gt. 0) then
             sl = sl * 2
             sr = sr / 2
             goto 100
         end if
         concat = num + sl
     end

C Calculate binary representation of number

     integer function bits(num)
         integer num, n, bx
         n = num
         bits = 0
         bx = 1
 100     if (n .gt. 0) then
             bits = bits + bx * mod(n,2)
             bx = bx * 10
             n = n / 2
             goto 100
         end if
     end</lang>
Output:
  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

FreeBASIC

<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</lang>

Output:
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

Alternate

No log() function required. <lang freebasic>dim as uinteger n = 1, k = 0, p = 2 do

   if n >= p then p = p + p
   k = n + n * p
   if k < 1000 then print k, bin(k) else end
   n = n + 1

loop</lang>

Output:

Same as log() version.

Haskell

<lang haskell>import Control.Monad import Data.Bits import Text.Printf

-- Find the amount of bits required to represent a number nBits :: Int -> Int nBits = liftM2 (-) finiteBitSize countLeadingZeros

-- Concatenate the bits of a number to itself concatSelf :: Int -> Int concatSelf = (.|.) =<< ap shift nBits

-- Integers whose base-2 representation is the concatenation of -- two identical binary strings identStrInts :: [Int] identStrInts = map concatSelf [1..]

main :: IO () main = putStr $ unlines $ map (join $ printf "%d: %b") to1000

   where to1000 = takeWhile (<= 1000) identStrInts</lang>
Output:
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

J

<lang J>(":,': ',":@#:)@(,~&.#:)"0 (>:i.30)</lang>

Output:
3: 1 1
10: 1 0 1 0
15: 1 1 1 1
36: 1 0 0 1 0 0
45: 1 0 1 1 0 1
54: 1 1 0 1 1 0
63: 1 1 1 1 1 1
136: 1 0 0 0 1 0 0 0
153: 1 0 0 1 1 0 0 1
170: 1 0 1 0 1 0 1 0
187: 1 0 1 1 1 0 1 1
204: 1 1 0 0 1 1 0 0
221: 1 1 0 1 1 1 0 1
238: 1 1 1 0 1 1 1 0
255: 1 1 1 1 1 1 1 1
528: 1 0 0 0 0 1 0 0 0 0
561: 1 0 0 0 1 1 0 0 0 1
594: 1 0 0 1 0 1 0 0 1 0
627: 1 0 0 1 1 1 0 0 1 1
660: 1 0 1 0 0 1 0 1 0 0
693: 1 0 1 0 1 1 0 1 0 1
726: 1 0 1 1 0 1 0 1 1 0
759: 1 0 1 1 1 1 0 1 1 1
792: 1 1 0 0 0 1 1 0 0 0
825: 1 1 0 0 1 1 1 0 0 1
858: 1 1 0 1 0 1 1 0 1 0
891: 1 1 0 1 1 1 1 0 1 1
924: 1 1 1 0 0 1 1 1 0 0
957: 1 1 1 0 1 1 1 1 0 1
990: 1 1 1 1 0 1 1 1 1 0

MAD

<lang MAD> NORMAL MODE IS INTEGER

           INTERNAL FUNCTION(BT)
           ENTRY TO BITS.
           BITN = BT
           BITRSL = 0
           BITIDX = 1

GETBIT WHENEVER BITN.G.0

               BITNX = BITN/2
               BITRSL = BITRSL + BITIDX*(BITN-BITNX*2)
               BITN = BITNX
               BITIDX = BITIDX*10
               TRANSFER TO GETBIT
           END OF CONDITIONAL
           FUNCTION RETURN BITRSL
           END OF FUNCTION
           
           INTERNAL FUNCTION(DVAL)
           ENTRY TO DPLBIT.
           DTEMP = DVAL
           DSHFT = DVAL

DSTEP WHENEVER DTEMP.G.0

               DSHFT = DSHFT * 2
               DTEMP = DTEMP / 2
               TRANSFER TO DSTEP
           END OF CONDITIONAL
           FUNCTION RETURN DSHFT + DVAL
           END OF FUNCTION
           
           THROUGH NUM, FOR N=1, 1, DPLBIT.(N).GE.1000

NUM PRINT FORMAT NFMT, DPLBIT.(N), BITS.(DPLBIT.(N))

           VECTOR VALUES NFMT = $I3,2H: ,I10*$
           END OF PROGRAM  </lang>
Output:
  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

Pascal

Works with: Turbo Pascal

<lang pascal>program IdenticalStrings; const

   LIMIT = 1000;

var

   n: Integer;

function BitLength(n: Integer): Integer;

   var count: Integer;
   begin
       count := 0;
       while n > 0 do
       begin
           n := n shr 1;
           count := count + 1;
       end;
       BitLength := count;
   end;

function Concat(n: Integer): Integer;

   begin
       Concat := n shl BitLength(n) or n;
   end;

procedure WriteBits(n: Integer);

   var bit: Integer;
   begin
       bit := 1 shl (BitLength(n)-1);
       while bit > 0 do
       begin
           if (bit and n) <> 0 then Write('1')
           else Write('0');
           bit := bit shr 1;
       end;
  end;

begin

  n := 1;
  while Concat(n) < LIMIT do
  begin
      Write(Concat(n));
      Write(': ');
      WriteBits(Concat(n));
      WriteLn;
      n := n + 1;
  end;

end.</lang>

Output:
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

Python

<lang python>def bits(n):

   """Count the amount of bits required to represent n"""
   r = 0
   while n:
       n >>= 1
       r += 1
   return r
   

def concat(n):

   """Concatenate the binary representation of n to itself"""
   return n << bits(n) | n
   

n = 1 while concat(n) <= 1000:

   print("{0}: {0:b}".format(concat(n)))
   n += 1</lang>
Output:
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

Perl

<lang perl>#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Two_identical_strings use warnings;

while( 1 )

 {
 my $binary = ( sprintf "%b", ++$- ) x 2;
 (my $decimal = oct "b$binary") >= 1000 and last;
 printf "%4d  %s\n", $decimal, $binary;
 }</lang>
Output:
   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

Phix

integer n = 1
sequence res = {}
while true do
    string binary = sprintf("%b%b",n)
    integer decimal = to_number(binary,0,2)
    if decimal>1000 then exit end if
    res &= {sprintf("%-4d %-10s",{decimal,binary})}
    n += 1
end while
printf(1,"Found %d numbers:\n%s\n",{n-1,join_by(res,5,6)})
Output:
Found 30 numbers:
3    11           54   110110       187  10111011     528  1000010000   693  1010110101   858  1101011010
10   1010         63   111111       204  11001100     561  1000110001   726  1011010110   891  1101111011
15   1111         136  10001000     221  11011101     594  1001010010   759  1011110111   924  1110011100
36   100100       153  10011001     238  11101110     627  1001110011   792  1100011000   957  1110111101
45   101101       170  10101010     255  11111111     660  1010010100   825  1100111001   990  1111011110

Raku

<lang perl6>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)];</lang>
Output:
30 matching numbers
          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)

REXX

<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.*/ if hi== | hi=="," then hi= 1000 /* " " " " " " */ if cols== | cols=="," then cols= 4 /* " " " " " " */ w= 20 /*width of a number in any column. */

    @dnbn= ' decimal integers whose binary version is a doubled binary literal, N  < ' ,
             commas(hi)

if cols>0 then say ' index │'center(@dnbn, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')

  1. = 0; idx= 1 /*initialize # of integers and index. */

$= /*a list of nice primes (so far). */

    do j=1  for hi-1;  b= x2b( d2x(j) ) + 0     /*find binary values that can be split.*/
    L= length(b);      h= L % 2                 /*obtain length of the binary value.   */
    if L//2                      then iterate   /*Can binary version be split? No, skip*/
    if left(b, h)\==right(b, h)  then iterate   /*Left half match right half?   "    " */
    #= # + 1                                    /*bump the number of integers found.   */
    if cols==0                   then iterate   /*Build the list  (to be shown later)? */
    c= commas(j) || '(' || b")"                 /*maybe add commas, add binary version.*/
    $= $ right(c, max(w, length(c) ) )          /*add a nice prime ──► list, allow big#*/
    if #//cols\==0               then iterate   /*have we populated a line of output?  */
    say center(idx, 7)'│'  substr($, 2);   $=   /*display what we have so far  (cols). */
    idx= idx + cols                             /*bump the  index  count for the output*/
    end   /*j*/

if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say say 'Found ' commas(#) @dnbn 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 ?</lang>

output   when using the default inputs:
 index │    decimal integers whose binary version is a doubled binary literal, N  <  1,000
───────┼─────────────────────────────────────────────────────────────────────────────────────
   1   │                3(11)             10(1010)             15(1111)           36(100100)
   5   │           45(101101)           54(110110)           63(111111)        136(10001000)
   9   │        153(10011001)        170(10101010)        187(10111011)        204(11001100)
  13   │        221(11011101)        238(11101110)        255(11111111)      528(1000010000)
  17   │      561(1000110001)      594(1001010010)      627(1001110011)      660(1010010100)
  21   │      693(1010110101)      726(1011010110)      759(1011110111)      792(1100011000)
  25   │      825(1100111001)      858(1101011010)      891(1101111011)      924(1110011100)
  29   │      957(1110111101)      990(1111011110)

Found  30  decimal integers whose binary version is a doubled binary literal, N  <  1,000

Ring

<lang ring>load "stdlib.ring"

decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]

see "working..." + nl see "Numbers whose base 2 representation is the juxtaposition of two identical strings:" + nl

row = 0 limit1 = 1000

for n = 1 to limit1

   bin = decimaltobase(n,2)
   ln = len(bin)
   if ln & 1 = 0
      if left(bin,ln/2) = right(bin,ln/2)
         row++
         see sfl(n, 3) + " (" + sfrs(bin, 10) + ")  "
         if row % 5 = 0 see nl ok
      ok
    ok

next

? nl + "Found " + row + " numbers whose base 2 representation is the juxtaposition of two identical strings" ? "done..."

func decimaltobase(nr,base)

    binList = [] 
    binary = 0
    remainder = 1
    while(nr != 0)
         remainder = nr % base
         ind = find(decList,remainder)
         rem = baseList[ind]
         add(binList,rem)
         nr = floor(nr/base) 
    end
    binlist = reverse(binList)
    binList = list2str(binList)
    binList = substr(binList,nl,"")  
    return binList

  1. a very plain string formatter, intended to even up columnar outputs

def sfrs x, y

   l = len(x)
   x += "            "
   if l > y y = l ok
   return substr(x, 1, y)

  1. a very plain string formatter, intended to even up columnar outputs

def sfl x, y

   s = string(x) l = len(s)
   if l > y y = l ok
   return substr("          ", 11 - y + l) + s</lang>
Output:
working...
Numbers whose base 2 representation is the juxtaposition of two identical strings:
  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 whose base 2 representation is the juxtaposition of two identical strings
done...

Visual Basic .NET

Translation of: FreeBASIC

Based on the Alternate version.

<lang vbnet>Imports System.Console Module Module1

 Sub Main()
   Dim p, c, k, lmt as integer : p = 2 : lmt = 1000
   For n As Integer = 1 to lmt
     p += If(n >= p, p, 0) : k = n + n * p
     If k > lmt Then Exit For Else c += 1
     Write("{0,3} ({1,-10})  {2}", k, Convert.ToString( k, 2),
         If(c Mod 5 = 0, vbLf, ""))
   Next : WriteLine(vbLf + "Found {0} numbers whose base 2 representation is the concatenation of two identical strings.", c)
 End Sub

End Module</lang>

Output:
  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 whose base 2 representation is the concatenation of two identical strings.

Wren

Library: Wren-fmt

<lang ecmascript>import "/fmt" for Conv, Fmt

var i = 1 while(true) {

   var b2 = Conv.itoa(i, 2)
   b2 = b2 + b2
   var d = Conv.atoi(b2, 2)
   if (d >= 1000) break
   Fmt.print("$3d : $s", d, b2)
   i = i + 1

} System.print("\nFound %(i-1) numbers.")</lang>

Output:
  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.