Special divisors: Difference between revisions
m (→{{header|REXX}}: added the computer programming language REXX.) |
|||
Line 4: | Line 4: | ||
Numbers n such that reverse(d) divides reverse(n) for all divisors d of n, where '''n < 200''' |
Numbers n such that reverse(d) divides reverse(n) for all divisors d of n, where '''n < 200''' |
||
<br><br> |
<br><br> |
||
=={{header|Phix}}== |
|||
<!--<lang Phix>--> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">rev</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #000000;">r</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">special_divisors</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">rn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rev</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rev</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">200</span><span style="color: #0000FF;">),</span><span style="color: #000000;">special_divisors</span><span style="color: #0000FF;">)})</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d special divisors:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18</span><span style="color: #0000FF;">)})</span> |
|||
<!--</lang>--> |
|||
{{out}} |
|||
<pre> |
|||
Found 72 special divisors: |
|||
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 |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang perl6>use Prime::Factor:ver<0.3.0+>; |
<lang perl6>use Prime::Factor:ver<0.3.0+>; |
Revision as of 06:37, 31 March 2021
- Task
Numbers n such that reverse(d) divides reverse(n) for all divisors d of n, where n < 200
Phix
function rev(integer n) integer r = 0 while n do r = r*10+remainder(n,10) n = floor(n/10) end while return r end function function special_divisors(integer n) sequence fn = factors(n) if length(fn) then integer rn = rev(n) for i=1 to length(fn) do if remainder(rn,rev(fn[i])) then return false end if end for end if return true end function sequence res = apply(true,sprintf,{{"%3d"},filter(tagset(200),special_divisors)}) printf(1,"Found %d special divisors:\n%s\n",{length(res),join_by(res,1,18)})
- Output:
Found 72 special divisors: 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
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...