Special divisors
Numbers n such that reverse(d) divides reverse(n) for all divisors d of n, where n < 200
- Task
ALGOL 68
<lang algol68>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</lang>
- 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
<lang algolw>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.</lang>
- Output:
Same as the Algol 68 sample.
APL
<lang APL>(⊢(/⍨)(0∧.=(⍎⌽∘⍕)¨∘(⍸0=⍳|⊢)|(⍎⌽∘⍕))¨) ⍳200</lang>
- 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
<lang applescript>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}</lang>
- Output:
<lang applescript>{|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}}</lang>
BASIC
<lang BASIC>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</lang>
- 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
<lang BCPL>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')
$)</lang>
- 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
<lang c>#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;
}</lang>
- 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++
<lang cpp>#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;
}</lang>
- 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
COBOL
<lang cobol> 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).</lang>
- 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
Delphi
<lang Delphi>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.</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 Found 72 special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200 Done...
Factor
<lang factor>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.</lang>
- 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
J
<lang J>([#~([:*./0=|.&.":"0@>:@I.@(0=>:@i.|])||.&.":)"0)>:i.200</lang>
- 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
Julia
<lang julia>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))
</lang>
- 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
<lang MAD> 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</lang>
- 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
Nim
<lang Nim>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: ' '</lang>
- 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
see http://rosettacode.org/wiki/Special_Divisors#Delphi%7CDelphi
Perl
<lang perl>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;</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
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
PL/M
<lang plm>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</lang>
- 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
<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 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 divisiors' ,
' D of N, where N < ' commas(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 numsers 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)? */ c= commas(j) /*maybe add commas to the number. */ $= $ right(c, max(w, length(c) ) ) /*add a special div ─► list, allow big#*/ 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 ' commas(found) title 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 divisiors 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 divisiors D of N, where N < 200
Ring
<lang ring> 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
</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 Found 72 special divisors N that reverse(D) divides reverse(N) for all divisors D of N, where N < 200 done...
Wren
<lang ecmascript>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.")</lang>
- 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
<lang XPL0>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."); ]</lang>
- 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.