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

Content added Content deleted
m (temporarily added a (forced) TOC.)
m (→‎{{header|Pascal}}: alternative only using one Uint64)
Line 152: Line 152:
The 678,910th occurrence of 2 raised to a power whose product starts with "123" is 193,060,223
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>
//64Bit real 0m43,031s //32Bit real 0m13,363s</pre>
===alternative===
Using only the first digits of 2**i in an Uint64 suffices.
<lang pascal>program Power2Digits;
uses
sysutils,strUtils;

function FindExp(CntLmt:NativeUint;Number : AnsiString):NativeUint;
var
probe : Uint64;
i,cnt: NativeUInt;
begin
cnt := 0;
i := 0;
Probe := 1;
repeat
inc(Probe,Probe);
inc(i);
// if 2**i gets to big divide by one digit = 10
If Probe >= 1000*1000*1000 *1000*1000*1000 then
Probe := Probe DIV 10;
//Check the first characters of the number
IF COPY(IntToStr(Probe),1,Length(number)) = 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(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.</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

real 0m22,931s
//Without IF COPY(IntToStr(Probe),1,Length(number)) = Number then
FindExp(193060223,'123') takes
real 0m0,212s</pre>


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