Advertisement
algorithmuscanorj

Untitled

Dec 28th, 2012
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.90 KB | None | 0 0
  1. (*
  2.  
  3. Revision: Dec 28 2012, Inluding this program as helper utility for secuencing: A185310.
  4.  
  5. Location URL: http://www.oeis.org/A185310
  6.  
  7. ------------------------------------------------------------------------------------------------------------------
  8. ------------------------------------------------------------------------------------------------------------------
  9.  Begin: Dump of the PARI sourcecode for use with this program.
  10. ------------------------------------------------------------------------------------------------------------------
  11. ------------------------------------------------------------------------------------------------------------------
  12.  
  13. /*------ Notes:
  14.  
  15. 0) Compile and use the external program permworep.pas to generate the data file.
  16.  
  17. .........Command line call exmaple: cano@arithmoz$ ./permworep 9
  18.  
  19. 1) This sequencer requires the file "permworep_idata.txt" generated externally.
  20.  
  21. 2) It might be necessary to call "allocatemem()" more than 1 time or redefine properly the stacksize when invoking the gp interpreter.
  22.  
  23. -------- Executable code below: */
  24.  
  25. inputdata=readvec("permworep_idata.txt");
  26.  
  27. a=vector(#inputdata, kappa, (inputdata[kappa]-inputdata[1])\9);
  28.  
  29. for(k=1, #a, print(a[k]));
  30.  
  31. quit;
  32.  
  33. ------------------------------------------------------------------------------------------------------------------
  34. ------------------------------------------------------------------------------------------------------------------
  35.  End: Dump of the PARI sourcecode for use with this program.
  36. ------------------------------------------------------------------------------------------------------------------
  37. ------------------------------------------------------------------------------------------------------------------
  38.  
  39.  
  40. ------------------------------------------------------------------------------------------------------------------
  41.  
  42.  Revision: Dec 22 2012 New age dawn! :-D
  43.  
  44.     -Minor corrections made inside the comments for clarity and readability.
  45.     -A not used variable detected and deleted.
  46.    
  47.  For example if you want to obtain the "natural" permutations for base-13, this
  48.  is for the decimal digits and a,b,c notice that c=12 regarded as digit in the
  49.  conventional notation, but since we started here to count from zero, each latin
  50.  alphabet letter have its conventional value +1 when it is used for counts.
  51.  
  52.  Then follow the usual instructions given below (please read them), and call:
  53.  
  54.   --------------------------------------------------------[Linux Shell example]
  55.  
  56.   gandalf@rivendell@sh$ permworep c
  57.  
  58.   ----------------------------------------------------------------------------
  59.  
  60.   Written on my host machine:
  61.  
  62.   * 32-bit Intel Atom with 160Gb Hdd 1Gb Ram,
  63.   * Slackware GNU Linux distro version 14
  64.   * Free Pascal Compiler 2.6.2
  65.  
  66.   Formerly was http://pastebin.com/ZCLc63XG... all bogus bugs apparently fixed.
  67.  
  68. *)
  69.  
  70. program PermutationsWithoutRepetitions;
  71.  
  72. (*
  73.  
  74.   USAGE:
  75.   -----
  76.  
  77.    i] Compile this sourcecode and place the executable in a proper location.
  78.       (It is strongly suggested to use the FPC compiler. Please visit:
  79.       freepascal.org)
  80.    
  81.   ii] Create a empty file called "permworep_idata.txt" and fill it just with
  82.       a sigle zero. Save it in the same directory for the executable mentioned
  83.       in [i] and ensure that there will be granted read-write permissions
  84.       for this program during its execution.
  85.      
  86.  iii] For obtaining the 10! permutations for the first 10 non-negative integers
  87.       enumerated starting from zero, call the program passing "9" as its unique
  88.       parameter.
  89.      
  90.       (This is due zero is the first digit in every positional number system)
  91.      
  92.       Instead of using double byte numbers like 11 or 16, use the letter
  93.       representing it as digit in a base greater than the decimal. The program
  94.       is theoretically limited up to 36 digits enumerated from zero.
  95.  
  96.   iv] Depending on the resources available during the execution, this program
  97.       might not work properly as expected, being interrumped or crashing for
  98.       example when used to computed sets of permutations greater than 10 ("a"
  99.       letter).
  100.  
  101.  Written by R. J. Cano, <aallggoorriitthhmmuuss@gmail.com> on Dec 9th 2012.
  102.  
  103.  Observation: There exists better known algorithms/methods for computing
  104.  pemutations. The present program is merely another one more among the possible
  105.  implementations for doing such jobs.
  106.  
  107.  See for example the entry A055881 at OEIS.org, specifically the comment
  108.  by Joerg Arndt.
  109.  
  110.  The purpose on writing this program was to serve as ilustration for the
  111.  algorithm known as Steinhaus-Jhonson-Trotter, and additionally about how
  112.  to do these thing using the hard disk instead the primary memory.
  113.  
  114.  Important: The present program is released under the terms
  115.             of the GNU General Public License 3.0 and it goes
  116.             without any warranty whatsoever.
  117.  
  118. *)
  119.  
  120. type
  121.  my_string=string[15];
  122.  (* Mem. saving measure: Limit only 16 bytes per string.
  123.     Up to the first 15! permutations only *)
  124.  
  125. const
  126.  basename:string='permworep';
  127.  digit:array [0..35] of char=
  128.    ('0','1','2','3','4','5','6','7','8','9',
  129.     'a','b','c','d','e','f','g','h','i','j',
  130.     'k','l','m','n','o','p','q','r','s','t',
  131.     'u','v','w','x','y','z');
  132. var
  133.  f_input,
  134.  f_output : text;
  135.  input,
  136.  output,
  137.  request:my_string;
  138.  size_i,
  139.  size_o,
  140.  index_a,
  141.  index_b:byte;
  142.  index_c,
  143.  dir:shortint;
  144.  
  145. function f(x:char):byte;
  146. var ans:byte;
  147. begin
  148.   if (x in ['a'..'z']) then begin
  149.     ans:=10+ord(x)-ord('a');
  150.   end else if (x in ['0'..'9']) then begin
  151.     ans:=ord(x)-ord('0');
  152.   end else begin
  153.     ans:= 255;
  154.   end;
  155.   f:=ans;
  156. end;
  157.  
  158. begin
  159.  if (paramcount = 1) then begin
  160.   request:=paramstr(1);
  161.   size_o:=f(request[length(request)])+1;
  162.   assign(f_input,basename+'_idata.txt');
  163.   assign(f_output,basename+'_odata.txt');
  164.   reset(f_input);
  165.   readln(f_input,input);
  166.   close(f_input);
  167.   size_i:=f(input[length(input)])+1;
  168.   while (size_o > size_i) do begin
  169.    dir:=-1;
  170.    assign(f_input,basename+'_idata.txt');
  171.    assign(f_output,basename+'_odata.txt');
  172.    reset(f_input);
  173.    rewrite(f_output);
  174.    while (not eof(f_input)) do begin
  175.      readln(f_input,input);
  176.      for index_a:= 0 to size_i do begin
  177.        output:='';
  178.        index_c:=-1;
  179.        for index_b:= 0 to size_i do begin
  180.          if ((dir=-1) and (index_a+index_b=size_i)) then begin
  181.            output:=output+digit[size_i];
  182.          end else if ((dir=1) and (index_a=index_b)) then begin
  183.            output:=output+digit[size_i];
  184.          end else begin
  185.            if (index_c=size_i-1) then index_c:=-1;
  186.            index_c:=index_c+1;
  187.            output:=output+input[index_c+1];
  188.          end;
  189.        end;
  190.        writeln(f_output,output);
  191.      end;
  192.      dir:= dir*(-1);
  193.    end;
  194.    close(f_input);
  195.    close(f_output);
  196.    erase(f_input);
  197.    assign(f_input,'tmp');
  198.    rename(f_output,basename+'_idata.txt');
  199.    size_i:= size_i + 1;
  200.   end;
  201.  end;
  202. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement