Upside-down numbers: Difference between revisions
→{{header|Free Pascal}}: added direct calculation of n-th Upside-Down numbers
(→{{header|Free Pascal}}: added direct calculation of n-th Upside-Down numbers) |
|||
Line 212:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
extended to 50E6. Added direct calculation of n-th Upside-Down number.
<syntaxhighlight lang="pascal">
program UpSideDownNumbers;
Line 219:
const
middle = 5;
type
tUpDown = record
UD_half : array[0..15] of Int32;
UD_Dgt : Int32;
end;
function CalcUpDownNumber(const UD :tUpDown):Uint64;
var
begin
with UD do
Begin
dc := (UD_Dgt shr 1) -1;
result := 0;
For i := dc downto 0 do
result := result*10+UD_half[i];
if Odd(UD_Dgt) then
result := result*10+middle;
For i := 0 to dc do
result := result*10+(10-UD_half[i]);
end;
end;
procedure NthUpDownNumber(n : Uint64;var UD:tUpDown);
var
sum,
dSum : Int64;
begin
repeat
if
dgtCnt +=1;
if Odd(dgtCnt) then
dSum *= 9;
sum += dSum;
until false;
dgtCnt -=1;
with UD do
begin
UD_Dgt := dgtCnt;
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] := sum mod 9+1;
sum := sum div 9;
dec(dgtCnt);
until dgtCnt <0;
end;
end;
end;
procedure NextNumb(var
var
i,dc,dgt : Uint32;
begin
begin
dc := dc shr 1-1;
repeat
dgt := UD_half[i]+1;
if dgt <10 then
begin
UD_half[i] := dgt;
BREAK;
end;
UD_half[i] := 1;
inc(i);
until i > dc;
if i > dc then
Begin
UD_half[i]:= 1;
inc(UD_Dgt);
end;
end
else
begin
end;
end;
end;
var
{$ALIGN 32}
UD1,Ud2 : tUpDown;
Count,
limit :
Begin
Count := 0;
limit := 50;
Writeln('First fifty upside-downs:');
limit := 50;
repeat
NextNumb(UD1);
inc(Count);
write(CalcUpDownNumber(UD1):5);
if Count MOD 10 = 0 then
writeln;
until Count>=limit;
writeln;
writeln(' digits count value');
repeat
repeat
NextNumb(
until count
NthUpDownNumber(count,UD2);
writeln(' next ',UD1.UD_Dgt:3,count:10,CalcUpDownNumber(UD1):20);
writeln(' calc
limit
until Limit > 50*1000*1000 ;
repeat
NthUpDownNumber(Limit,UD2);
until Limit > 500*1000*1000 ;
writeln;
end.
</syntaxhighlight>
Line 310 ⟶ 359:
3467 3557 3647 3737 3827 3917 4196 4286 4376 4466
digits
next 4
calc 4
next 6
calc 6 500 494616
next 8 5000 56546545
calc 8 5000 56546545
next 10 50000 6441469664
calc 10 50000 6441469664
next 12 500000 729664644183
calc 12 500000 729664644183
next 14 5000000 82485246852682
calc 14 5000000 82485246852682
next 16 50000000 9285587463255281
calc 16 50000000 9285587463255281
calc 19 500000000 1436368345672474769
</pre>
|