Jump to content

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

Fix pascal version to run in delphi
(Fix pascal version to run in delphi)
Line 982:
 
uses
sysutils,strUtils;
strUtils;
 
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 FindExpNumb2USA(CntLmt,Numberconst S:NativeUint string):NativeUint string;
var
i,cnt,DgtShift NA: NativeUIntInteger;
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;
DgtShift := 1;
while i >= 10 do
Beginbegin
DgtShift* := DgtShift * 10;
i := i div 10;
end;
Line 1,005 ⟶ 1,032:
// x= i*ld10 -> 2^I = 10^x
// 10^frac(x) -> [0..10[ = exp(ln(10)*frac(i*lD10))
IFif truncTrunc(DgtShift * exp(ln(10) * frac(i * lD10))) = Number then
Beginbegin
inc(cnt);
IFif 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(numberNumber)));
writeln('" is ', Numb2USA(IntToStr(i)));
FindExp := i;
end;
 
begin
Begin
FindExp(1, 12);
FindExp(2, 12);
 
FindExp(45, 123);
FindExp(12345, 123);
FindExp(678910, 123);
end.</lang>
{{out}}
478

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.