factoradicals

No Language here - just algorithms!
Previous topicNext topic

Topic Author
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 892
Joined: Wed Sep 11, 2013 3:57 pm

factoradicals

Post by enrico-sorichetti » Thu Jun 25, 2015 8:01 pm

Code: Select all

#!  /usr/bin/rexx
Trace "O"
signal on novalue name novalue
numeric digits 32

src = "abcdefghijklmnopqrstuvwxyz"
src = "WOC"

parse arg n ord
if  n = "" then ,
    n = length(src)

if  n > length(src) then ,
   exit

l = length(FACT(n))

low = 0
max = fact(n) - 1
if   ord \= "" then ,
   if   ord > max then ,
      exit
   else do
      low = ord - 1
      max = ord - 1
      l   = ""
   end

do   m = low to max
   k = FRADICALS(m, n)
   r = 0
   do   i = 1 to k
   --   call charout , f.i"x"k-i"! +"
      r = r + fact(k-i)*f.i
   end
   -- say
   if   r \= m then do
      say "oh s**t!"
      exit
   end

   ans = ""
   tmp = left(src,n)
   do   i = 1 to k
      j = f.i + 1
      ans = ans || substr(tmp,j,1)
      tmp = delstr(tmp,j,1)
   end
   if l = "" then ,
      say m+1 ans
   else ,
      say right(m+1,l) ans
end

exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
FRADICALS:procedure expose F.
   parse arg M, N
   do   I =  1 to N
      F.I = 0
   end
   do   J = 2 while ( M \= 0 )
      I = N - J + 1
      F.I = M // J
      M   = M % J
   end
   return N
exit -1

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
FACT:procedure
   parse arg N
   if   N = 0 then ,
      return 1
   F = 1
   do   I = 1 to N
      F *= I
   end
   return F
exit -1

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
logic_error:
say "**"copies(" -", 35)
say "**"
say "**" "Logic error at line '"sigl"' "
say "**"
say "**"copies(" -", 35)
exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
novalue:
say "**"copies(" -", 35)
say "**"
say "**" "Novalue trapped, line '"sigl"' var '"condition("D")"' "
say "**"
say "**"copies(" -", 35)
exit



cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)

Previous topicNext topic

Return to “Programming Algorithms.”