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

Content deleted Content added
MaiconSoft (talk | contribs)
Fix pascal version to run in delphi
Line 982: Line 982:


uses
uses
sysutils,strUtils;
sysutils,
strUtils;

const
const
{$IFDEF FPC}
ld10= ln(2)/ln(10);// thats 1/log2(10)
{$MODE DELPHI}

ld10 :double = ln(2)/ln(10);// thats 1/log2(10)
{$ELSE}
ld10 = 0.30102999566398119521373889472449;


function FindExp(CntLmt,Number:NativeUint):NativeUint;
function Numb2USA(const S: string): string;
var
var
i,cnt,DgtShift: NativeUInt;
i, NA: Integer;
begin
begin
i := Length(S);
//calc how many Digits needed
Result := S;
NA := 0;
while (i > 0) do
begin
if ((Length(Result) - i + 1 - NA) mod 3 = 0) and (i <> 1) then
begin
insert(',', Result, i);
inc(NA);
end;
Dec(i);
end;
end;

{$ENDIF}

function FindExp(CntLmt, Number: NativeUint): NativeUint;
var
i, cnt, DgtShift: NativeUInt;
begin
//calc how many Digits needed
i := Number;
i := Number;
DgtShift:= 1;
DgtShift := 1;
while i >= 10 do
while i >= 10 do
Begin
begin
DgtShift*= 10;
DgtShift := DgtShift * 10;
i := i div 10;
i := i div 10;
end;
end;
Line 1,005: Line 1,032:
// x= i*ld10 -> 2^I = 10^x
// x= i*ld10 -> 2^I = 10^x
// 10^frac(x) -> [0..10[ = exp(ln(10)*frac(i*lD10))
// 10^frac(x) -> [0..10[ = exp(ln(10)*frac(i*lD10))
IF trunc(DgtShift*exp(ln(10)*frac(i*lD10))) = Number then
if Trunc(DgtShift * exp(ln(10) * frac(i * lD10))) = Number then
Begin
begin
inc(cnt);
inc(cnt);
IF cnt>= CntLmt then
if cnt >= CntLmt then
BREAK;
BREAK;
end;
end;
until false;
until false;
write('The ',Numb2USA(IntToStr(cnt)),'th occurrence of 2 raised to a power');
write('The ', Numb2USA(IntToStr(cnt)), 'th occurrence of 2 raised to a power');
write(' whose product starts with "',Numb2USA(IntToStr(number)));
write(' whose product starts with "', Numb2USA(IntToStr(Number)));
writeln('" is ',Numb2USA(IntToStr(i)));
writeln('" is ', Numb2USA(IntToStr(i)));
FindExp := i;
FindExp := i;
end;
end;


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


FindExp(45,123);
FindExp(45, 123);
FindExp(12345,123);
FindExp(12345, 123);
FindExp(678910,123);
FindExp(678910, 123);
end.</lang>
end.</lang>
{{out}}
{{out}}