Special divisors: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Raku}}: Add a Raku example)
m (→‎{{header|REXX}}: added the computer programming language REXX.)
Line 19: Line 19:
157 163 167 169 173 179 181 187 191 193
157 163 167 169 173 179 181 187 191 193
197 199</pre>
197 199</pre>

=={{header|REXX}}==
<lang rexx>/*REXX program finds special divisorsa: numbers N such that reverse(D) divides */
/*───────────────────────────────────────────── reverse(N) for all divisors D of N. */
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 200 /* " " " " " " */
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 10 /*width of a number in any column. */
@sds= ' special divisors N that reverse(D) divides reverse(N)' ,
'for all divisors D of N, where N < ' commas(hi)
if cols>0 then say ' index │'center(@sds, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
sds= 0; idx= 1 /*initialize # of nice primes and index*/
$= /*a list of nice primes (so far). */
do j=1 to hi-1; r= reverse(j)
do k=2 to j%2 /*skip the first divisor (unity) & last*/
if j//k==0 then if r//reverse(k)==0 then nop
else iterate j
end /*m*/
sds= sds + 1 /*bump the number of sds numbers. */
if cols==0 then iterate /*Build the list (to be shown later)? */
c= commas(j) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add a nice prime ──► list, allow big#*/
if sds//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/

if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say
say 'Found ' commas(sds) @sds
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 1 2 3 4 5 6 7 8 9 11
11 │ 13 17 19 22 23 26 27 29 31 33
21 │ 37 39 41 43 44 46 47 53 55 59
31 │ 61 62 66 67 69 71 73 77 79 82
41 │ 83 86 88 89 93 97 99 101 103 107
51 │ 109 113 121 127 131 137 139 143 149 151
61 │ 157 163 167 169 173 179 181 187 191 193
71 │ 197 199

Found 72 special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200
</pre>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 19:49, 30 March 2021

Special divisors is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Numbers n such that reverse(d) divides reverse(n) for all divisors d of n, where n < 200

Raku

<lang perl6>use Prime::Factor:ver<0.3.0+>;

say "{+$_} matching numbers:\n{.batch(10)».fmt('%3d').join: "\n"}"

   given (1..^200).grep: { all .flip «%%« .&divisors».flip };</lang>
Output:
72 matching numbers:
  1   2   3   4   5   6   7   8   9  11
 13  17  19  22  23  26  27  29  31  33
 37  39  41  43  44  46  47  53  55  59
 61  62  66  67  69  71  73  77  79  82
 83  86  88  89  93  97  99 101 103 107
109 113 121 127 131 137 139 143 149 151
157 163 167 169 173 179 181 187 191 193
197 199

REXX

<lang rexx>/*REXX program finds special divisorsa: numbers N such that reverse(D) divides */ /*───────────────────────────────────────────── reverse(N) for all divisors D of N. */ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 200 /* " " " " " " */ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */

                      @sds= ' special divisors  N  that reverse(D) divides reverse(N)' ,
                            'for all divisors  D  of  N,  where  N  < '      commas(hi)

if cols>0 then say ' index │'center(@sds, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') sds= 0; idx= 1 /*initialize # of nice primes and index*/ $= /*a list of nice primes (so far). */

    do j=1  to  hi-1;       r= reverse(j)
        do k=2  to j%2                          /*skip the first divisor (unity) & last*/
        if j//k==0  then if r//reverse(k)==0  then nop
                                              else iterate j
        end   /*m*/
    sds= sds + 1                                /*bump the number of  sds  numbers.    */
    if cols==0            then iterate          /*Build the list  (to be shown later)? */
    c= commas(j)                                /*maybe add commas to the number.      */
    $= $ right(c, max(w, length(c) ) )          /*add a nice prime ──► list, allow big#*/
    if sds//cols\==0      then iterate          /*have we populated a line of output?  */
    say center(idx, 7)'│'  substr($, 2);   $=   /*display what we have so far  (cols). */
    idx= idx + cols                             /*bump the  index  count for the output*/
    end       /*j*/

if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say say 'Found ' commas(sds) @sds exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>

output   when using the default inputs:
 index │     special divisors  N  that reverse(D) divides reverse(N) for all divisors  D  of  N,  where  N  <  200
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │          1          2          3          4          5          6          7          8          9         11
  11   │         13         17         19         22         23         26         27         29         31         33
  21   │         37         39         41         43         44         46         47         53         55         59
  31   │         61         62         66         67         69         71         73         77         79         82
  41   │         83         86         88         89         93         97         99        101        103        107
  51   │        109        113        121        127        131        137        139        143        149        151
  61   │        157        163        167        169        173        179        181        187        191        193
  71   │        197        199

Found  72  special divisors  N  that reverse(D) divides reverse(N) for all divisors  D  of  N,  where  N  <  200

Ring

<lang ring> load "stdlib.ring"

see "working..." + nl

row = 0 limit1 = 200

for n = 1 to limit1

   flag = 1
   revNum = rever(string(n))
   revNum = number(revNum)
   for m = 1 to n/2
       revDiv = rever(String(m))
       revDiv = number(revDiv)
       if n%m = 0
          if revNum % revDiv = 0
             flag = 1
          else
             flag = 0
             exit
          ok
       ok
   next 
   if flag = 1
      row = row + 1
      see "" + n + " "
      if row%10 = 0
         see nl
      ok
   ok

next

see nl + "done..." + nl

func rever(str)

    rev = ""
    for n = len(str) to 1 step -1
        rev = rev + str[n]
    next
    return rev  

</lang>

Output:
working...
1 2 3 4 5 6 7 8 9 11 
13 17 19 22 23 26 27 29 31 33 
37 39 41 43 44 46 47 53 55 59 
61 62 66 67 69 71 73 77 79 82 
83 86 88 89 93 97 99 101 103 107 
109 113 121 127 131 137 139 143 149 151 
157 163 167 169 173 179 181 187 191 193 
197 199 
done...