First power of 2 that has leading decimal digits of 12: Difference between revisions

Content added Content deleted
m (→‎{{header|Go}}: added another comment to be elided later (hopefully).)
(added header pascal no use of something like gmp.)
Line 136: Line 136:
p(123, 12345) = 3,510,491
p(123, 12345) = 3,510,491
</pre>
</pre>
=={{header|Pascal}}==
First convert 2**i -> 10**x => x= ln(2)/ln(10) *i<BR>
The integer part of x is the position of the comma.Only the fraction of x leads to the digits.
Only the first digits are needed.So I think, the accuracy is sufficient, because the results are the same :-)
<lang pascal>program Power2FirstDigits;

uses
sysutils,strUtils;
const
ld10= ln(2)/ln(10);

function FindExp(CntLmt,Number:NativeUint):NativeUint;
var
i,dgts,cnt: NativeUInt;
begin
i := Number;
dgts := 1;
while i >= 10 do
Begin
dgts *= 10;
i := i div 10;
end;

cnt := 0;
i := 0;
repeat
inc(i);
IF trunc(dgts*exp(ln(10)*frac(i*lD10))) = Number then
Begin
inc(cnt);
IF cnt>= CntLmt then
BREAK;
end;
until false;
write('The ',Numb2USA(IntToStr(cnt)),'th occurrence of 2 raised to a power');
write(' whose product starts with "',Numb2USA(IntToStr(number)));
writeln('" is ',Numb2USA(IntToStr(i)));
FindExp := i;
end;

Begin
FindExp(1,12);
FindExp(2,12);

FindExp(45,123);
FindExp(12345,123);
FindExp(678910,123);
end.

cnt := 0;
CntLmt := 678910-10;
for i := 1 to 193060223 do
Begin
x:= 100*exp(ln(10)*frac(i*lD10));
IF trunc(x) = 123 then
Begin
inc(cnt);
IF cnt>= CntLmt then
writeln(cnt:8,i:10,trunc(x):5)
end;
end;
end.</lang>
{{out}}
<pre>
The 1th occurrence of 2 raised to a power whose product starts with "12" is 7
The 2th occurrence of 2 raised to a power whose product starts with "12" is 80
The 45th occurrence of 2 raised to a power whose product starts with "123" is 12,710
The 12,345th occurrence of 2 raised to a power whose product starts with "123" is 3,510,491
The 678,910th occurrence of 2 raised to a power whose product starts with "123" is 193,060,223
//64Bit real 0m43,031s //32Bit real 0m13,363s</pre>


=={{header|REXX}}==
=={{header|REXX}}==