Two identical strings: Difference between revisions

Add Refal
(RPL: optimized version)
(Add Refal)
 
(7 intermediate revisions by 6 users not shown)
Line 1,714:
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}}==
Line 1,769 ⟶ 1,854:
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#}}==
Line 3,680 ⟶ 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
<syntaxhighlight lang="text">/*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. */</syntaxhighlight>
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 3,842 ⟶ 4,008:
 
=={{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}}
Line 3,863 ⟶ 4,029:
</pre>
Runs on an HP-28S in 130 seconds.
 
=== Optimized version===
{{trans|FreeBASIC}}
Line 3,880 ⟶ 4,047:
SWAP R→B →STR 2 OVER SIZE 1 - SUB + + 4 ROLLD
SWAP 1 +
'''UNTIL''' SWAP 1000 ≥ '''END''' DROP2
≫ ''''TASK'''' STO
|
Line 3,938 ⟶ 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}}==
Line 4,119 ⟶ 4,336:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var i = 1
2,094

edits