Undulating numbers: Difference between revisions
→{{header|ALGOL 68}}: Simplify
(→{{header|ALGOL 68}}: Biug fix) |
(→{{header|ALGOL 68}}: Simplify) |
||
Line 39:
# ABAB... #
LONG INT max number = LONG 2 ^ 53; # maximum number we will consider #
STRING max name = "2^53"; # "name" of max number #
# returns TRUE if n is prime, FALSE otherwise - uses trial division #
PROC is prime = ( LONG INT n )BOOL:
Line 71 ⟶ 73:
result
END # commatise # ;
# digit s with d digits in total #▼
BEGIN▼
LONG INT un := 0;▼
FOR digit TO d DO▼
un *:= b +:= IF ODD digit THEN f ELSE s FI▼
OD;▼
un▼
# returns a string representation of the undulating number in base b #
# with first digit f and second digit s with d digits in total #
Line 93 ⟶ 85:
commatise( un )
END # undulating string # ;
#
BEGIN
LONG INT
INT
[ 1 : (
IF first digit /= second digit THEN▼
LONG INT un = undulating number( d, first digit, second digit, b );▼
IF IF NOT primes only THEN TRUE ELSE is prime( un ) FI THEN▼
print( ( " ", whole( un, -d ) ) );▼
IF ( p count +:= 1 ) MOD max digit = 0 THEN print( ( newline ) ) FI▼
FI▼
FI▼
IF p count MOD max digit /= 0 THEN print( ( newline ) ) FI;▼
print( ( newline ) )▼
# the final one and show number are also shown #▼
LONG INT un := 0;▼
INT un count := 0;▼
INT last f := 0, last s := 0, last d := 0;▼
FOR digits FROM 3 WHILE un < max number DO
print( ( whole( b, 0 ) ) );
IF b /= 10 THEN print( ( " (shown in base 10)" ) ) FI;
print( ( ":", newline ) )
FOR f TO b - 1 WHILE un < max number DO
FOR s FROM 0 TO b - 1 WHILE un < max number DO
IF f /= s THEN
FOR d TO digits
un count +:=
last un
IF digits = 3 OR digits = 4 THEN
IF digits = 3 THEN
▲ FI
▲ FI
print( ( "Undulating number 600 in base ", whole( b, 0 ), ": " ) );
IF b /= 10 THEN
print( ( newline, " which is: " ) );
Line 148 ⟶ 136:
FI
FI
OD;
print( ( "Prime 3 digit Undulating Numbers in base " ) );
IF b /= 10 THEN print( ( " (shown in base 10)" ) ) FI;
print( ( ":", newline ) );
FOR i TO p count DO
print( ( whole( prime un[ i ], -4 ) ) );
IF i MOD b = ( b - 1 ) THEN print( ( newline ) ) FI
▲ OD;
▲ print( ( newline ) )
OD;
print( ( "There are ", whole( un count, 0 ) ) );
Line 157 ⟶ 161:
print( ( " which is: ", commatise( whole( last un, 0 ) ), " in base 10", newline ) )
FI
END # print
▲ # shows various Undulating numbers in base b #
▲ PROC print undulating info = ( INT b )VOID:
print
print( ( newline ) );
print
END
|