{ Start of file JugglerL.PAS ***********************************************}
{$A+,B-,D+,E+,F-,I+,L+,N+,O-,R-,S+,V+} { Turbo Pascal Default options }
{$I+} {System i/o error checks}
{$N+} {Uses numeric coprocessor}
{$R+} {index Range checking}
{$V-} {no string length checking}
{$M 65520, 0, 655360} {16384, 0, 655360 are defaults; $M 65520 max stack}
program JugglerL; { Investigates long length Juggler Sequences.
Also saves to disk n and max length each time an n
produces a max length larger than max length for any
starting x less than n. }
{ Juggler Sequence defined in Nov 1990 issue of Algorithm in PERSONAL
PROGRAMS by Clifford A. Pickover. }
uses
Crt, { Turbo Pascal 5.5 interface }
MultiID; { Multiple-precision integer decimal algorithms unit}
const
Name = 'JugglerL - Investigates long length Juggler Sequences';
Version = 'Version 1.00, last revised: 90/12/27, 0700 hours';
Author = 'Copyright (c) 1981-1990 by author: Harry J. Smith,';
Address = '19628 Via Monte Dr., Saratoga, CA 95070. All rights reserved.';
{***************************************************************************}
{ Developed in TURBO Pascal 5.5 }
{ Pickover's definition of a juggler sequence:
input positive integer, x
repeat
if x is even
then x <-- [x^(1/2)]
else x <-- [x^(3/2)]
until x=1
This program by Harry Smith outputs n (x zero), a string of characters
U for up (odd x) and D for down (even x), the length of the sequence, and
the number of digits in max x for this n. Disk output only if new max L
or new max DM. Format of output to disk:
n=37 L=18 DM=14, New max L, New max DM
UUUUDUUUDDUUDDDDD
n=77 L=20 DM=7, New max L
UUUDUDUUDDUDUUDUDDD
n=113 L=17 DM=27, New max DM
UUUDUUUUUDDDDDDD
Format of output to screen:
n=9 UUDUDDD L=8 DM=3, New max L, New max DM
}
const
LLim = 256; { Sequence length limit for Up/Down string }
var
I : Integer; { Utility index }
Ch : Char; { Character input by ReadKey }
St : String[128];{ String for starting number input from keyboard }
UD : String[255];{ String of ups and downs for this n }
FileSt: String[12]; { Output file name }
Disk : Text; { Text file }
Size : LongInt; { Size of max x in MultiID super digits }
Up : LongInt; { Number of times x was odd }
Down : LongInt; { Number of times x was even }
n : ^MultiSI; { Starting x, x zero }
x : ^MultiSI; { Current value of x }
x2 : ^MultiSI; { Current x squared }
x3 : ^MultiSI; { Current x cubed }
P : ^MultiSI; { Power used to input large n = b ^ P }
One : ^MultiSI; { One in MultiSI format }
LSD : LongInt; { Least significant super digit }
Di : LongInt; { Decimal digits in x }
DM : LongInt; { Number of decimal digits in max x for this x zero }
MDM : LongInt; { Number of decimal digits in max x so far }
L : LongInt; { Length of sequence }
ML : LongInt; { Max length of sequence so far }
NewMDM: Boolean; { True if new max DM this time }
NewML : Boolean; { True if new max L this time }
{--------------------------------------}
procedure Init; { Initialize program }
begin
TextBackground( Blue);
TextColor( Yellow);
ClrScr;
WriteLn;
WriteLn( Name);
WriteLn( Version);
WriteLn( Author);
WriteLn( Address);
WriteLn;
FileSt:= 'JugglerL.Out';
WriteLn( 'Will write file: ', FileSt, ' to show ups and downs');
WriteLn;
MultiIDV.Init( 1); { Init Multiple-precision integer decimal algorithms }
MuLenD:= 21; MuErrRep:= True;
New( One, Init(1)); One^.SetTo1( 1); { Create new objects }
New( P, Init(1));
Size:= (3 * (Memavail - 155)) div (15 * 4); { Use all but 100 bytes, heap }
if (2 * Size > MuNMax) then Size:= MuNMax div 2; { MuNMax = 16379 }
WriteLn( 'MemAvail=', MemAvail);
WriteLn;
WriteLn( 'Max_x limited to ', MuDMax * Size,
' decimal digits, (57323 digits is max)');
New( n, Init( 2 * Size div 3));
WriteLn;
WriteLn( 'Input starting number, x zero (Try 10^255, ENTER => 1):');
ReadLn( St);
While (Length( St) > 0) and (St[1] = ' ') do Delete( St, 1, 1);
if Length( St) = 0 then St:= '1';
n^.Value( St, I); { Convert String to n, returns I = # of character used }
Delete( St, 1, I);
While (Length( St) > 0) and (St[1] = ' ') do Delete( St, 1, 1);
if (Length( St) > 1) and (St[1] = '^') then begin
Delete( St, 1, 1);
While (Length( St) > 0) and (St[1] = ' ') do Delete( St, 1, 1);
P^.Value( St, I);
n^.RPowMB( P^);
end;
if (n^.Comp( One^) < 0) then n^.SetTo1(1);
MuErr:= False; MuAbort:= False;
Write( 'Input = '); n^.WritLn( Output);
New( x, Init( Size));
New( x2, Init( 4 * Size div 3));
New( x3, Init( 2 * Size));
WriteLn;
WriteLn( 'MemAvail=', MemAvail);
WriteLn( 'Type any key to continue... (or Esc to quit)');
Ch:= ReadKey;
if Ord( Ch) = 27 then Halt(0);
end; { Init }
{--------------------------------------}
function Digits(x : MultiUI) : LongInt; { Compute number of digits in x }
var
Q : LongInt;
I : Integer;
begin
Q:= Round( x.V^[ x.N - 1]); I:= 0;
repeat
Q:= Q div 10; Inc(I);
until Q = 0;
Digits:= I + MuDMax * LongInt( x.N - 1);
end; { Digits }
{--------------------------------------}
begin { Main program, JugglerL }
Init;
Assign( Disk, FileSt);
Rewrite( Disk);
WriteLn;
WriteLn( Disk,
'Juggler Sequences with Max_L > all previous Max_L (JugglerL)');
WriteLn( Disk, '(n=x zero, L=Sequence length, ',
'DM=Number of digits in max x for this n)');
WriteLn( Disk);
MDM:= 0; ML:= 0;
n^.RAdd1( -1);
repeat
n^.RAdd1( 1); x^.SetTo( n^); Up:= 0; Down:= 0; UD:= '';
DM:= Digits(x^); NewMDM:= False; NewML:= False;
Write('n='); n^.Writ( Output); Write(' ');
repeat
LSD:= Round( x^.V^[0]);
if (LSD mod 2) = 1 then begin
Inc( Up); UD:= UD + 'U'; Write('U');
Tracn:= Down; Trace:= Up;
x2^.Sq( x^); x3^.Mul( x^, x2^); x^.SqRtRem( x3^, x3^);
Di:= Digits(x^);
if Di > DM then begin
DM:= Di;
end;
end
else begin
Inc( Down); UD:= UD + 'D'; Write('D');
Tracn:= Down; Trace:= Up;
x^.SqRtRem( x^, x3^);
end;
until (x^.Comp( one^) = 0) or MuErr or MuAbort;
L:= Up + Down + 1;
if (x^.Comp( n^) = 0) then L:= 1;
if L > ML then begin
ML:= L; NewML:= True;
end;
if DM > MDM then begin
MDM:= DM; NewMDM:= True;
end;
Write(' L=', L, ' DM=', DM);
if NewML or NewMDM then begin
Write( Disk, 'n='); n^.Writ( Disk);
Write( Disk, ' L=', L, ' DM=', DM);
end;
if NewML then Write( Disk, ', New max L');
if NewML then Write( ', New max L');
if NewMDM then Write( Disk, ', New max DM');
if NewMDM then Write( ', New max DM');
WriteLn;
if NewML or NewMDM then begin
WriteLn( Disk);
repeat
WriteLn( Disk, ' ', Copy( UD, 1, 65));
Delete( UD, 1, 65);
until UD = '';
if L > LLim then begin
WriteLn( Disk, ' Up/Down String truncated');
end;
end;
until MuErr or MuAbort;
Write( Disk, 'n='); n^.Writ( Disk); Write(Disk, ' ');
if MuErr then WriteLn( Disk, 'Numbers got too big for memory')
else if MuAbort then WriteLn( Disk, 'Run aborted by operator');
Close( Disk);
Ch:= ReadKey;
end. { JugglerL }
{ End of file JugglerL.PAS *************************************************}