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, |
sysutils, |
||
strUtils; |
|||
const |
const |
||
{$IFDEF FPC} |
|||
⚫ | |||
{$MODE DELPHI} |
|||
⚫ | |||
{$ELSE} |
|||
ld10 = 0.30102999566398119521373889472449; |
|||
function |
function Numb2USA(const S: string): string; |
||
var |
var |
||
i, |
i, NA: Integer; |
||
begin |
begin |
||
i := Length(S); |
|||
⚫ | |||
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 |
|||
⚫ | |||
i := Number; |
i := Number; |
||
DgtShift:= 1; |
DgtShift := 1; |
||
while i >= 10 do |
while i >= 10 do |
||
begin |
|||
DgtShift |
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 |
|||
begin |
|||
inc(cnt); |
inc(cnt); |
||
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( |
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}} |