{ Start of file Juggler1.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 Juggler1;{ Investigates Juggler Sequences and saves results to disk }
{ 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 = 'Juggler1 - Investigates Juggler Sequences Ups and Downs';
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. Format of output to disk:
n=9 UUDUDDD L=8 DM=3, New max L, New max DM
Format of output to screen:
n=9 UUDUDDD L=8 DM=3, New max L, New max DM
}
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 }
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:= 'Juggler1.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, Juggler1 }
Init;
Assign( Disk, FileSt);
Rewrite( Disk);
WriteLn;
WriteLn( Disk, 'Juggler Sequences Ups and Downs (Juggler1)');
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;
DM:= Digits(x^); NewMDM:= False; NewML:= False;
Write( Disk, 'n='); n^.Writ( Disk); Write(Disk, ' ');
Write( 'n='); n^.Writ( Output); Write(' ');
repeat
LSD:= Round( x^.V^[0]);
if (LSD mod 2) = 1 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^);
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( Disk, ' L=', L, ' DM=', DM);
Write( ' 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. { Juggler1 }
{ End of file Juggler1.PAS *************************************************}