JugglerM.Pas Program Listing




{ Start of file JugglerM.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 JugglerM; { Searches for a Juggler Sequence that does not fall to 1.
                    Also saves to disk n and max x each time an n produces a
                    max x larger than max x 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    =
        'JugglerM - Searches for a Juggler Sequence that does not fall to 1';
  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 J. Smith tries to find a starting number, n, that
  does not fall to 1.  As a byproduct it outputs n and max x each time an n
  produces a max x larger than max x for any starting x less than n

  n <- 1,  Max <- 0,  NewMax <- False
  repeat
    n <- n + 2,  x <- n,  WriteLn( Screen, 'n = ', n)
    repeat
      if x is odd then  x <- x * x * x
      x <- [Sqrt(x)]
      if x > Max then  Max <- x,  NewMax <- True
    until x <= n
    if NewMax then  WriteLn( File, n, ': ', Max),  NewMax <- False
  until x = n
  WriteLn( File, n, ' Repeats forever ******')

  Format of output to disk:
  x_zero: Max_x (digits in Max_x)
  113: 20,29245,88924,12533,94245,50328 (27)
    ...
  30817: 11705,...,77326 (45391)

  Format of output to screen:
  n=9 UUDUD L=6
}

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 except x zero }
  Down  : LongInt;    { Number of times x was even except last x }
  n     : ^MultiSI;   { Starting x, x zero }
  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 }
  L     : LongInt;    { Length of sequence }
  NewMax: Boolean;    { True if a new max x found for this n }
  Done  : Boolean;    { True if Max x repeats for same n }
  LSD   : LongInt;    { Least significant super digit }

{--------------------------------------}
procedure Init; { Initialize program }
begin
  TextBackground( Blue);
  TextColor( Yellow);
  ClrScr;
  WriteLn;
  WriteLn( Name);
  WriteLn( Version);
  WriteLn( Author);
  WriteLn( Address);
  WriteLn;
  FileSt:= 'JugglerM.Out';
  WriteLn( 'Will write file: ', FileSt,
           ' containing Max_x > all previous Max_x');
  WriteLn;
  MultiIDV.Init( 1);  { Init Multiple-precision integer decimal algorithms }
  MuLenD:= 21;  MuErrRep:= True;
  New( One, Init(1));  One^.SetTo1( 1);
  New( P, Init(1));
  Size:= (3 * (Memavail - 175)) div (20 * 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( nMax, Init( 2 * Size div 3));
  New( x,  Init( Size));
  New( x2, Init( 4 * Size div 3));
  New( x3, Init( 2 * Size));
  New( Max, Init( Size));
  Max^.Clear;  NewMax:= False;  Done:= False;
  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 }

{--------------------------------------}
begin { Main program, JugglerM }
  Init;
  Assign( Disk, FileSt);
  Rewrite( Disk);
  WriteLn;
  WriteLn( Disk,
    'Juggler Sequences with Max_x > all previous Max_x (JugglerM)');
  WriteLn( Disk);
  WriteLn( Disk, 'x_zero: Max_x (digits in Max_x)');
  WriteLn( Disk);
  if (n^.Comp( One^) = 0) then begin
    WriteLn('n=1 U L=1');
    WriteLn( Disk, '1: 1 (1)');  WriteLn( Disk);
  end;
  x^.SetTo1( 3);
  if (n^.Comp( x^) < 0) then begin
    WriteLn('n=2 D L=2');
    WriteLn( Disk, '2: 2 (1)');  WriteLn( Disk);  n^.SetTo1( 3);
  end;
  LSD:= Round( n^.V^[0]);
  if (LSD mod 2) = 0 then  n^.RAdd1( 1);
  n^.RAdd1( -2);
  repeat
    n^.RAdd1( 2);  x^.SetTo( n^);  Up:= 0;  Down:= 0;
    Write( 'n=');  n^.Writ( Output);  Write(' ');
    repeat
      LSD:= Round( x^.V^[0]);
      if (LSD mod 2) = 1 then begin
        Inc( Up);  Write('U');
        Tracn:= Down;  Trace:= Up;
        x2^.Sq( x^);  x3^.Mul( x^, x2^);  x^.SqRtRem( x3^, x3^);
      end
      else begin
        Inc( Down);  Write('D');
        Tracn:= Down;  Trace:= Up;
        x^.SqRtRem( x^, x3^);
      end;
      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 NewMax then begin
      n^.Writ( Disk);  Write( Disk, ': ');
      MuLenD:= 0;  Max^.ShortWrLn( Disk, 100);  MuLenD:= 21;
      WriteLn( Disk);  NewMax:= False;
    end;
    L:= Up + Down + 1;
    if (x^.Comp( n^) = 0) then  L:= 1;
    WriteLn(' L=', L);
  until (x^.Comp( n^) = 0) or Done or MuErr or MuAbort;
  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')
  else  WriteLn( Disk, 'Repeats forever ******');
  Close( Disk);
  Ch:= ReadKey;
end. { JugglerM }

{ End of file JugglerM.PAS *************************************************}

Return to Juggler Numbers
Return to Harry's Home Page


This page accessed times since October 20, 2004.
Page created by: hjsmithh@sbcglobal.net
Changes last made on Saturday, 14-May-05 12:49:13 PDT