Advertisement
algorithmuscanorj

Cat's police Dept. looking for the mices

Dec 22nd, 2012
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2.  
  3.   Revision: Dec 22 2012 New age dawn! :-D
  4.  
  5.     -Minor corrections made inside the comments for clarity and readability.
  6.  
  7.   Written on my host machine:
  8.  
  9.   * 32-bit Intel Atom with 160Gb Hdd 1Gb Ram,
  10.   * Slackware GNU Linux distro version 14
  11.   * Free Pascal Compiler 2.6.2
  12.  
  13. *)
  14.  
  15. program a_first_half_factorial_of_terms_for_a217626;
  16.  
  17. label
  18.   The_end;
  19.  
  20. (*
  21.  
  22. Devised and released for documentation purposes under the terms of use of
  23. "The On-Line Encyclopedia Of Integer Sequences" (OEIS.org).
  24.  
  25. For details please visit: http://www.oeis.org
  26.  
  27.  
  28. Dear user/reader:
  29. -----------------
  30.  
  31. When it is used properly, this code should help you to find the first (B!-1) terms
  32. of A217626, a task for which you need to have stored at least the first (B-1)! terms
  33. of A215940. For now it is assumed that everything is done reading the numbers in base B,
  34. and then translating such into decimal for verifying the definition of A215940 for finally
  35. converting the results back to base B, when it is guessed that the final answers are the
  36. terms for the base-B version of A217626.
  37.  
  38. The idea behind this processing was taken from a test session under PARI/GP like:
  39.  
  40. x=[6,0,1,2,3,4,5,7,8,9,10,11];
  41. z=sum(y=1,12,x[y]*12^(12-y));
  42. print(z);
  43.  
  44. This program is not expensive in lines of code and extra details such as tech checkings...
  45. It is assumed here that you know what you are doing when messing with it, for this
  46. subtle reason previously mentioned, this code goes without any warranty whatsoever.
  47.  
  48. *)
  49.  
  50. const
  51.   base=13;
  52.   faked_base=10;
  53.  
  54. type
  55.   ptr_int64= ^int64;
  56.   ptr_pizza= ^pizza;
  57.   pizza= array [1..base] of int64;
  58.  
  59. const
  60.   either_napole_or_sicilian: ptr_pizza = nil; (* PLEASE, always remember to initialize this properly!!! *)
  61.   swissknife: ptr_int64 = nil; (* The same principle applies here *)
  62.  
  63.   (*
  64.  
  65.     Dear user,
  66.    
  67.     Sorry: You need to change properly the following two arrays when modifying base.
  68.    
  69.     My apologizes for this.
  70.    
  71.   *)
  72.  
  73.   power: pizza=
  74.   (base*base*base*base*base*base*base*base*base*base*base*base,
  75.    base*base*base*base*base*base*base*base*base*base*base,
  76.    base*base*base*base*base*base*base*base*base*base,
  77.    base*base*base*base*base*base*base*base*base,
  78.    base*base*base*base*base*base*base*base,
  79.    base*base*base*base*base*base*base,
  80.    base*base*base*base*base*base,
  81.    base*base*base*base*base,
  82.    base*base*base*base,
  83.    base*base*base,
  84.    base*base,
  85.    base,
  86.    1);
  87.  
  88.   faked_power: pizza=
  89.   (faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base,
  90.    faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base,
  91.    faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base,
  92.    faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base,
  93.    faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base,
  94.    faked_base*faked_base*faked_base*faked_base*faked_base*faked_base*faked_base,
  95.    faked_base*faked_base*faked_base*faked_base*faked_base*faked_base,
  96.    faked_base*faked_base*faked_base*faked_base*faked_base,
  97.    faked_base*faked_base*faked_base*faked_base,
  98.    faked_base*faked_base*faked_base,
  99.    faked_base*faked_base,
  100.    faked_base,
  101.    1);
  102.    
  103. var
  104. vbase,
  105. vfaked_base: int64;
  106. input_f:text;
  107. input_s:string;
  108. delta_A_operand,
  109. delta_B_operand,
  110. current,
  111. last: int64;
  112. register: array [1..10000] of int64;
  113.  
  114. function f(x:char):byte;
  115. var
  116.   ans:byte;
  117. begin
  118.   if (x in ['a'..'z']) then begin
  119.     ans:=10+ord(x)-ord('a');
  120.   end else if (x in ['0'..'9']) then begin
  121.     ans:=ord(x)-ord('0');
  122.   end else begin
  123.     ans:= 255;
  124.   end;
  125.   f:=ans;
  126. end;
  127.  
  128. function g(y:string):int64;
  129. var
  130.   ans:int64;
  131.   k:byte;
  132. begin
  133.   ans:=0;
  134.   if (length(y) = base) then for k:= 1 to base do begin
  135.     ans:= ans + f(y[k])*either_napole_or_sicilian^[k];
  136.   end;
  137.   g:=ans;
  138. end;
  139.  
  140. function p(q:longint):int64;
  141. var
  142.   ans:int64;
  143.   u:longint;
  144. begin
  145.   ans:=1;
  146.   if (q >= 1) then begin
  147.      for u:= 1 to q do begin
  148.     ans:= ans*faked_base;
  149.      end;
  150.   end;
  151.   p:=ans;
  152. end;
  153.  
  154. function h(b:longint; z:int64):int64;
  155. var
  156.   ans,
  157.   cz:int64;
  158.   k,
  159.   w:longint;
  160. begin
  161.   ans:=z;
  162.   if (b <> faked_base) then begin
  163.     ans:=0;
  164.     cz:=z;
  165.     k:=1;
  166.     while (cz >= b) do begin
  167.       register[k]:= cz mod b;
  168.       cz:= cz div b;
  169.       k:=k+1;
  170.     end;
  171.     register[k]:=cz;
  172.     for w:= k downto 1 do begin
  173.       ans:= ans+register[w]*p(w-1);
  174.     end;
  175.   end;
  176.   h:=ans;
  177. end;
  178.  
  179. (*
  180.     The filename of the data intended to be processed here should be
  181.     passed as the first and unique parameter in the command line
  182.     invocation to this program. The output is made directly to the
  183.     console, so normally you should redirect it to a file in order
  184.     to save the answer (Just a term of A217626 in each line, like
  185.     in a b-file but without including the offsets at left)
  186. *)
  187.  
  188. begin
  189.  
  190.   vbase:= base; (* Don't touch this!*)
  191.   vfaked_base:= faked_base; (* Don't touch this!*)
  192.  
  193.   (* Do you want to see sparks?... disable the following enclosed block. ... It was a joke, dont do it.*)
  194.   (* * )
  195.   ( * *)  
  196.   if (vbase <= 10) then begin
  197.     either_napole_or_sicilian:= @faked_power;
  198.     swissknife:= @vfaked_base;
  199.   end else begin
  200.     either_napole_or_sicilian:= @power;
  201.     swissknife:= @vbase;
  202.   end;
  203.   (* * )
  204.   ( * *)  
  205.  
  206.   if (paramcount() = 1) then begin
  207.     assign(input_f, paramstr(1));
  208.     (*$i-*)reset(input_f);(*$i+*)
  209.     if (IOResult = 0) then begin
  210.       readln(input_f, input_s);
  211.       delta_A_operand:= g(input_s);
  212.       last:=0;
  213.       while (not eof(input_f)) do begin
  214.     readln(input_f,input_s);
  215.     delta_B_operand:= g(input_s);
  216.     current:= h(swissknife^, (delta_B_operand-delta_A_operand) div (swissknife^-1) );
  217.     writeln(current-last);
  218.     last:= current;
  219.       end;
  220.     end else begin
  221.       writeln('Unexpected error when processing: ',paramstr(1),'; **ABORTING**');
  222.       goto The_end;
  223.     end;
  224.     close(input_f);
  225.   end else begin
  226.     writeln('You must specify one and only one filename as invocation parameter. Please try again.');
  227.   end;
  228.   The_end:
  229. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement