JugglerW.Cpp Program Listing




// ------- file JugglerW.Cpp

char *Name   =
   "JugglerW - Generate data for 50 dollar award, using huge pointer in C++.";
char *Version= "Version 3.10, last revised: 92/07/26, 0600 hours";
char *Author = "Copyright (c) 1981-1992 by author: Harry J. Smith,";
char *Address= "19628 Via Monte Dr., Saratoga, CA 95070.  All rights reserved.";

#include "MultiIDW.h" // Multiple-precision integer decimal algorithms module
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <alloc.h>
#include <stdlib.h>
#include <fstream.h>

// Developed in Borland C++ 2.0.  Converted to Borland C++ 3.0 for Windows.
// Recompiled with Borland C++ 3.1 for Windows.
/*--------
  Juggler Sequence defined in Dr. Clifford A. Pickover's new book "Computers
  and the Imagination." This book has scores of educational and recreational
  experiments that can be done on a personal computer.

  At the end of Appendix C in this book you will find:

  ". . . An award of 50 dollars is offered by the publisher for a printout of
  the largest Juggler number computed by readers. The award will be given on
  or about September, 1993, and the sequence will also be published in the
  Juggernaut. Currently, the largest juggler number computed is a 45,391-digit
  giant for the starting number 30817. It was computed by Harry J. Smith using
  his own software package to perform multiple precision integer arithmetic.
  His package is written in the object-oriented programming language Turbo
  Pascal 5.5 by Borland International, Inc. His juggler package is a subset of
  his super-precision calculator software which computes transcendental
  functions to thousands of decimal places. Write him to obtain the software:
  Harry J. Smith, 19628 Via Monte Drive, Saratoga, CA 95070. . . ."

  Juggler Sequence also defined in Nov 1990 issue of Algorithm in PERSONAL
  PROGRAMS by Clifford A. Pickover.

  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 uses the results of my earlier program JuggerM.Pas which found
  a number to big for memory from a starting number of 48443, and outputs to
  disk the entire sequence from this starting number.

  Format of output to disk:
  --------

  Juggler Sequence starting with x(0) = 48443 (JugglerW)
  Computed by:
  Harry J. Smith, 19628 Via Monte Dr., Saratoga, CA 95070

  x(i) = x (digits in x)

  x(0) = 48443 (5)
  x(1) = 106,62193 (8)
  x(2) = 3,48152,73349 (11)
  . . .

  x(157) = 1 (1)
  Length = 158
  --------

  Format of output to screen:
  --------
  n=48443 (5)
  U  Up started  T = 0  DT = 0 sec.
    x^2 ended  T = 0  DT = 0 sec.
    x * x^2 ended  T = 0  DT = 0 sec.
    SqRt ended  T = 0.05  DT = 0.05 sec.
    i = 1  N = 14  T = 0.11  DT = 0.06 sec.
  U  Up started  T = 0.27  DT = 0.16 sec.
    x^2 ended  T = 0.32  DT = 0.05 sec.
    x * x^2 ended  T = 0.43  DT = 0.11 sec.
    SqRt ended  T = 0.54  DT = 0.11 sec.
    i = 2  N = 14  T = 0.65  DT = 0.11 sec.
  . . .

  U  Up started  T = 5.21  DT = 0.11 sec.
    x^2 ended  T = 6.2  DT = 0.99 sec.
    x * x^2 ended  T = 6.31  DT = 0.11 sec.
    SqRt ended  T = 6.37  DT = 0.06 sec.
    i = xx  N = nn  T = ttt.tt  DT = ttt.tt sec.
  Length = xx+1
  --------

  A log file is also written to file JugglerW.Log.
  The format of this file is:
  --------

  Juggler Sequence starting with x(0) = 48443 (JugglerW)
  Computed by:
  Harry J. Smith, 19628 Via Monte Dr., Saratoga, CA 95070

  n=48443 (5)
  U  Up started  T = 0  DT = 0 sec.
    x^2 ended  T = 0  DT = 0 sec.
    x * x^2 ended  T = 0  DT = 0 sec.
    SqRt ended  T = 0.05  DT = 0.05 sec.
    i = 1  N = 14  T = 0.11  DT = 0.06 sec.
  U  Up started  T = 0.27  DT = 0.16 sec.
    x^2 ended  T = 0.32  DT = 0.05 sec.
    x * x^2 ended  T = 0.43  DT = 0.11 sec.
    SqRt ended  T = 0.54  DT = 0.11 sec.
    i = 2  N = 14  T = 0.65  DT = 0.11 sec.
  . . .

  U  Up started  T = 5.21  DT = 0.11 sec.
    x^2 ended  T = 6.2  DT = 0.99 sec.
    x * x^2 ended  T = 6.31  DT = 0.11 sec.
    SqRt ended  T = 6.37  DT = 0.06 sec.
    i = 13  N = 7  T = 6.48  DT = 0.11 sec.
  Length = 14
  Run aborted by operator
  --------*/

  int i;           // Utility index
  char Ch;         // Character input by ReadKey
  char St[128];    // String for starting number input from keyboard
  char FileSt[13]; // Disk output file name
  char LogSt[13];  // Log output file name
//ofstream Disk;   // Text file, Disk output (Defined in MultiIDW.h)
//ofstream LogF;   // Text file, Log output file ( "   "   " )
  FILE *R;         // Text file, input
  char VName[128]; // String for name of input value for X(0) from file
  char *sp;        // String pointer
  int OK;          // Boolean flag for ReadSI OK
  long int Up;     // Number of times x was odd except x zero
  long int Down;   // Number of times x was even except last x
  MultiSI n;       // Starting x, x zero
  MultiSI x;       // Current value of x
  MultiSI x2;      // Current x squared
  MultiSI P;       // Power used to input large n = b ^ P
  MultiSI One;     // One in MultiSI format
  long int L;      // Length of sequence
  long int LSD;    // Least significant super digit

// ------- Initialize main program
void init() {
  clrscr();
  cout << nL<< Name << nL<< Version << nL<< Author << nL<< Address << dL;
  strcpy( FileSt, "JugglerW.Out");
  strcpy( LogSt,  "JugglerW.Log");
  cout << "Will write file: " << FileSt <<
    " containing the entire sequence from input starting number" << dL;
  cout << "Will write file: " << LogSt <<
    " containing a log of operations" << dL;
  InitMulti();  // Init Multiple-precision integer decimal algorithms
  MuMaxW = 65;  MuLenD = 21;  MuErrRep = True;  DiagOn = True;
  One |= 1;     // Value = 1, |= is the minimal memory assignment operator
  cout << "Max_x limited to about 1,000,000 decimal digits." << dL;
  cout << "Input starting number, x(0)";
  cout << " (Try 10^255, 0 => Read file X(0), ENTER => 48443):" << nL;
  ReadSt( St); //  cin >> St;  did not allow for a blank line
  while ((strlen( St) > 0) && (St[0] == ' '))  strcpy( St, &St[1]);
  if (!strlen( St))  strcpy( St, "48443");
  n |= St;  // Convert String to n
  strcpy( St, strchr( St, '^'));
  if ((strlen( St) > 1) && (St[0] == '^')) {
    strcpy( St, &St[1]);
    while ((strlen( St) > 0) && (St[0] == ' '))  strcpy( St, &St[1]);
    P |= St;
    n |= n >>= n ^ P;  // >>= is the swap assignment operator
    P &= 0;  // Release memory for the value of P
  }
  OK = False;  L = 0;
  if (n < One) {
    if ((R = fopen("X(0)", "r")) == NULL) {
      cout << "Cannot open file X(0)\n";  exit(1);
    }
    n.NMax(1050000 / MuDMax); // Allow 1,050,000 decimal digits
    n.ReadSI(R, VName, OK);  fclose(R);
    if (!OK) { cout << "Cannot read file X(0)\n";  exit(1); }
    if ((sp = strchr( VName, '(')) != NULL) {
      sp++;  L = atol( sp);
    }
  }
  MuErr = False;  MuAbort = False;
  if (!OK)  cout << "Input = " << n << nL;  // MuTot not used with cout
  cout << "Type any key to continue...  (or Esc to quit)" << nL;
  while (kbhit())  Ch = getch();
  Ch = getch();  if (!Ch)  getch();
  if (Ch == 27)  exit(0);
} // --end-- init

// ------- Terminate program
void quit()
{
  Disk.close();  LogF.close();
//HeapOk(); // Heap check not supported by MS Windows
  exit(0);
} // --end-- quit

int main() // ------- JugglerW
{
  init();
  Disk.open( FileSt, ios::trunc); // Open output file for rewrite
  if (Disk.fail())  { cout << "Cannot open " << FileSt << nL;  exit(1); }
  LogF.open( LogSt, ios::trunc);  // Open Log file for rewrite
  if (LogF.fail())  { cout << "Cannot open " << LogSt << nL;  exit(1); }
  cout << nL;  LogF << nL;  Disk << nL;
  strcpy( St, "Juggler Sequence starting with x(0) ");
  LogF << St;  MuTot = 2 + strlen( St);
  if (OK)  LogF << "from file X(0) (JugglerW)" << nL;
    else   LogF << "= " << n << " (JugglerW)" << nL;
  Disk << St;    MuTot = 2 + strlen( St);
  if (OK)  Disk << "from file X(0) (JugglerW)" << nL;
    else   Disk << "= " << n << " (JugglerW)" << nL;
  strcpy( St, "Computed by:");
  LogF << St << nL;
  Disk << St << nL;
  strcpy( St, "Harry J. Smith, 19628 Via Monte Dr., Saratoga, CA 95070");
  LogF << St << dL;
  Disk << St << dL;
  Disk << "x(i) = x (digits in x)" << dL;
  x >>= n;  Up = 0;  Down = 0;  MuLenD = 0;
//ShortN = 0;   // Select full output format for all numbers, default
  ShortN = 42;  // Select short format for output, at most 42 in long format
  if (!OK) {
    MuTot = 2;  LogF << "n=" << x << nL;
    MuTot = 2;  cout << "n=" << x << nL;
  }
  sprintf( St, "%d", L);  MuTot = 6 + strlen( St);
  Disk << "x(" << St << ") = " << x << nL;
  TV2 = TV1 = TV0 = DosClock();  // Reset timing from zero
  while ((x > One) && !MuErr && !MuAbort && !SoftAbort) {
    LSD = (long int) (x.V[0]);
    if (LSD % 2) {
      Up++;  LogF << 'U';  cout << 'U';
      TracN = Down;  Trace = Up;
      strcpy( St, "  Up started  ");
      cout << St;  Diag("");
      LogF << St;  DiagL(LogF, "");
	x2 >>= x * x;
      strcpy( St, "  x^2 ended  ");
      cout << St;  Diag("");
      LogF << St;  DiagL(LogF, "");
	x >>= x * x2;
      strcpy( St, "  x * x^2 ended  ");
      cout << St;  Diag("");
      LogF << St;  DiagL(LogF, "");
      x2 &= 0;
	if (!MuErr)  x >>= sqrt(x);
    }
    else {
      Down++;  LogF << 'D';  cout << 'D';
      TracN = Down;  Trace = Up;
      strcpy( St, "  Down started  ");
      cout << St;  Diag("");
      LogF << St;  DiagL( LogF, "");
	x >>= sqrt(x);
    }
    strcpy( St, "  SqRt ended  ");
    cout << St;  Diag("");
    LogF << St;  DiagL( LogF, "");
    L++;  sprintf( St, "%d", L);  MuTot = 6 + strlen( St);
    Disk << "x(" << St << ") = " << x << nL;
    cout << "  i = " << L << "  N = " << x.N * 7 << "  ";  Diag("");
    LogF << "  i = " << L << "  N = " << x.N * 7 << "  ";  DiagL( LogF, "");
    Disk << flush;  LogF << flush;
  }
  if (MuErr) {
    strcpy( St, "Numbers got too big for memory");
    Disk << St << nL;
    LogF << St << nL;
    cout << St << nL;
  }
  else  if (MuAbort) {
    strcpy( St, "Run aborted by operator");
    Disk << St << nL;
    LogF << St << nL;
    cout << St << nL;
  }
  else  if (SoftAbort) {
    strcpy( St, "Soft abort by operator");
    Disk << St << nL;
    LogF << St << nL;
    cout << St << nL;
  }
  else {
    strcpy( St, "Length = ");
    Disk << St << L+1 << nL;
    LogF << St << L+1 << nL;
    cout << St << L+1 << nL;
  }
  quit();
  return 0;
} // --end-- main JugglerW

// --end-- file JugglerW.CPP

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:12 PDT