How to resolve the algorithm 9 billion names of God the integer step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm 9 billion names of God the integer step by step in the C++ programming language

Table of Contents

Problem Statement

This task is a variation of the short story by Arthur C. Clarke. (Solvers should be aware of the consequences of completing this task.) In detail, to specify what is meant by a   “name”:

Display the first 25 rows of a number triangle which begins: Where row

n

{\displaystyle n}

corresponds to integer

n

{\displaystyle n}

,   and each column

C

{\displaystyle C}

in row

m

{\displaystyle m}

from left to right corresponds to the number of names beginning with

C

{\displaystyle C}

. A function

G ( n )

{\displaystyle G(n)}

should return the sum of the

n

{\displaystyle n}

-th   row. Demonstrate this function by displaying:

G ( 23 )

{\displaystyle G(23)}

,

G ( 123 )

{\displaystyle G(123)}

,

G ( 1234 )

{\displaystyle G(1234)}

,   and

G ( 12345 )

{\displaystyle G(12345)}

.
Optionally note that the sum of the

n

{\displaystyle n}

-th   row

P ( n )

{\displaystyle P(n)}

is the     integer partition function. Demonstrate this is equivalent to

G ( n )

{\displaystyle G(n)}

by displaying:

P ( 23 )

{\displaystyle P(23)}

,

P ( 123 )

{\displaystyle P(123)}

,

P ( 1234 )

{\displaystyle P(1234)}

,   and

P ( 12345 )

{\displaystyle P(12345)}

.

If your environment is able, plot

P ( n )

{\displaystyle P(n)}

against

n

{\displaystyle n}

for

n

1 … 999

{\displaystyle n=1\ldots 999}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 9 billion names of God the integer step by step in the C++ programming language

The code is a C++ program that calculates the hypotenuse of a right triangle with legs of length n and g, where n is an integer greater than or equal to 1 and g is an integer greater than or equal to 0 and less than or equal to n. The hypotenuse is the length of the side opposite the right angle. The code does this by iterating over the values of n from 1 to N/2 and, for each value of n, calculating the value of the hypotenuse for all values of g from 0 to N-3. The value of the hypotenuse for g is calculated using the formula G(n,g) = 1 + G(n-g-1,g+1), where G(n,g) is the value of the hypotenuse for n and g. The code uses the GMP library to perform the calculations. GMP is a library for performing arbitrary-precision arithmetic. The code also prints the values of the hypotenuse for each value of n and g.

Source code in the cpp programming language

// Calculate hypotenuse n of OTT assuming only nothingness, unity, and hyp[n-1] if n>1
// Nigel Galloway, May 6th., 2013
#include <gmpxx.h>
int N{123456};
mpz_class hyp[N-3];
const mpz_class G(const int n,const int g){return g>n?0:(g==1 or n-g<2)?1:hyp[n-g-2];};
void G_hyp(const int n){for(int i=0;i<N-2*n-1;i++) n==1?hyp[n-1+i]=1+G(i+n+1,n+1):hyp[n-1+i]+=G(i+n+1,n+1);}
}


#include <iostream>
#include <iomanip>
int main(){
  N=25;
  for (int n=1; n<N/2; n++){
    G_hyp(n);
    for (int g=0; g<N-3; g++) std::cout << std::setw(4) << hyp[g];
    std::cout << std::endl;
  }
}


int main(){
  N = 25;
  std::cout << std::setw(N+52) << "1" << std::endl;
  std::cout << std::setw(N+55) << "1     1" << std::endl;
  std::cout << std::setw(N+58) << "1     1     1" << std::endl;
  std::string ott[N-3];
  for (int n=1; n<N/2; n++) {
    G_hyp(n);
    for (int g=(n-1)*2; g<N-3; g++) {
      std::string t = hyp[g-(n-1)].get_str();
      //if (t.size()==1) t.insert(t.begin(),1,' ');
      ott[g].append(t);
      ott[g].append(6-t.size(),' ');
    }
  }
  for(int n = 0; n<N-3; n++) {
    std::cout <<std::setw(N+43-3*n) << 1 << "     " << ott[n];
    for (int g = (n+1)/2; g>0; g--) {
      std::string t{hyp[g-1].get_str()};
      t.append(6-t.size(),' ');
      std::cout << t;
    }
    std::cout << "1     1" << std::endl; 
  }


#include <iostream>
int main(){
  for (int n=1; n<N/2; n++) G_hyp(n);
  std::cout << "G(23)     = " << hyp[21] << std::endl;
  std::cout << "G(123)    = " << hyp[121] << std::endl;
  std::cout << "G(1234)   = " << hyp[1232] << std::endl;
  std::cout << "G(12345)  = " << hyp[12343] << std::endl;
  mpz_class r{3};
  for (int i = 0; i<N-3; i++) r += hyp[i];
  std::cout << "G(123456) = " << r << std::endl;
}


  

You may also check:How to resolve the algorithm Jewels and stones step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Test a function step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Go programming language
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the Phix programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Amazing Hopper programming language