Advertisement
algorithmuscanorj

Some routines in C useful for working in C and A207324

Dec 28th, 2012
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. /*
  2.     2012 Sept. 15th, R.J. Cano <remy@ula.ve>
  3.     Program for working with the Steinhaus-Johnson-Trotter algorithm sequence.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. long innerBigBeta(long, long);
  10. long bigBeta(long);
  11. long groundOffsetOfNumber(long);
  12. long groundOffsetOfarraySTJ(long);
  13. void demo01();
  14. void demo02();
  15. void demo03();
  16. void demo04();
  17.  
  18. int main(void) {
  19.     //demo01();
  20.     //demo02();
  21.     //demo03();
  22.     demo04();
  23.     return EXIT_SUCCESS;
  24. }
  25.  
  26. long innerBigBeta(long K, long N) {
  27.     return (K==N) ? N : ( K+(K+1)*innerBigBeta(K+1, N) );
  28. }
  29.  
  30. /*
  31.     A nice recursive-like function to compute the sum: (LaTEX) \beta = \sum_{k=1}^{N}\left(k!k\right)
  32. */
  33. long bigBeta(long N) {
  34.     return innerBigBeta(1,N);
  35. }
  36.  
  37. /*
  38.     The following pair of functions:
  39.    
  40.     Counts how many terms of the sequence are behind:
  41.         -The number N for the first time it appears.
  42.         -The STJ array sized by N.
  43.  
  44.     {################################...##NULL}
  45.     1
  46.     (Starting at head)
  47.    
  48.     x = groundOffsetOf...(N);
  49.    
  50.     move "x" times from head to tail.
  51.    
  52.     {...#######($)######################...#NULL}
  53.                 ^
  54.                 ^
  55.     (Target pointer: The first N appearing in the sequence or the [1,1] component
  56.     of the STJ array with size N)
  57.    
  58.     The time-expensive operation would be a first call to the reader fucntion of the
  59.     sequence. After a first call, every known pointer useful for our work may be stored
  60.     in a cache database as long as such information is needed. For example when computing
  61.     matrix determinants hundred or thousand of times for a size of those matrix not known at
  62.     compiling time, this strategy would works fine, fast and reliable.
  63.  
  64.     A concrete example:
  65.    
  66.     The concatenation of first 3 STJ arrays gives the following segment of the sequence:
  67.    
  68.     STJsequence= {1,1,2,2,1,1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
  69.    
  70.     It let us know a(n) for n in 1..23, and if we are interested only in 3x3
  71.     determinants, we need be placed at the [1,1] of the STJ(3) array, then dependig on
  72.     the programming enviroment and the particular names of the functions it is made
  73.     by some similar to this:
  74.    
  75.     ref_STJ3= stck_forward_offset(&STJsequence, groundOffsetOfarraySTJ(3));
  76.    
  77.     Where if a pointer called "tref" is used inside stck_forward_offset()
  78.     for moving,    it must invariably do this:
  79.  
  80.     (At beginning)
  81.    
  82.     STJsequence= {1,1,2,2,1,1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
  83.                   ^
  84.                   ^                  
  85.                   tref
  86.  
  87.     (After moving)
  88.    
  89.     STJsequence= {1,1,2,2,1,1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
  90.                             ^
  91.                             ^                  
  92.                             tref
  93.                 {1>2>3>4>5>(1),2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3}
  94.  
  95.     Because groundOffsetOfarraySTJ(3) returns: 5;
  96.    
  97.     That's all we really need to face as unknown. The rest of the job
  98.     will be finished by a careful application of the Leibnitz-Laplace
  99.     formula for determinants ("careful" refers to signatures).
  100.    
  101.     This is enough for numerical determinants and gives a scratch from
  102.     where it must not be difficult to implement optimizations, upgrades,
  103.     and why not, the developing of a CAS like symbolic determinant algorithm.
  104.    
  105. */
  106.  
  107. long groundOffsetOfNumber(long N) {
  108.     return (2 <= N) ? ( bigBeta(N-1) + (N-1) ) : 0;
  109. }
  110.  
  111. long groundOffsetOfarraySTJ(long N) {
  112.     return (2 <= N) ? ( bigBeta(N-1) ) : 0;
  113. }
  114.  
  115. void demo01() {
  116.     long n;
  117.     printf("\n\nEnter \"n\" for bigBeta(): ");
  118.     scanf("%li",&n);
  119.     printf("\nbigBeta(%li)=%12li",n,bigBeta(n));
  120. }
  121.  
  122. void demo02(){
  123.     long n, j;
  124.     printf("\n\nEnter an unpper bound for bigBeta sequence: ");
  125.     scanf("%li",&n);
  126.     printf("\n");
  127.     for (j=1;j<=n;j++) {
  128.         printf("\n\t n=%12li; \t bigBeta(n)=%12li",j,bigBeta(j));
  129.     }
  130. }
  131.  
  132. void demo03() {
  133.     long go;
  134.     printf("\n\nEnter the integer N which ground offset you want know: ");
  135.     scanf("%li",&go);
  136.     printf("\n groundOffsetOfNumber(%li)=%12li",go,groundOffsetOfNumber(go));
  137. }
  138.  
  139. void demo04() {
  140.     long go;
  141.     printf("\n\nEnter the size of STJ array which ground offset you want know: ");
  142.     scanf("%li",&go);
  143.     printf("\n groundOffsetOfNumber(%li)=%12li",go,groundOffsetOfarraySTJ(go));
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement