{ Start of file JugglerX.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 JugglerX; { Investigates random Juggler Sequences that loop }
{ 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 = 'JugglerX - Investigates random Juggler Sequences that loop';
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 investigates random Juggler Sequences that loop
}
var
I : Integer; { Utility index }
Ch : Char; { Character input by ReadKey }
St : String[128];{ String for starting number input from keyboard }
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 }
n2 : ^MultiSI; { Starting x squared }
x : ^MultiSI; { Current value of x }
x2 : ^MultiSI; { Current x squared }
x3 : ^MultiSI; { Current x cubed }
Max : ^MultiSI; { Max value of x found so far }
nMax : ^MultiSI; { Value of n for this Max x }
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 }
NewMax: Boolean; { True if a new max x found for this n }
Done : Boolean; { True if Max x repeats for same n }
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:= 'JugglerX.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 (24 * 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( n2, Init( 4 * Size div 3));
New( x, Init( Size));
New( x2, Init( 4 * Size div 3));
New( x3, Init( 2 * Size));
New( nMax, Init( 2 * Size div 3));
New( Max, Init( Size));
Max^.Clear; NewMax:= False; Done:= False;
n2^.Sq( n^);
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, JugglerX }
Init;
Assign( Disk, FileSt);
Rewrite( Disk);
WriteLn;
WriteLn( Disk, 'Investigates random Juggler Sequences that loop (JugglerX)');
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;
n2^.Sq( n^);
DM:= Digits(x^); NewMDM:= False; NewML:= False;
Write( Disk, 'n='); n^.Writ( Disk); Write(Disk, ' ');
Write( 'n='); n^.Writ( Output); Write(' ');
Max^.Clear; NewMax:= False; Done:= False;
repeat
LSD:= Round( x^.V^[0]);
if x^.Comp( n2^) < 0 then begin
Inc( Up); { Write(Disk, '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); { Write(Disk, 'D'); Write('D'); *** }
Tracn:= Down; Trace:= Up;
x^.SqRtRem( x^, x3^);
if Down = 2000 then Max^.Clear;
end;
{ x^.WritLn( Disk); x^.WritLn( Output); }
if (x^.Comp( Max^) >= 0) then begin
if (x^.Comp( Max^) = 0) and (n^.Comp( nMax^) = 0) then begin
Done:= True;
end;
if (x^.Comp( Max^) > 0) then begin
Max^.SetTo( x^); NewMax:= True; nMax^.SetTo( n^);
end;
end;
until (x^.Comp( n^) = 0) or Done or MuErr or MuAbort;
if MuAbort then begin
MuAbort:= False;
Write( Disk, 'Computation aborted by operator');
end;
if Done then Write( Disk, 'Max x repeated before x zero did');
if Done then Write( 'Max x repeated before x zero did');
L:= Up + Down;
if (x^.Comp( one^) = 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( Disk, ' U=', Up, ' D=', Down, ' L=', L, ' DM=', DM);
Write( ' U=', Up, ' D=', Down, ' L=', L, ' DM=', DM);
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( Disk); WriteLn;
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. { JugglerX }
{ End of file JugglerX.PAS *************************************************}