Undulating numbers: Difference between revisions

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 # ;
# returns the undulating number in base b with first digit f and second #
# digit s with d digits in total #
PROC undulating number = ( INT d, f, s, b )LONG INT:
BEGIN
LONG INT un := 0;
FOR digit TO d DO
un *:= b +:= IF ODD digit THEN f ELSE s FI
OD;
un
END # undulating number # ;
# 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 # ;
# printsshows thevarious undulatingUndulating numbers with d digits in base b #
PROC print undulating infoundulation = ( INT b )VOID:
# if primes only is TRUE, only prime undulating numbers are shown #
PROC print undulating numbers = ( INT d, b, BOOL primes only )VOID:
BEGIN
LONG INT maxun digit = b - 1 := 0;
IFINT primes only THEN print( ( "Primeun "count ) ) FI := 0;
print(INT ( whole( d, 0 ), "p digitcount Undulating Numbers in base ", whole( b,:= 0 ) ) );
IFINT b /= 10 THEN print( (last "d (shown in base 10)" ) ):= FI0;
print(INT ( ":", newline ) ) last f := 0;
INT p count last s := 0;
FORLONG firstINT digitlast TOun max digit DO := 0;
[ 1 : ( FORb second- digit1 FROM) 0* TO( maxb digit- DO1 ) ]LONG INT prime un;
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
OD
OD;
IF p count MOD max digit /= 0 THEN print( ( newline ) ) FI;
print( ( newline ) )
END # print undulating numbers # ;
# prints the count of undulating numbers up to max number in base b #
# the final one and show number are also shown #
PROC print and count undulating numbers
= ( INT show number, LONG INT max number, STRING max name, INT b )VOID:
BEGIN
LONG INT un := 0;
LONG INT last 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
FORIF fdigits TO= b3 -OR 1digits WHILE= un4 < max number DOTHEN
FORprint( s( FROMwhole( digits, 0 TO), b" -digit 1Undulating WHILENumbers unin <base max" number) DO);
print( ( whole( b, 0 ) ) );
IF b /= 10 THEN print( ( " (shown in base 10)" ) ) FI;
print( ( ":", newline ) )
LONG INT un := 0FI;
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
IF ( un := undulating number( digits, f, s, b ) ) < max number:= THEN0;
FOR d TO digits last un := un;DO
last d un *:= digits;b
last f +:= IF ODD d THEN f; ELSE s FI
last s := sOD;
IF ( un count +:= 1 ) =< showmax number THEN
un count +:= print( ( "Undulating number ", whole( show number, 0 ) ) )1;
last un print( ( " in base ", whole( b, 0 ), ":= " ) )un;
print( ( undulating string( last d, last f, last s, b ) ):= )digits;
# the final one and show number are also shown last f := #f;
LONG INT un last s := 0s;
IF digits = 3 OR digits = 4 THEN
print( ( " ", whole( un, -ddigits ) ) );
IF digits = 3 THEN
IF IF NOT primes only THEN TRUE ELSE IF is prime( un ) FI THEN
# digit s with d digits in total prime un[ p count +:= 1 ] := #un
FI
FI
INT ELIF un count := 0;600 THEN
print( ( "Undulating number 600 in base ", whole( b, 0 ), ": " ) );
LONG INT un = print( ( undulating numberstring( ddigits, first digitf, second digits, b ) ) );
IF b /= 10 THEN
print( ( newline, " which is: " ) );
Line 148 ⟶ 136:
FI
FI
OD;
OD IF digits = 3 OR digits = 4 THEN print( ( newline ) ) FI
FOR digit TO d DOOD;
IF digits = 3 IFOR firstdigits digit /= second digit4 THEN
IF p count MOD max digit /= 0 THEN print( ( newline ) ) FI;
un *:= b +:= IF ODDdigits digit= 4 THEN f ELSE s FI
print( ( "Prime 3 digit Undulating Numbers in base " ) );
INT last f := 0, lastprint( s( :=whole( 0b, last0 d) :=) 0);
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;
IF ( p count +:=MOD 1b )/= MOD( maxb digit- =1 0) THEN print( ( newline ) ) FI;
print( ( newline ) )
un FI
BEGIN FI
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 and count undulating numbersundulation # ;
 
# shows various Undulating numbers in base b #
PROC print undulating info = ( INT b )VOID:
BEGIN
print undulating numbers( 3, b, FALSE );
print undulating numbers( 4, b, FALSE );
print undulating numbers( 3, b, TRUE );
print and count undulating numbers( 600, LONG 2 ^ 53, "2^53", b )
END # print undulating info # ;
 
print undulating infoundulation( 10 ); # base 10 undulating numbers #
print( ( newline ) );
print undulating infoundulation( 7 ) # base 7 undulating numbers #
 
END
3,044

edits