Upside-down numbers: Difference between revisions

Added an Algol68 translation of the Phix sample.
m (→‎higher limits: inline init)
(Added an Algol68 translation of the Phix sample.)
Line 29:
;* [[oeis:A299539|OEIS:A299539 - Upside-down numbers]]
 
 
=={{header|ALGOL 68}}==
{{Trans|Phix}}
Assumes <code>LONG INT</code> is at least 64 bits.
<syntaxhighlight lang="algol68">
BEGIN # find and couunt upside-down numbers #
PROC get counts = ( INT d, REF LONG INT first, last )LONG INT:
BEGIN
LONG INT count := first := last := 1;
FOR i FROM 2 TO d DO
first := last + 1;
IF NOT ODD i THEN count *:= 9 FI;
last := first + count - 1
OD;
count
END # get counts # ;
PROC kth of n = ( LONG INT kth, digits, count )STRING:
IF digits = 0 THEN ""
ELIF digits = 1 THEN "5"
ELSE
OP D = ( INT n )CHAR: REPR ( ABS "0" + n );
LONG INT reduced count := count OVER 9;
INT d = SHORTEN ( ( kth - 1 ) OVER reduced count );
D ( 1 + d ) + kth of n( kth - ( d * reduced count ), digits - 2, reduced count ) + D ( 9 - d )
FI # kth of n # ;
PROC get which = ( LONG INT nth in, REF LONG INT count, first, last, kth, digits )STRING:
BEGIN
LONG INT nth := nth in;
digits := count := first := last := 1;
WHILE nth > last DO
first := last + 1;
digits +:= 1;
IF NOT ODD digits THEN count *:= 9 FI;
last := first + count - 1
OD;
kth := nth - first + 1;
kth of n( kth, digits, count )
END # get which # ;
PROC ord = ( LONG INT n )STRING:
IF INT d2 = SHORTEN ( n MOD 100 );
d2 >= 10 AND d2 <= 20
THEN "th"
ELSE CASE SHORTEN ( n MOD 10 ) IN "st", "nd", "rd" OUT "th" ESAC
FI # ord # ;
print( ( "The first 50 upside down numbers:", newline ) );
FOR i TO 50 DO
STRING ud := get which( i
, LOC LONG INT, LOC LONG INT, LOC LONG INT
, LOC LONG INT, LOC LONG INT
);
WHILE ( UPB ud - LWB ud ) < 3 DO " " +=: ud OD;
print( ( ud, IF i MOD 10 /= 0 THEN " " ELSE newline FI ) )
OD;
print( ( newline ) );
FOR d TO 7 DO
LONG INT first := 0, ord first := 0, last := 0, ord last := 0;
LONG INT count = get counts( d, first, last );
print( ( "There are ", whole( count, 0 ), " ", whole( d, 0 )
, "-digit upside down numbers (the "
, whole( first, 0 ), ord( first ), " to ", whole( last, 0 ), ord( last )
, ")", newline
)
)
OD;
print( ( "(etc...)", newline, newline ) );
[]LONG INT position = ( 1, 2, 10, 11, 182, 500, 910, 911
, 5 000, 50 000, 500 000, 5 000 000, 50 000 000
, 500 000 000, 5 000 000 000, 50 000 000 000, 500 000 000 000
, 5 000 000 000 000, 50 000 000 000 000, 500 000 000 000 000
, 5 000 000 000 000 000
);
FOR i FROM LWB position TO UPB position DO
LONG INT w = position[ i ];
LONG INT count := 0, first := 0, last := 0, kth := 0, digits := 0;
STRING res = get which( w, count, first, last, kth, digits );
print( ( "The ", whole( w, 0 ), ord( w ), " upside down number"
, " is the ", whole( kth, 0 ), ord( kth ), " ", whole( digits, 0 )
, "-digit number (", res, ")", newline
)
)
OD
END
</syntaxhighlight>
{{out}}
<pre>
The first 50 upside down numbers:
5 19 28 37 46 55 64 73 82 91
159 258 357 456 555 654 753 852 951 1199
1289 1379 1469 1559 1649 1739 1829 1919 2198 2288
2378 2468 2558 2648 2738 2828 2918 3197 3287 3377
3467 3557 3647 3737 3827 3917 4196 4286 4376 4466
 
There are 1 1-digit upside down numbers (the 1st to 1st)
There are 9 2-digit upside down numbers (the 2nd to 10th)
There are 9 3-digit upside down numbers (the 11th to 19th)
There are 81 4-digit upside down numbers (the 20th to 100th)
There are 81 5-digit upside down numbers (the 101st to 181st)
There are 729 6-digit upside down numbers (the 182nd to 910th)
There are 729 7-digit upside down numbers (the 911th to 1639th)
(etc...)
 
The 1st upside down number is the 1st 1-digit number (5)
The 2nd upside down number is the 1st 2-digit number (19)
The 10th upside down number is the 9th 2-digit number (91)
The 11th upside down number is the 1st 3-digit number (159)
The 182nd upside down number is the 1st 6-digit number (111999)
The 500th upside down number is the 319th 6-digit number (494616)
The 910th upside down number is the 729th 6-digit number (999111)
The 911th upside down number is the 1st 7-digit number (1115999)
The 5000th upside down number is the 3361st 8-digit number (56546545)
The 50000th upside down number is the 35239th 10-digit number (6441469664)
The 500000th upside down number is the 367141st 12-digit number (729664644183)
The 5000000th upside down number is the 3804259th 14-digit number (82485246852682)
The 50000000th upside down number is the 39238321st 16-digit number (9285587463255281)
The 500000000th upside down number is the 15724390th 19-digit number (1436368345672474769)
The 5000000000th upside down number is the 641519500th 21-digit number (269222738456273888148)
The 50000000000th upside down number is the 10773675490th 23-digit number (41835623444566678457296)
The 500000000000th upside down number is the 146963079400th 25-digit number (5724139689945611241796835)
The 5000000000000th upside down number is the 1822667714590th 27-digit number (751766588225456588225443953)
The 50000000000000th upside down number is the 21404009431300th 29-digit number (94816546925914569158146549261)
The 500000000000000th upside down number is the 36744952787041st 32-digit number (12651942383972646483172786195489)
The 5000000000000000th upside down number is the 830704575083359th 34-digit number (1513835774459212468981566335727959)
</pre>
 
=={{header|Go}}==
3,021

edits