Special divisors: Difference between revisions
no edit summary
No edit summary |
|||
(11 intermediate revisions by 7 users not shown) | |||
Line 6:
=={{header|Action!}}==
<
INT i
Line 55:
FI
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Special_Divisors.png Screenshot from Atari 8-bit computer]
Line 65:
=={{header|ALGOL 68}}==
<
# of n #
# returns n with the digits reversed #
Line 96:
OD;
print( ( newline, "Found ", whole( rd count, 0 ), " ""special divisors"" below 200", newline ) )
END</
{{out}}
<pre>
Line 112:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<
% of n %
% returns n with the digits reversed %
Line 154:
end for_n ;
write( i_w := 1, s_w := 0, "Found ", rdCount, " ""special divisors"" below 200" )
end.</
{{out}}
Same as the Algol 68 sample.
Line 160:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{out}}
<pre>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
Line 168:
=={{header|AppleScript}}==
<
set output to {}
Line 217:
if (hasSpecialDivisors(n, base)) then set end of output to n
end repeat
return {|count|:(count output), finds:output}</
{{output}}
<
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">reversed: function [x]->
to :integer join to [:string] reverse digits x
specialDivisors: select 1..200 'n ->
every? factors n 'd ->
zero? (reversed n) % reversed d
loop split.every: 9 specialDivisors 'x ->
print map x 's -> pad to :string s 4</syntaxhighlight>
{{out}}
<pre> 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|BASIC}}==
<
20 FOR I=1 TO 199
30 J=I: X=0
Line 234 ⟶ 257:
100 NEXT J
110 PRINT I,
120 NEXT I</
{{out}}
<pre> 1 2 3 4 5
Line 251 ⟶ 274:
179 181 187 191 193
197 199</pre>
=={{header|BASIC256}}==
<syntaxhighlight lang="freebasic">c = 0
for n = 1 to 200
u = reverse(n)
s = true
for d = 1 to n
if n mod d = 0 then
b = reverse(d)
if u mod b <> 0 then s = false
end if
next d
if s then c += 1 : print n; chr(9);
next n
print
print "Found "; c; " special divisors."
end
function reverse(n)
u = 0
while n
u = u * 10 + n mod 10
n = n \ 10
end while
return u
end function</syntaxhighlight>
{{out}}
<pre>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.</pre>
=={{header|BCPL}}==
<
let reverse(n) = valof
Line 284 ⟶ 337:
$)
wrch('*N')
$)</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 297 ⟶ 350:
=={{header|C}}==
{{trans|Delphi}}
<
#include <stdio.h>
Line 346 ⟶ 399:
return 0;
}</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 360 ⟶ 413:
=={{header|C++}}==
<
#include <iomanip>
#include <vector>
Line 398 ⟶ 451:
std::cout << std::endl;
return 0;
}</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 411 ⟶ 464:
=={{header|C#}}==
{{trans|C}}
<
namespace SpecialDivisors {
Line 461 ⟶ 514:
}
}
}</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 475 ⟶ 528:
=={{header|CLU}}==
<
r: int := 0
while n>0 do
Line 507 ⟶ 560:
end
end
end start_up</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 519 ⟶ 572:
=={{header|COBOL}}==
<
PROGRAM-ID. SPECIAL-DIVISORS.
Line 576 ⟶ 629:
MOVE REV-DIGITS(RD) TO TEMP.
MOVE REV-DIGITS(3) TO REV-DIGITS(RD).
MOVE TEMP TO REV-DIGITS(3).</
{{out}}
<pre style='height:50ex;'> 1
Line 652 ⟶ 705:
=={{header|Cowgol}}==
<
const MAXIMUM := 200;
Line 685 ⟶ 738:
end if;
n := n + 1;
end loop;</
{{out}}
<pre style='height:50ex;'>1
Line 760 ⟶ 813:
{{libheader| System.StrUtils}}
{{Trans|Ring}}
<
{$IFDEF FPC}
{$MODE DELPHI}
Line 824 ⟶ 877:
Main;
{$IFNDEF UNIX} readln; {$ENDIF}
end.</
{{out}}
<pre>Working...
Line 842 ⟶ 895:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.primes.factors math.ranges prettyprint sequences ;
Line 851 ⟶ 904:
[ reverse-number divisor? ] with all? ;
200 [1..b] [ special? ] filter 18 group simple-table.</
{{out}}
<pre>
Line 862 ⟶ 915:
=={{header|Forth}}==
{{works with|Gforth}}
<
0 >r
begin
Line 906 ⟶ 959:
main
bye</
{{out}}
Line 922 ⟶ 975:
=={{header|FreeBASIC}}==
<
dim as integer u = 0
while n
Line 944 ⟶ 997:
next d
if s then print using "### ";n;
next n</
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 989 ⟶ 1,042:
}
fmt.Printf("\n%d special divisors found.\n", len(special))
}</
{{out}}
Line 1,005 ⟶ 1,058:
=={{header|J}}==
<
{{out}}
<pre>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>
Line 1,012 ⟶ 1,065:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
# divisors as an unsorted stream
def divisors:
Line 1,033 ⟶ 1,086:
| all(divisors; $nreverse % reverse_number == 0);
range(1;200) | select(is_special_divisor)</
{{out}}
A stream of numbers as shown elsewhere on this page.
=={{header|Julia}}==
<
function divisors(n)
Line 1,060 ⟶ 1,113:
const specials = filter(isspecialdivisor, 1:200)
foreach(p -> print(rpad(p[2], 4), p[1] % 18 == 0 ? "\n" : ""), enumerate(specials))
</
<pre>
1 2 3 4 5 6 7 8 9 11 13 17 19 22 23 26 27 29
Line 1,069 ⟶ 1,122:
=={{header|MAD}}==
<
INTERNAL FUNCTION(X)
Line 1,090 ⟶ 1,143:
CAND CONTINUE
VECTOR VALUES FMT = $I4*$
END OF PROGRAM</
{{out}}
<pre style='height:50ex;'> 1
Line 1,166 ⟶ 1,219:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Select[Range[199], SpecialDivisorQ]
Length[%]</
{{out}}
<pre>{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}
Line 1,174 ⟶ 1,227:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 1,215 ⟶ 1,268:
END;
WriteLn();
END SpecialDivisors.</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 1,227 ⟶ 1,280:
=={{header|Nim}}==
<
func reversed(n: Positive): int =
Line 1,253 ⟶ 1,306:
break check
inc count
stdout.write ($n).align(3), if count mod 12 == 0: '\n' else: ' '</
{{out}}
Line 1,267 ⟶ 1,320:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,279 ⟶ 1,332:
say @sd . " matching numbers:\n" .
(sprintf "@{['%4d' x @sd]}", @sd) =~ s/(.{40})/$1\n/gr;</
{{out}}
<pre>72 matching numbers:
Line 1,292 ⟶ 1,345:
=={{header|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>
Line 1,315 ⟶ 1,368:
<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>
<!--</
{{out}}
<pre>
Line 1,326 ⟶ 1,379:
=={{header|PILOT}}==
<
:n=1
*num
Line 1,354 ⟶ 1,407:
:a=b
J (a>0):*revloop
E :</
{{out}}
<pre style='height:50ex;'>1
Line 1,431 ⟶ 1,484:
=={{header|PL/I}}==
<
%replace MAX by 200;
Line 1,462 ⟶ 1,515:
end;
end;
end specialDivisors;</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 1,477 ⟶ 1,530:
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<
DECLARE TRUE LITERALLY '0FFH';
Line 1,546 ⟶ 1,599:
CALL PRINT$NUMBER( MAX$SD + 1 );
CALL PRINT$NL;
EOF</
{{out}}
<pre>
Line 1,567 ⟶ 1,620:
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page.
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
<
special_divisors_100H: procedure options (main);
Line 1,651 ⟶ 1,704:
CALL PRNL;
EOF: end special_divisors_100H;</
{{out}}
<pre>
Line 1,664 ⟶ 1,717:
FOUND 72 ''SPECIAL DIVISORS'' BELOW 200
</pre>
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure reverse(n.i)
u.i = 0
While n
u = u * 10 + (n % 10)
n = Int(n / 10)
Wend
ProcedureReturn u
EndProcedure
OpenConsole()
c.i = 0
For n.i = 1 To 200
u.i = reverse(n)
s.b = #True
For d.i = 1 To n
If n % d = 0
b = reverse(d)
If u % b <> 0
s = #False
EndIf
EndIf
Next d
If s
Print(Str(n) + #TAB$)
c + 1
EndIf
Next n
PrintN(#CRLF$ + "Found " + Str(c) + " special divisors.")
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>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.</pre>
=={{header|Python}}==
<syntaxhighlight lang="python">#!/usr/bin/python
def reverse(n):
u = 0
while n:
u = 10 * u + n % 10
n = int(n / 10)
return u
c = 0
for n in range(1, 200):
u = reverse(n)
s = True
for d in range (1, n):
if n % d == 0:
b = reverse(d)
if u % b != 0:
s = False
if s:
c = c + 1
print(n, end='\t')
print("\nEncontrados ", c, "divisores especiales.")</syntaxhighlight>
{{out}}
<pre>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
Encontrados 72 divisores especiales.</pre>
=={{header|Quackery}}==
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
<syntaxhighlight lang="Quackery"> [ 0
[ swap 10 /mod
rot 10 * +
over 0 = until ]
nip ] is revnum ( n --> n )
[]
[ 200 times
[ true
i^ revnum
i^ factors
witheach
[ revnum
dip dup mod
0 != if
[ dip not
conclude ] ]
drop
if [ i^ join ] ]
behead drop ]
[]
swap witheach
[ number$ nested join ]
48 wrap$</syntaxhighlight>
{{out}}
<pre>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}}==
<syntaxhighlight lang="raku"
say "{+$_} matching numbers:\n{.batch(10)».fmt('%3d').join: "\n"}"
given (1..^200).grep: { all .flip «%%« .&divisors».flip };</
{{out}}
<pre>72 matching numbers:
Line 1,682 ⟶ 1,842:
=={{header|REXX}}==
<
/*────────────────────────── reverse(N) for all divisors D of N, where N < 200. */
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 1,709 ⟶ 1,869:
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' found title</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,728 ⟶ 1,888:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,772 ⟶ 1,932:
next
return rev
</syntaxhighlight>
{{out}}
<pre>
Line 1,787 ⟶ 1,947:
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ →STR ""
OVER SIZE 1 '''FOR''' j
OVER j DUP SUB +
-1 '''STEP'''
STR→ NIP
≫ '<span style="color:blue">REVNUM</span>' STO
≪ {1}
2 200 FOR n
1 SF
n <span style="color:blue">REVNUM</span> n DIVIS
2 OVER SIZE 1 - '''FOR''' d
'''IF''' DUP2 d GET <span style="color:blue">REVNUM</span> MOD '''THEN'''
1 CF DUP SIZE 'd' STO '''END'''
'''NEXT''' DROP2
'''IF''' 1 FS? '''THEN''' n + '''END'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {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>
Runs in 62 seconds on a HP-50g.
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">class Integer
def reverse
to_s.reverse.to_i
end
def divisors
res = []
(1..Integer.sqrt(self)).each do |cand|
div, mod = self.divmod(cand)
res << cand << div if mod == 0
end
res.uniq.sort
end
def special_divisors?
r = self.reverse
divisors.all?{|d| r % d.reverse == 0}
end
end
p (1..200).select(&:special_divisors?)</syntaxhighlight>
{{out}}
<pre>
[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|Rust}}==
<syntaxhighlight lang="rust">fn condition( num : u16 ) -> bool {
let divis : Vec<u16> = divisors( num ) ;
let reversed : u16 = my_reverse( num ) ;
divis.iter( ).all( | d | {
let revi = my_reverse( *d ) ;
reversed % revi == 0 } )
}
fn my_reverse( num : u16 ) -> u16 {
let numstring : String = num.to_string( ) ;
let nstr : &str = numstring.as_str( ) ;
let mut reversed_str : String = String::new( ) ;
for c in nstr.chars( ).rev( ) {
reversed_str.push( c ) ;
}
let reversi : &str = reversed_str.as_str( ) ;
reversi.parse::<u16>( ).unwrap( )
}
fn divisors( n : u16 ) -> Vec<u16> {
(1..=n).filter( | &d | n % d == 0 ).collect( )
}
fn main() {
println!("{:?}" , (1u16..200u16).filter( | &d | condition( d ) ).collect
::<Vec<u16>>( ) ) ;
}</syntaxhighlight>
{{out}}
<pre>
[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|Sidef}}==
<
{{out}}
<pre>
Line 1,796 ⟶ 2,041:
=={{header|Swift}}==
<
func reverse(_ number: Int) -> Int {
Line 1,834 ⟶ 2,079:
}
}
print("\n\(count) numbers found.")</
{{out}}
Line 1,851 ⟶ 2,096:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./
var reversed = Fn.new { |n|
Line 1,873 ⟶ 2,116:
}
System.print("Special divisors in the range 0..199:")
Fmt.tprint("$3d", special, 12)
System.print("\n%(special.count) special divisors found.")</
{{out}}
Line 1,890 ⟶ 2,133:
=={{header|XPL0}}==
<
int N, M;
[M:= 0;
Line 1,923 ⟶ 2,166:
IntOut(0, Count);
Text(0, " such numbers found.");
]</
{{out}}
Line 1,939 ⟶ 2,182:
=={{header|Yabasic}}==
{{trans|BASIC}}
<
// by Galileo, 04/2022
Line 1,952 ⟶ 2,195:
100 NEXT J
110 PRINT I,
120 NEXT I</
{{out}}
<pre>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 ---Program done, press RETURN---</pre>
=={{header|Zig}}==
<
const N = u16; // smallest integer type that fits
Line 1,990 ⟶ 2,233:
}
try stdout.print("\n", .{});
}</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
|