Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 2012 Sept. 15th, R.J. Cano <remy@ula.ve>
- Program for working with the Steinhaus-Johnson-Trotter algorithm sequence.
- */
- #include <stdio.h>
- #include <stdlib.h>
- long innerBigBeta(long, long);
- long bigBeta(long);
- long groundOffsetOfNumber(long);
- long groundOffsetOfarraySTJ(long);
- void demo01();
- void demo02();
- void demo03();
- void demo04();
- int main(void) {
- //demo01();
- //demo02();
- //demo03();
- demo04();
- return EXIT_SUCCESS;
- }
- long innerBigBeta(long K, long N) {
- return (K==N) ? N : ( K+(K+1)*innerBigBeta(K+1, N) );
- }
- /*
- A nice recursive-like function to compute the sum: (LaTEX) \beta = \sum_{k=1}^{N}\left(k!k\right)
- */
- long bigBeta(long N) {
- return innerBigBeta(1,N);
- }
- /*
- The following pair of functions:
- Counts how many terms of the sequence are behind:
- -The number N for the first time it appears.
- -The STJ array sized by N.
- {################################...##NULL}
- 1
- (Starting at head)
- x = groundOffsetOf...(N);
- move "x" times from head to tail.
- {...#######($)######################...#NULL}
- ^
- ^
- (Target pointer: The first N appearing in the sequence or the [1,1] component
- of the STJ array with size N)
- The time-expensive operation would be a first call to the reader fucntion of the
- sequence. After a first call, every known pointer useful for our work may be stored
- in a cache database as long as such information is needed. For example when computing
- matrix determinants hundred or thousand of times for a size of those matrix not known at
- compiling time, this strategy would works fine, fast and reliable.
- A concrete example:
- The concatenation of first 3 STJ arrays gives the following segment of the sequence:
- STJsequence= {1,1,2,2,1,1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
- It let us know a(n) for n in 1..23, and if we are interested only in 3x3
- determinants, we need be placed at the [1,1] of the STJ(3) array, then dependig on
- the programming enviroment and the particular names of the functions it is made
- by some similar to this:
- ref_STJ3= stck_forward_offset(&STJsequence, groundOffsetOfarraySTJ(3));
- Where if a pointer called "tref" is used inside stck_forward_offset()
- for moving, it must invariably do this:
- (At beginning)
- STJsequence= {1,1,2,2,1,1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
- ^
- ^
- tref
- (After moving)
- STJsequence= {1,1,2,2,1,1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
- ^
- ^
- tref
- {1>2>3>4>5>(1),2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
- Because groundOffsetOfarraySTJ(3) returns: 5;
- That's all we really need to face as unknown. The rest of the job
- will be finished by a careful application of the Leibnitz-Laplace
- formula for determinants ("careful" refers to signatures).
- This is enough for numerical determinants and gives a scratch from
- where it must not be difficult to implement optimizations, upgrades,
- and why not, the developing of a CAS like symbolic determinant algorithm.
- */
- long groundOffsetOfNumber(long N) {
- return (2 <= N) ? ( bigBeta(N-1) + (N-1) ) : 0;
- }
- long groundOffsetOfarraySTJ(long N) {
- return (2 <= N) ? ( bigBeta(N-1) ) : 0;
- }
- void demo01() {
- long n;
- printf("\n\nEnter \"n\" for bigBeta(): ");
- scanf("%li",&n);
- printf("\nbigBeta(%li)=%12li",n,bigBeta(n));
- }
- void demo02(){
- long n, j;
- printf("\n\nEnter an unpper bound for bigBeta sequence: ");
- scanf("%li",&n);
- printf("\n");
- for (j=1;j<=n;j++) {
- printf("\n\t n=%12li; \t bigBeta(n)=%12li",j,bigBeta(j));
- }
- }
- void demo03() {
- long go;
- printf("\n\nEnter the integer N which ground offset you want know: ");
- scanf("%li",&go);
- printf("\n groundOffsetOfNumber(%li)=%12li",go,groundOffsetOfNumber(go));
- }
- void demo04() {
- long go;
- printf("\n\nEnter the size of STJ array which ground offset you want know: ");
- scanf("%li",&go);
- printf("\n groundOffsetOfNumber(%li)=%12li",go,groundOffsetOfarraySTJ(go));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement