Upside-down numbers: Difference between revisions
→{{header|Free Pascal}}: up to High(Uint64)-1 = 18446744073709551614.th
(→{{header|Free Pascal}}: added direct calculation of n-th Upside-Down numbers) |
(→{{header|Free Pascal}}: up to High(Uint64)-1 = 18446744073709551614.th) |
||
Line 212:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
extended to 50E6. Added direct calculation of n-th Upside-Down number.<br>up to High(Uint64)-1 = 18446744073709551614.th
<syntaxhighlight lang="pascal">
program UpSideDownNumbers;
{$IFDEF FPC}{$MODE DELPHI}{$Optimization ON,All}{$ENDIF}
{$IFDEF Windows}{$APPTYPE CONSOLE}{$ENDIF}
//count of UpSideDownNumbers until dgt
//1,+1,+9,+9,+9*9,+9*9,+9*9*9,...
const TotalCnt_Dgt : array[0..41] of Uint64=
(1,2,11,20,101,182,911,1640,8201,14762,73811,132860,664301,1195742,
5978711,10761680,53808401,96855122,484275611,871696100,4358480501,
7845264902,39226324511,70607384120,353036920601,635466457082,
3177332285411,5719198113740,28595990568701,51472783023662,
257363915118311,463255047212960,2316275236064801,4169295424916642,
20846477124583211,37523658824249780,187618294121248901,
337712929418248022,1688564647091240111,3039416364764232200,
15197081823821161001,HIGH(UINT64));
type
tUpDown = record
UD_half : array[0..
UD_Dgt : Int32;
end;
function
var
i,dc,idx : Int32;
begin
with UD do
Begin
dc :=
setlength(result
dc := dc shr 1 -1;
For i := dc downto 0 do
Begin
result := result*10+UD_half[i];▼
result[idx] := chr(UD_half[i]+Ord('0'));
end;
if Odd(UD_Dgt) then
Begin
result[idx] :=
end;
For i := 0 to dc do
Begin
▲ result := result*10+(10-UD_half[i]);
inc(idx);
end;
end;
end;
Line 244 ⟶ 265:
var
dgtCnt,i : Int32;
begin
dgtCnt := 1;
while (dgtCnt<= High(TotalCnt_Dgt)) AND (n>= TotalCnt_Dgt[dgtCnt]) do
▲ dsum := 1;
▲ Break;
▲ dgtCnt +=1;
▲ dSum *= 9;
with UD do
begin
UD_Dgt := dgtCnt;
n -= TotalCnt_Dgt[dgtCnt-1];
if dgtCnt > 1 then
begin
dgtCnt := dgtCnt SHR 1-1;
i := dgtcnt;
repeat
UD_half[i-dgtcnt] :=
dec(dgtCnt);
until dgtCnt <0;
Line 326 ⟶ 335:
NextNumb(UD1);
inc(Count);
write(
if Count MOD 10 = 0 then
writeln;
until Count>=limit;
writeln;
writeln(' digits count value');
Line 337 ⟶ 347:
until count >= limit;
NthUpDownNumber(count,UD2);
writeln(' next ',UD1.UD_Dgt:3,count:10,
writeln(' calc ',UD2.UD_Dgt:3,count:10,
limit *= 10;
until Limit > 50*1000*1000 ;
writeln;
limit :=TotalCnt_Dgt[High(TotalCnt_Dgt)-1]-1;
writeln('+1':20);
NthUpDownNumber(Limit,UD2);
writeln(limit:20,UD2.UD_Dgt:6,EmitUpDownNumber(UD2):20*2+2,#13);
</syntaxhighlight>▼
writeln('Highest nth High(Uint64)-1');
limit := TotalCnt_Dgt[High(TotalCnt_Dgt)]-1;
NthUpDownNumber(Limit,UD2);
writeln(limit:20,UD2.UD_Dgt:6,EmitUpDownNumber(UD2):20*2+2,#13);
▲end.</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Line 375 ⟶ 390:
calc 16 50000000 9285587463255281
15197081823821161000 40 9999999999999999999911111111111111111111
+1
15197081823821161001 41 11111111111111111111599999999999999999999
Highest nth High(Uint64)-1
18446744073709551614 41 34687465242995612644566489451186854632467
Real time: 0.
</pre>
|