Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This demonstrative program should be used for either documentation or instructive purposes only,
- and goes without any warranty whatsoever.
- This is part of: The On-Line Encyclopedia Of Integer Sequences OEIS(TM)
- Visit: http://www.oeis.org/A055881
- Author: Joerg Arndt (www.jjj.de), Dec 12 2012
- Pastebin by: R. J. Cano (remy@ula.ve), Dec 17 2012
- */
- perm(n)=
- /* Compute the first n! terms of A055881 by counting
- in rising factorial base.
- Generate all permutations of n along the way.
- */
- {
- my( fc=vector(n) ); /* mixed radix numbers (rising factorial base) */
- my( ct=0, j, a=0 );
- my( p=vector(n,k,k-1) ); /* permutation */
- my( k, t ); /* for permutations */
- while ( 1,
- print1(ct, ": ", p); /* permutation */
- print1(" ", a ); /* A055881(ct): position of rightmost change in fc[] */
- print1(" ", fc); /* factorial number */
- print();
- ct += 1;
- /* increment factorial number fc[]: */
- j = 1;
- while ( fc[j] == j, fc[j]=0; j+=1; ); /* scan over nines */
- if ( j==n, return() ); /* current is last */
- fc[j] += 1;
- /* update permutation p[], reverse prefix of length j+1: */
- a = j; /* next term of A055881 */
- j += 1; k = 1;
- while ( k < j,
- t=p[j]; p[j]=p[k]; p[k]=t;
- k+=1; j-=1;
- );
- );
- }
- print("Demonstration: Reproduces the example given in the comments section for A055881");
- perm(4);
- print(" ");
- print("You are free to call perm() and experiment with it. When finished, type \"quit\" to exit.");
- print(" ");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement