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
const
//1,+1,+9,+9,+9*9,+9*9,+9*9*9,...
middle = 5;
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..1521] of Int32;
UD_Dgt : Int32;
end;
 
function CalcUpDownNumberEmitUpDownNumber(const UD :tUpDown):Uint64Ansistring;
var
i,dc,idx : Int32;
begin
with UD do
Begin
dc := (UD_Dgt shr 1) -1;
setlength(result := 0,dc);
dc := dc shr 1 -1;
dsum idx := 1;
For i := dc downto 0 do
Begin
result := result*10+UD_half[i];
result[idx] := chr(UD_half[i]+Ord('0'));
Breakinc(idx);
end;
if Odd(UD_Dgt) then
Begin
result[idx] := result*10+middle'5';
dSum *= 9inc(idx);
end;
For i := 0 to dc do
Begin
result := result*10+(10-UD_half[i]);
result[idx] := result*chr(10+Ord('0')-UD_half[i]);
inc(idx);
end;
end;
end;
Line 244 ⟶ 265:
var
dgtCnt,i : Int32;
sum,
dSum : Int64;
begin
sum := 0;
dgtCnt := 1;
while (dgtCnt<= High(TotalCnt_Dgt)) AND (n>= TotalCnt_Dgt[dgtCnt]) do
dsum := 1;
inc(dgtCnt +=1);
repeat
if (sum>=n) then
Break;
dgtCnt +=1;
if Odd(dgtCnt) then
dSum *= 9;
sum += dSum;
until false;
 
dgtCnt -=1;
with UD do
begin
UD_Dgt := dgtCnt;
n -= TotalCnt_Dgt[dgtCnt-1];
sum := n-(sum-dSum)-1;// -1 because of dgtcnt=1
if dgtCnt > 1 then
begin
dgtCnt := dgtCnt SHR 1-1;//
i := dgtcnt;
repeat
UD_half[i-dgtcnt] := sumn mod 9+1;
sumn := sumn div 9;
dec(dgtCnt);
until dgtCnt <0;
Line 326 ⟶ 335:
NextNumb(UD1);
inc(Count);
write(CalcUpDownNumberEmitUpDownNumber(UD1):5);
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,CalcUpDownNumberEmitUpDownNumber(UD1):20);
writeln(' calc ',UD2.UD_Dgt:3,count:10,CalcUpDownNumberEmitUpDownNumber(UD2):20);
limit *= 10;
until Limit > 50*1000*1000 ;
writeln;
 
repeat
limit :=TotalCnt_Dgt[High(TotalCnt_Dgt)-1]-1;
NthUpDownNumber(Limit,UD2);
writeln(' calc 'limit:20,UD2.UD_Dgt:3,Limit:106,CalcUpDownNumberEmitUpDownNumber(UD2):20*2+2);
inc(limit *= 10);
until Limit > 500*1000*1000 ;
writeln('+1':20);
NthUpDownNumber(Limit,UD2);
end.
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
calc 19 500000000 1436368345672474769
+1
15197081823821161001 41 11111111111111111111599999999999999999999
Highest nth High(Uint64)-1
18446744073709551614 41 34687465242995612644566489451186854632467
 
Real time: 0.282271 s CPU share: 9899.6500 %
</pre>
 
132

edits