I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Special Divisors

From Rosetta Code
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

ALGOL 68[edit]

BEGIN # find numbers where reverse(d) divides reverse(n) for all divisors d #
# of n #
# returns n with the digits reversed #
OP REVERSE = ( INT n )INT:
BEGIN
INT reverse := 0;
INT v := ABS n;
WHILE v > 0 DO
reverse *:= 10 +:= v MOD 10;
v OVERAB 10
OD;
reverse * SIGN n
END # REVERSE # ;
# find the numbers up to 200 #
INT rd count := 0;
FOR n TO 199 DO
INT reverse n = REVERSE n;
BOOL reverse divisor := TRUE;
FOR d FROM 2 TO n OVER 2 WHILE reverse divisor DO
IF n MOD d = 0 THEN
# have a divisor of n #
reverse divisor := reverse n MOD REVERSE d = 0
FI
OD;
IF reverse divisor THEN
# all the divisors of n reversed divide n reversed #
print( ( " ", whole( n, -3 ) ) );
IF ( rd count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
FI
OD;
print( ( newline, "Found ", whole( rd count, 0 ), " ""special divisors"" below 200", newline ) )
END
Output:
   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
Found 72 "special divisors" below 200

ALGOL W[edit]

Translation of: ALGOL 68
begin % find numbers where reverse(d) divides reverse(n) for all divisors d %
 % of n  %
 % returns n with the digits reversed  %
integer procedure reverse ( integer value n ) ;
begin
integer r, v;
r := 0;
v := abs n;
while v > 0 do begin
r := ( r * 10 ) + ( v rem 10 );
v := v div 10
end while_v_gt_0 ;
if n < 0 then - r else r
end reverse ;
 % find the numbers up to 200  %
integer rdCount;
rdCount := 0;
for n := 1 until 199 do begin
integer reverseN, d, maxD;
logical reverseDivisor;
reverseN  := reverse( n );
reverseDivisor := true;
d  := 1;
maxD  := n div 2;
while begin
d := d + 1;
d <= maxD and reverseDivisor
end
do begin
if n rem d = 0 then begin
 % have a divisor of n  %
reverseDivisor := reverseN rem reverse( d ) = 0
end if_n_rem_d_eq_0
end while_d_le_maxD_and_reverseDivisor ;
if reverseDivisor then begin
 % all the divisors of n reversed divide n reversed  %
writeon( i_w := 3, s_w := 0, " ", n );
rdCount := rdCount + 1;
if rdCount rem 10 = 0 then write()
end if_reverseDivisor
end for_n ;
write( i_w := 1, s_w := 0, "Found ", rdCount, " ""special divisors"" below 200" )
end.
Output:

Same as the Algol 68 sample.

APL[edit]

Works with: Dyalog APL
(⊢(/⍨)(0∧.=(⍎⌽∘⍕)¨∘(⍸0=⍳|⊢)|(⍎⌽∘⍕))¨) ⍳200
Output:
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

AppleScript[edit]

on factors(n)
set output to {}
 
if (n > 0) then
set sqrt to n ^ 0.5
set limit to sqrt div 1
if (limit = sqrt) then
set end of output to limit
set limit to limit - 1
end if
repeat with i from limit to 1 by -1
if (n mod i is 0) then
set beginning of output to i
set end of output to n div i
end if
end repeat
end if
 
return output
end factors
 
on reversedIntVal(n, base)
set r to n mod base as integer
set n to n div base
repeat until (n = 0)
set r to r * base + n mod base
set n to n div base
end repeat
 
return r
end reversedIntVal
 
on hasSpecialDivisors(n, base)
set divisors to factors(n)
if (divisors is {}) then return false
set r to reversedIntVal(n, base)
repeat with d in divisors
if (r mod (reversedIntVal(d, base)) > 0) then return false
end repeat
 
return true
end hasSpecialDivisors
 
local output, base, n
set output to {}
set base to 10
repeat with n from 1 to 199
if (hasSpecialDivisors(n, base)) then set end of output to n
end repeat
return {|count|:(count output), finds:output}
Output:
{|count|:72, finds:{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}}

BASIC[edit]

10 DEFINT A-Z
20 FOR I=1 TO 199
30 J=I: X=0
40 IF J>0 THEN X=X*10+J MOD 10: J=J\10: GOTO 40
50 FOR J=1 TO I\2
60 IF I MOD J GOTO 100
70 K=J: Y=0
80 IF K>0 THEN Y=Y*10+K MOD 10: K=K\10: GOTO 80
90 IF X MOD Y GOTO 120
100 NEXT J
110 PRINT I,
120 NEXT I
Output:
 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

BCPL[edit]

get "libhdr"
 
let reverse(n) = valof
$( let r = 0
while n > 0
$( r := r*10 + n rem 10
n := n/10
$)
resultis r
$)
 
let special(n) = valof
$( let r = reverse(n)
for d = 1 to n/2
if n rem d = 0 & r rem reverse(d) ~= 0
resultis false
resultis true
$)
 
let start() be
$( let c = 0
for n = 1 to 199
if special(n)
$( writed(n,4)
c := c + 1
if c = 10
$( wrch('*N')
c := 0
$)
$)
wrch('*N')
$)
Output:
   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

C[edit]

Translation of: Delphi
#include <stdbool.h>
#include <stdio.h>
 
int reverse(int n) {
int result = 0;
while (n > 0) {
result = 10 * result + n % 10;
n /= 10;
}
return result;
}
 
int main() {
const int limit1 = 200;
 
int row = 0;
int num = 0;
int n;
 
for (n = 1; n < limit1; n++) {
bool flag = true;
int revNum = reverse(n);
int m;
 
for (m = 1; m < n / 2; m++) {
int revDiv = reverse(m);
if (n % m == 0) {
if (revNum % revDiv == 0) {
flag = true;
} else {
flag = false;
break;
}
}
}
 
if (flag) {
num++;
row++;
printf("%4d ", n);
if (row % 10 == 0) {
printf("\n");
}
}
}
 
printf("\n\nFound %d special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200\n", num);
 
return 0;
}
Output:
   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

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

C++[edit]

#include <iostream>
#include <iomanip>
#include <vector>
 
using uint = unsigned int;
 
std::vector<uint> divisors(uint n) {
std::vector<uint> divs;
for (uint d=1; d<=n/2; d++) {
if (n % d == 0) divs.push_back(d);
}
return divs;
}
 
uint reverse(uint n) {
uint r;
for (r = 0; n; n /= 10) r = (r*10) + (n%10);
return r;
}
 
bool special(uint n) {
for (uint d : divisors(n))
if (reverse(n) % reverse(d) != 0) return false;
return true;
}
 
int main() {
for (uint n=1, c=0; n < 200; n++) {
if (special(n)) {
std::cout << std::setw(4) << n;
if (++c == 10) {
c = 0;
std::cout << std::endl;
}
}
}
std::cout << std::endl;
return 0;
}
Output:
   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

C#[edit]

Translation of: C
using System;
 
namespace SpecialDivisors {
class Program {
static int Reverse(int n) {
int result = 0;
while (n > 0) {
result = 10 * result + n % 10;
n /= 10;
}
return result;
}
 
static void Main() {
const int LIMIT = 200;
 
int row = 0;
int num = 0;
 
for (int n = 1; n < LIMIT; n++) {
bool flag = true;
int revNum = Reverse(n);
 
for (int m = 1; m < n / 2; m++) {
int revDiv = Reverse(m);
if (n % m == 0) {
if (revNum % revDiv == 0) {
flag = true;
} else {
flag = false;
break;
}
}
}
 
if (flag) {
num++;
row++;
Console.Write("{0,4}", n);
if (row % 10 == 0) {
Console.WriteLine();
}
}
}
 
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Found {0} special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200", num);
}
}
}
Output:
   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

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

COBOL[edit]

        IDENTIFICATION DIVISION.
PROGRAM-ID. SPECIAL-DIVISORS.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
02 CANDIDATE PIC 999.
02 CAND-REV PIC 999.
02 REVERSE PIC 999.
02 REV-DIGITS REDEFINES REVERSE PIC 9 OCCURS 3 TIMES.
02 DIVMAX PIC 999.
02 DIVISOR PIC 999.
02 DIVRSLT PIC 999V999.
02 FILLER REDEFINES DIVRSLT.
03 FILLER PIC 999.
03 FILLER PIC 999.
88 DIVISIBLE VALUE 0.
02 TEMP PIC 9.
02 RD PIC 9 COMP.
02 STATUS-FLAG PIC X.
88 OK VALUE 'Y'.
02 SPECIAL-N PIC ZZ9.
 
PROCEDURE DIVISION.
BEGIN.
PERFORM CHECK-SPECIAL-DIVISOR
VARYING CANDIDATE FROM 1 BY 1
UNTIL CANDIDATE IS EQUAL TO 200.
STOP RUN.
 
CHECK-SPECIAL-DIVISOR.
MOVE CANDIDATE TO REVERSE.
PERFORM REVERSE-NUMBER.
MOVE REVERSE TO CAND-REV.
DIVIDE CANDIDATE BY 2 GIVING DIVMAX.
MOVE 'Y' TO STATUS-FLAG.
PERFORM TRY-DIVISOR
VARYING DIVISOR FROM 1 BY 1
UNTIL DIVISOR IS GREATER THAN DIVMAX.
IF OK
MOVE CANDIDATE TO SPECIAL-N
DISPLAY SPECIAL-N.
 
TRY-DIVISOR.
IF OK
DIVIDE CANDIDATE BY DIVISOR GIVING DIVRSLT
IF DIVISIBLE
MOVE DIVISOR TO REVERSE
PERFORM REVERSE-NUMBER
DIVIDE CAND-REV BY REVERSE GIVING DIVRSLT
IF NOT DIVISIBLE MOVE 'N' TO STATUS-FLAG.
 
REVERSE-NUMBER.
SET RD TO 1.
INSPECT REVERSE TALLYING RD FOR LEADING '0'.
MOVE REV-DIGITS(RD) TO TEMP.
MOVE REV-DIGITS(3) TO REV-DIGITS(RD).
MOVE TEMP TO REV-DIGITS(3).
Output:
  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

Cowgol[edit]

include "cowgol.coh";
 
const MAXIMUM := 200;
typedef N is int(0, MAXIMUM);
 
sub reverse(n: N): (r: N) is
r := 0;
while n != 0 loop
r := r*10 + n%10;
n := n/10;
end loop;
end sub;
 
sub special(n: N): (r: uint8) is
r := 0;
var revn := reverse(n);
var dsor: N := 1;
while dsor <= n/2 loop
if n % dsor == 0 and revn % reverse(dsor) != 0 then
return;
end if;
dsor := dsor + 1;
end loop;
r := 1;
end sub;
 
var n: N := 1;
while n < MAXIMUM loop
if special(n) != 0 then
print_i32(n as uint32);
print_nl();
end if;
n := n + 1;
end loop;
Output:
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
149
151
157
163
167
173
179
181
191
193
197
199

Delphi[edit]

Translation of: Ring
program Special_Divisors;
{$IFDEF FPC}
{$MODE DELPHI}
uses
SysUtils,
StrUtils;
{$ELSE}
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.StrUtils;
{$ENDIF}
 
const
limit1 = 200;
 
var
row, num, revNum, revDiv: Integer;
flag: boolean;
 
procedure Main();
var
n,m: NativeUint;
begin
writeln('Working...'#10);
row := 0;
num := 0;
 
for n := 1 to limit1 do
begin
flag := True;
revNum := reversestring(n.ToString).ToInteger;
for m := 1 to n div 2 do
begin
revDiv := reversestring(m.ToString).ToInteger;
if n mod m = 0 then
if revNum mod revDiv = 0 then
flag := True
else
begin
flag := False;
Break;
end;
end;
 
if flag then
begin
inc(num);
inc(row);
write(n: 4);
if row mod 10 = 0 then
Writeln;
end;
end;
 
writeln(#10#10'Found ', num,
' special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200');
 
writeln('Done...');
end;
 
begin
Main;
{$IFNDEF UNIX} readln; {$ENDIF}
end.
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

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

Factor[edit]

Works with: Factor version 0.99 2021-02-05
USING: grouping kernel math.functions math.parser
math.primes.factors math.ranges prettyprint sequences ;
 
: reverse-number ( n -- reversed ) 10 >base reverse dec> ;
 
: special? ( n -- ? )
[ reverse-number ] [ divisors ] bi
[ reverse-number divisor? ] with all? ;
 
200 [1..b] [ special? ] filter 18 group simple-table.
Output:
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

Go[edit]

Translation of: Wren
Library: Go-rcu
package main
 
import (
"fmt"
"rcu"
)
 
func reversed(n int) int {
rev := 0
for n > 0 {
rev = rev*10 + n%10
n = n / 10
}
return rev
}
 
func main() {
var special []int
for n := 1; n < 200; n++ {
divs := rcu.Divisors(n)
revN := reversed(n)
all := true
for _, d := range divs {
if revN%reversed(d) != 0 {
all = false
break
}
}
if all {
special = append(special, n)
}
}
fmt.Println("Special divisors in the range 0..199:")
for i, n := range special {
fmt.Printf("%3d ", n)
if (i+1)%12 == 0 {
fmt.Println()
}
}
fmt.Printf("\n%d special divisors found.\n", len(special))
}
Output:
Special divisors in the range 0..199:
  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 

72 special divisors found.

J[edit]

([#~([:*./0=|.&.":"0@>:@[email protected](0=>:@i.|])||.&.":)"0)>:i.200
Output:
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

jq[edit]

Works with: jq

Works with gojq, the Go implementation of jq

 
# divisors as an unsorted stream
def divisors:
if . == 1 then 1
else . as $n
| label $out
| range(1; $n) as $i
| ($i * $i) as $i2
| if $i2 > $n then break $out
else if $i2 == $n then $i
elif ($n % $i) == 0 then $i, ($n/$i)
else empty
end
end
end;
 
def is_special_divisor:
def reverse_number: tostring|explode|reverse|implode|tonumber;
reverse_number as $nreverse
| all(divisors; $nreverse % reverse_number == 0);
 
range(1;200) | select(is_special_divisor)
Output:

A stream of numbers as shown elsewhere on this page.

Julia[edit]

using Primes
 
function divisors(n)
f = [one(n)]
for (p,e) in factor(n)
f = reduce(vcat, [f*p^j for j in 1:e], init=f)
end
return f[1:end-1]
end
 
function isspecialdivisor(n)::Bool
isprime(n) && return true
nreverse = evalpoly(10, reverse(digits(n)))
for d in divisors(n)
dreverse = evalpoly(10, reverse(digits(d)))
 !(nreverse ÷ dreverse ≈ nreverse / dreverse) && return false
end
return true
end
 
const specials = filter(isspecialdivisor, 1:200)
foreach(p -> print(rpad(p[2], 4), p[1] % 18 == 0 ? "\n" : ""), enumerate(specials))
 
Output:
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

MAD[edit]

            NORMAL MODE IS INTEGER
 
INTERNAL FUNCTION(X)
ENTRY TO RVRSE.
XR = X
RR = 0
LOOP WHENEVER XR.E.0, FUNCTION RETURN RR
XD = XR/10
RR = RR*10 + XR-XD*10
XR = XD
TRANSFER TO LOOP
END OF FUNCTION
 
THROUGH CAND, FOR N=1, 1, N.GE.200
RN = RVRSE.(N)
THROUGH DIVS, FOR D=1, 1, D.G.N/2
RD = RVRSE.(D)
DIVS WHENEVER N/D*D.E.N .AND. RN/RD*RD.NE.RN, TRANSFER TO CAND
PRINT FORMAT FMT,N
CAND CONTINUE
VECTOR VALUES FMT = $I4*$
END OF PROGRAM
Output:
   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

Mathematica/Wolfram Language[edit]

SpecialDivisorQ[n_Integer] := AllTrue[Divisors[n], Divisible[IntegerReverse[n], IntegerReverse[#]] &]
Select[Range[199], SpecialDivisorQ]
Length[%]
Output:
{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}
72

Nim[edit]

import strutils
 
func reversed(n: Positive): int =
var n = n.int
while n != 0:
result = 10 * result + n mod 10
n = n div 10
 
func divisors(n: Positive): seq[int] =
result = @[1, n]
var d = 2
while d * d <= n:
if n mod d == 0:
result.add d
if d * d != n:
result.add n div d
inc d
 
var count = 0
for n in 1..<200:
let revn = reversed(n)
block check:
for d in divisors(n):
if revn mod reversed(d) != 0:
break check
inc count
stdout.write ($n).align(3), if count mod 12 == 0: '\n' else: ' '
Output:
  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

Pascal[edit]

see http://rosettacode.org/wiki/Special_Divisors#Delphi%7CDelphi

Perl[edit]

Library: ntheory
use strict;
use warnings;
use feature 'say';
use ntheory 'divisors';
 
my @sd;
for my $n (1..199) {
map { next if $_ != int $_ } map { reverse($n) / reverse $_ } divisors $n;
push @sd, $n;
}
 
say @sd . " matching numbers:\n" .
(sprintf "@{['%4d' x @sd]}", @sd) =~ s/(.{40})/$1\n/gr;
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

Phix[edit]

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

PL/I[edit]

specialDivisors: procedure options(main);
 %replace MAX by 200;
 
reverse: procedure(nn) returns(fixed);
declare (r, n, nn) fixed;
r = 0;
do n=nn repeat(n/10) while(n > 0);
r = r*10 + mod(n, 10);
end;
return(r);
end reverse;
 
isSpecial: procedure(n) returns(bit);
declare (n, rev, div) fixed;
rev = reverse(n);
do div=1 to n/2;
if mod(n, div)=0 & mod(rev, reverse(div))^=0 then
return('0'b);
end;
return('1'b);
end isSpecial;
 
declare (cand, col) fixed;
col = 0;
do cand=1 to MAX;
if isSpecial(cand) then do;
put edit(cand) (F(4));
col = col+1;
if mod(col, 10)=0 then put skip;
end;
end;
end specialDivisors;
Output:
   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

PL/M[edit]

100H: /* FIND NUMBERS WHOSE REVERSED DIVISORS DIVIDE THE REVERSED NUMBER    */
 
DECLARE TRUE LITERALLY '0FFH';
DECLARE FALSE LITERALLY '0';
 
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
DECLARE FN BYTE, ARG ADDRESS;
GOTO 5;
END BDOS;
PRINT$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PRINT$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PRINT$NL: PROCEDURE; CALL PRINT$STRING( .( 0DH, 0AH, '$' ) ); END;
PRINT$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PRINT$STRING( .N$STR( W ) );
END PRINT$NUMBER;
 
REVERSE: PROCEDURE( N )ADDRESS; /* RETURNS THE REVERSED DIGITS OF N */
DECLARE N ADDRESS;
DECLARE ( R, V ) ADDRESS;
V = N;
R = V MOD 10;
DO WHILE( ( V := V / 10 ) > 0 );
R = ( R * 10 ) + ( V MOD 10 );
END;
RETURN R;
END REVERSE ;
 
/* FIND AND SHOW THE NUMBERS UP TO 200 */
DECLARE MAX$SD LITERALLY '199';
DECLARE ( N, RN, SD$COUNT, D, D$MAX ) ADDRESS;
DECLARE IS$SD BYTE;
SD$COUNT = 0;
DO N = 1 TO MAX$SD;
RN = REVERSE( N );
IS$SD = TRUE;
D = 2; D$MAX = N / 2;
DO WHILE( IS$SD AND D < D$MAX );
IF N MOD D = 0 THEN DO;
/* HAVE A DIVISOR OF N */
IS$SD = ( RN MOD REVERSE( D ) = 0 );
END;
D = D + 1;
END;
IF IS$SD THEN DO;
/* ALL THE REVERSED DIVISORS OF N DIVIDE N REVERSED */
CALL PRINT$CHAR( ' ' );
IF N < 100 THEN DO;
CALL PRINT$CHAR( ' ' );
IF N < 10 THEN CALL PRINT$CHAR( ' ' );
END;
CALL PRINT$NUMBER( N );
IF ( SD$COUNT := SD$COUNT + 1 ) MOD 10 = 0 THEN CALL PRINT$NL;
END;
END;
CALL PRINT$NL;
CALL PRINT$STRING( .'FOUND $' );
CALL PRINT$NUMBER( SD$COUNT );
CALL PRINT$STRING( .' ''''SPECIAL DIVISORS'''' BELOW $' );
CALL PRINT$NUMBER( MAX$SD + 1 );
CALL PRINT$NL;
EOF
Output:
   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
FOUND 72 ''SPECIAL DIVISORS'' BELOW 200

Raku[edit]

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 };
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[edit]

/*REXX program finds special divisors:   numbers  N  such that  reverse(D)  divides ··· */
/*────────────────────────── reverse(N) for all divisors D of N, where N < 200. */
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. */
title= ' special divisors N that reverse(D) divides reverse(N) for all divisors' ,
' D of N, where N < ' hi
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0; idx= 1 /*initialize # found numbers and index.*/
$= /*a list of numbers found (so far). */
do j=1 for hi-1; r= reverse(j) /*search for special divisors. */
do k=2 to j%2 /*skip the first divisor (unity) & last*/
if j//k==0 then if r//reverse(k)\==0 then iterate J /*Not OK? Skip*/
end /*m*/
found= found+1 /*bump the number of special divisors. */
if cols<0 then iterate /*Build the list (to be shown later)? */
$= $ right(j, w) /*add a special divisor ──► the $ list.*/
if found//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.*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' found title
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[edit]

 
load "stdlib.ring"
 
see "working..." + nl
 
row = 0
num = 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
num = num + 1
row = row + 1
see "" + n + " "
if row%10 = 0
see nl
ok
ok
next
 
see nl + "Found " + num + " special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200" + nl
see "done..." + nl
 
func rever(str)
rev = ""
for n = len(str) to 1 step -1
rev = rev + str[n]
next
return rev
 
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 
Found 72 special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where  N  <  200
done...

Sidef[edit]

1..200 -> grep {|n| n.divisors.all {|d| d.flip `divides` n.flip } }.say
Output:
[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]

Swift[edit]

import Foundation
 
func reverse(_ number: Int) -> Int {
var rev = 0
var n = number
while n > 0 {
rev = rev * 10 + n % 10
n /= 10
}
return rev
}
 
func special(_ number: Int) -> Bool {
var n = 2
let rev = reverse(number)
while n * n <= number {
if number % n == 0 {
if rev % reverse(n) != 0 {
return false
}
let m = number / n
if m != n && rev % reverse(m) != 0 {
return false
}
}
n += 1
}
return true
}
 
var count = 0
for n in 1..<200 {
if special(n) {
count += 1
print(String(format: "%3d", n),
terminator: count % 10 == 0 ? "\n" : " ")
}
}
print("\n\(count) numbers found.")
Output:
  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 
72 numbers found.

Wren[edit]

Library: Wren-math
Library: Wren-seq
Library: Wren-fmt
import "/math" for Int
import "/seq" for Lst
import "/fmt" for Fmt
 
var reversed = Fn.new { |n|
var rev = 0
while (n > 0) {
rev = rev * 10 + n % 10
n = (n/10).floor
}
return rev
}
 
var special = []
for (n in 1...200) {
var divs = Int.divisors(n)
var revN = reversed.call(n)
if (divs.all { |d| revN % reversed.call(d) == 0 }) special.add(n)
}
System.print("Special divisors in the range 0..199:")
for (chunk in Lst.chunks(special, 12)) Fmt.print("$3d", chunk)
System.print("\n%(special.count) special divisors found.")
Output:
Special divisors in the range 0..199:
  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

72 special divisors found.

XPL0[edit]

func Reverse(N);        \Reverse the order of the digits
int N, M;
[M:= 0;
repeat N:= N/10;
M:= M*10 + rem(0);
until N = 0;
return M;
];
 
func Test(N);
\Return 'true' if reverse(D) divides reverse(N) for all divisors D of N
int N, D, RevNum, RevDiv;
[RevNum:= Reverse(N);
for D:= 1 to N/2 do
if rem(N/D) = 0 then
[RevDiv:= Reverse(D);
if rem(RevNum/RevDiv) then return false;
];
return true;
];
 
int Count, N;
[Count:= 0;
for N:= 1 to 199 do
[if Test(N) then
[IntOut(0, N);
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
];
CrLf(0);
IntOut(0, Count);
Text(0, " such numbers found.");
]
Output:
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     
72 such numbers found.

Zig[edit]

const MAX = 200;  // max number to check
const N = u16; // smallest integer type that fits
 
pub fn reverse(n: N) N {
var r: N = 0;
var nn = n;
while (nn > 0) : (nn /= 10)
r = r*10 + nn%10;
return r;
}
 
pub fn special(n: N) bool {
var r = reverse(n);
var d: N = 1;
while (d <= n/2) : (d += 1)
if (n % d == 0 and r % reverse(d) != 0)
return false;
return true;
}
 
pub fn main() !void {
const stdout = @import("std").io.getStdOut().writer();
 
var c: N = 0;
var n: N = 1;
while (n <= MAX) : (n += 1) {
if (special(n)) {
try stdout.print("{d:4}", .{n});
c += 1;
if (c % 10 == 0) try stdout.print("\n", .{});
}
}
try stdout.print("\n", .{});
}
Output:
   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