Smith numbers: Difference between revisions

m
→‎{{header|BASIC}}: fix syntax highlighting
m (Fix Perl 6 -> Raku links)
m (→‎{{header|BASIC}}: fix syntax highlighting)
 
(46 intermediate revisions by 21 users not shown)
Line 26:
* from The Prime pages:   [[http://primes.utm.edu/glossary/xpage/SmithNumber.html Smith numbers]].
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F factors(=n)
[Int] rt
V f = 2
I n == 1
rt.append(1)
E
L
I 0 == (n % f)
rt.append(f)
n I/= f
I n == 1
R rt
E
f++
R rt
 
F sum_digits(=n)
V sum = 0
L n > 0
V m = n % 10
sum += m
n -= m
n I/= 10
R sum
 
F add_all_digits(lst)
V sum = 0
L(i) 0 .< lst.len
sum += sum_digits(lst[i])
R sum
 
F list_smith_numbers(cnt)
[Int] r
L(i) 4 .< cnt
V fac = factors(i)
I fac.len > 1
I sum_digits(i) == add_all_digits(fac)
r.append(i)
R r
 
V sn = list_smith_numbers(10'000)
print(‘Count of Smith Numbers below 10k: ’sn.len)
print()
print(‘First 15 Smith Numbers:’)
print_elements(sn[0.<15])
print()
print(‘Last 12 Smith Numbers below 10000:’)
print_elements(sn[(len)-12..])</syntaxhighlight>
 
{{out}}
<pre>
Count of Smith Numbers below 10k: 376
 
First 15 Smith Numbers:
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378
 
Last 12 Smith Numbers below 10000:
9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
</pre>
 
=={{header|360 Assembly}}==
{{trans|Rexx}}
<langsyntaxhighlight lang="360asm">* Smith numbers - 02/05/2017
SMITHNUM CSECT
USING SMITHNUM,R13 base register
Line 175 ⟶ 238:
XDEC DS CL12 temp
YREGS
END SMITHNUM</langsyntaxhighlight>
{{out}}
<pre>
Line 203 ⟶ 266:
9861 9880 9895 9924 9942 9968 9975 9985
376 smith numbers found <= 10000
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN factors n:
PUT {} IN factors
PUT 2 IN factor
WHILE n >= factor:
SELECT:
n mod factor = 0:
INSERT factor IN factors
PUT n/factor IN n
ELSE:
PUT factor+1 IN factor
RETURN factors
 
HOW TO RETURN digit.sum n:
PUT 0 IN sum
WHILE n > 0:
PUT sum + (n mod 10) IN sum
PUT floor (n/10) IN n
RETURN sum
 
HOW TO REPORT smith.number n:
PUT factors n IN facs
IF #facs = 1: FAIL
PUT 0 IN fac.dsum
FOR fac IN facs:
PUT fac.dsum + digit.sum fac IN fac.dsum
REPORT fac.dsum = digit.sum n
 
PUT 0 IN col
FOR i IN {1..9999}:
IF smith.number i:
WRITE (i>>5)
PUT col+1 IN col
IF col=16:
WRITE /
PUT 0 IN col
WRITE /</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985</pre>
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<syntaxhighlight lang="action!">CARD FUNC SumDigits(CARD n)
CARD res,a
 
res=0
WHILE n#0
DO
res==+n MOD 10
n==/10
OD
RETURN (res)
 
CARD FUNC PrimeFactors(CARD n CARD ARRAY f)
CARD a,count
 
a=2 count=0
DO
IF n MOD a=0 THEN
f(count)=a
count==+1
n==/a
IF n=1 THEN
RETURN (count)
FI
ELSE
a==+1
FI
OD
RETURN (0)
 
PROC Main()
CARD n,i,s1,s2,count,tmp
CARD ARRAY f(100)
 
FOR n=4 TO 10000
DO
count=PrimeFactors(n,f)
IF count>=2 THEN
s1=SumDigits(n)
s2=0
FOR i=0 TO count-1
DO
tmp=f(i)
s2==+SumDigits(tmp)
OD
IF s1=s2 THEN
PrintC(n) Put(32)
FI
FI
Poke(77,0) ;turn off the attract mode
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smith_numbers.png Screenshot from Atari 8-bit computer]
<pre>
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 517 526 535 562 576 588 627 634 636 645
648 654 663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958 985 1086 1111 1165 1219 1255 1282 1284 1376
1449 1507 1581 1626 1633 1642 1678 1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409 2434 2461 2475 2484 2515 2556 2576
2578 2583 2605 2614 2679 2688 2722 2745 2751 2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091
3138 3168 3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663 3690 3694 3802 3852 3864
3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191 4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594
4702 4743 4765 4788 4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242 5248 5253 5269
5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642 5674 5772 5818 5854 5874 5915 5926 5935 5936 5946
5998 6036 6054 6084 6096 6115 6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583 6585
6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062 7068 7078 7089 7119 7136 7186 7195 7227
7249 7287 7339 7402 7438 7447 7465 7503 7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952
7978 8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347 8372 8412 8421 8466 8518 8545
8568 8628 8653 8680 8736 8754 8766 8790 8792 8851 8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229
9274 9276 9285 9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571 9598 9633 9634 9639
9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
</pre>
 
=={{header|Ada}}==
{{works with|Ada|2012}}
<langsyntaxhighlight lang="ada">
with Ada.Text_IO;
 
Line 235 ⟶ 434:
end loop;
end smith;
</syntaxhighlight>
</lang>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># sieve of Eratosthene: sets s[i] to TRUE if i is prime, FALSE otherwise #
PROC sieve = ( REF[]BOOL s )VOID:
BEGIN
Line 310 ⟶ 509:
OD;
print( ( newline, "THere are ", whole( smith count, -7 ), " Smith numbers below ", whole( max number, -7 ), newline ) )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 320 ⟶ 519:
THere are 376 Smith numbers below 10000
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
 
#proto muestranúmeroencontrado(_X_)
 
algoritmo
 
resultado={}, primos="", suma1=0, suma2=0 , i=1
temp_primos=0,
fijar separador 'NULO'
decimales '0'
iterar para ( num=4, #(num<=10000), ++num )
ir por el siguiente si ' es primo(num) '
obtener divisores de (num);
luego obtener los primos de esto para 'primos'
 
sumar los dígitos de 'num'; guardar en 'suma2'
/* análisis p-ádico */
guardar 'primos' en 'temp_primos'
iterar para(q=1, #( q<=length(temp_primos) ) , ++q )
iterar para( r=2, #( (num % (temp_primos[q]^r)) == 0 ), ++r )
#(temp_primos[q]); meter en 'primos'
siguiente
siguiente
sumar dígitos de cada número de 'primos'
guardar en 'suma1'
 
'suma1' respecto a 'suma2' son iguales?
entonces{
_muestra número encontrado 'num'
}
siguiente
terminar
 
subrutinas
 
muestra número encontrado (x)
imprimir ( #(lpad(" ",4,string(x))), solo si ( #(i<8), " " ) )
++i
cuando ( #(i>8) ){
saltar, guardar '1' en 'i'
}
retornar
 
</syntaxhighlight>
{{out}}
<pre>
4 22 27 58 85 94 121 166
202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562
576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778
825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284
1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872
1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218
2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578
2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958
2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390
3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946
3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464
4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960
4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397
5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935
5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315
6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816
6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227
7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764
7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154
8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628
8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036
9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386
9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708
9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
 
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">digitSum: function [v][
n: new v
result: new 0
while [n > 0][
'result + n % 10
'n / 10
]
return result
]
 
smith?: function [z][
return
(prime? z) ? -> false
-> (digitSum z) = sum map factors.prime z 'num [digitSum num]
]
found: 0
loop 1..10000 'x [
if smith? x [
found: found + 1
prints (pad to :string x 6) ++ " "
if 0 = found % 10 -> print ""
]
]
print ""</syntaxhighlight>
 
{{out}}
 
<pre> 4 22 27 58 85 94 121 166 202 265
274 319 346 355 378 382 391 438 454 483
517 526 535 562 576 588 627 634 636 645
648 654 663 666 690 706 728 729 762 778
825 852 861 895 913 915 922 958 985 1086
1111 1165 1219 1255 1282 1284 1376 1449 1507 1581
1626 1633 1642 1678 1736 1755 1776 1795 1822 1842
1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265
2286 2326 2362 2366 2373 2409 2434 2461 2475 2484
2515 2556 2576 2578 2583 2605 2614 2679 2688 2722
2745 2751 2785 2839 2888 2902 2911 2934 2944 2958
2964 2965 2970 2974 3046 3091 3138 3168 3174 3226
3246 3258 3294 3345 3366 3390 3442 3505 3564 3595
3615 3622 3649 3663 3690 3694 3802 3852 3864 3865
3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557
4592 4594 4702 4743 4765 4788 4794 4832 4855 4880
4918 4954 4959 4960 4974 4981 5062 5071 5088 5098
5172 5242 5248 5253 5269 5298 5305 5386 5388 5397
5422 5458 5485 5526 5539 5602 5638 5642 5674 5772
5818 5854 5874 5915 5926 5935 5936 5946 5998 6036
6054 6084 6096 6115 6171 6178 6187 6188 6252 6259
6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855
6880 6934 6981 7026 7051 7062 7068 7078 7089 7119
7136 7186 7195 7227 7249 7287 7339 7402 7438 7447
7465 7503 7627 7674 7683 7695 7712 7726 7762 7764
7782 7784 7809 7824 7834 7915 7952 7978 8005 8014
8023 8073 8077 8095 8149 8154 8158 8185 8196 8253
8257 8277 8307 8347 8372 8412 8421 8466 8518 8545
8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166
9184 9193 9229 9274 9276 9285 9294 9296 9301 9330
9346 9355 9382 9386 9387 9396 9414 9427 9483 9522
9535 9571 9598 9633 9634 9639 9648 9657 9684 9708
9717 9735 9742 9760 9778 9840 9843 9849 9861 9880
9895 9924 9942 9968 9975 9985</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SMITH_NUMBERS.AWK
# converted from C
Line 400 ⟶ 769:
return(sum)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 429 ⟶ 798:
9861 9880 9895 9924 9942 9968 9975 9985
</pre>
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 DIM F(32)
30 FOR I=2 TO 9999
40 F=0: N=I
50 IF N>0 AND (N AND 1)=0 THEN N=N\2: F(F)=2: F=F+1: GOTO 50
60 P=3
70 GOTO 100
80 IF N MOD P=0 THEN N=N\P: F(F)=P: F=F+1: GOTO 80
90 P=P+2
100 IF P<=N GOTO 80
110 IF F<=1 GOTO 190
120 N=I: S=0
130 IF N>0 THEN S=S+N MOD 10: N=N\10: GOTO 130
140 FOR J=0 TO F-1
150 N=F(J)
160 IF N>0 THEN S=S-N MOD 10: N=N\10: GOTO 160
170 NEXT
180 IF S=0 THEN PRINT USING " ####";I;: C=C+1
190 NEXT
200 PRINT
210 PRINT "Found";C;"Smith numbers."</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
Found 376 Smith numbers.</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
// Find the sum of the digits of N
let digsum(n) =
n<10 -> n,
n rem 10 + digsum(n/10)
 
// Factorize N
let factors(n, facs) = valof
$( let count = 0 and fac = 3
// Powers of 2
while n>0 & (n & 1)=0
$( n := n >> 1
facs!count := 2
count := count + 1
$)
// Odd factors
while fac <= n
$( while n rem fac=0
$( n := n / fac
facs!count := fac
count := count + 1
$)
fac := fac + 2
$)
resultis count
$)
 
// Is N a Smith number?
let smith(n) = valof
$( let facs = vec 32
let nfacs = factors(n, facs)
let facsum = 0
if nfacs<=1 resultis false // primes are not Smith numbers
for fac = 0 to nfacs-1 do
facsum := facsum + digsum(facs!fac)
resultis digsum(n) = facsum
$)
 
// Count and print Smith numbers below 10,000
let start() be
$( let count = 0
for i = 2 to 9999 if smith(i)
$( writed(i, 5)
count := count + 1
if count rem 16 = 0 then wrch('*N')
$)
writef("*NFound %N Smith numbers.*N", count)
$)</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
Found 376 Smith numbers.</pre>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">
<lang c>
#include <stdlib.h>
#include <stdio.h>
Line 511 ⟶ 1,009:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 543 ⟶ 1,041:
=={{header|C sharp|C#}}==
{{trans|java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 599 ⟶ 1,097:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 166 202
Line 650 ⟶ 1,148:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <vector>
Line 696 ⟶ 1,194:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 707 ⟶ 1,205:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn divisible? [a b]
(zero? (mod a b)))
 
Line 732 ⟶ 1,230:
(sum-digits (clojure.string/join "" (prime-factors n))))))
 
(filter smith-number? (range 1 10000))</langsyntaxhighlight>
 
{{out}}
Line 740 ⟶ 1,238:
9760 9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985)
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Get all digits of a number
digits = iter (n: int) yields (int)
while n > 0 do
yield(n // 10)
n := n / 10
end
end digits
 
% Get all prime factors of a number
prime_factors = iter (n: int) yields (int)
% Take factors of 2 out first (the compiler should optimize)
while n // 2 = 0 do yield(2) n := n/2 end
 
% Next try odd factors
fac: int := 3
while fac <= n do
while n // fac = 0 do
yield(fac)
n := n/fac
end
fac := fac + 2
end
end prime_factors
 
% See if a number is a Smith number
smith = proc (n: int) returns (bool)
dsum: int := 0
fac_dsum: int := 0
 
% Find the sum of the digits
for d: int in digits(n) do dsum := dsum + d end
 
% Find the sum of the digits of all factors
nfac: int := 0
for fac: int in prime_factors(n) do
nfac := nfac + 1
for d: int in digits(fac) do fac_dsum := fac_dsum + d end
end
 
% The number is a Smith number if these two are equal,
% and the number is not prime (has more than one factor)
return(fac_dsum = dsum cand nfac > 1)
end smith
 
% Yield all Smith numbers up to a limit
smiths = iter (max: int) yields (int)
for i: int in int$from_to(1, max-1) do
if smith(i) then yield(i) end
end
end smiths
 
% Display all Smith numbers below 10,000
start_up = proc ()
po: stream := stream$primary_output()
count: int := 0
for s: int in smiths(10000) do
stream$putright(po, int$unparse(s), 5)
count := count + 1
if count // 16 = 0 then stream$putl(po, "") end
end
stream$putl(po, "\nFound " || int$unparse(count) || " Smith numbers.")
end start_up</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
Found 376 Smith numbers.</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
typedef N is uint16; # 16-bit math is good enough
 
# Print a value right-justified in a field of length N
sub print_right(n: N, width: uint8) is
var arr: uint8[16];
var buf := &arr[0];
var nxt := UIToA(n as uint32, 10, buf);
var len := (nxt - buf) as uint8;
while len < width loop
print_char(' ');
len := len + 1;
end loop;
print(buf);
end sub;
 
# Find the sum of the digits of a number
sub digit_sum(n: N): (sum: N) is
sum := 0;
while n > 0 loop
sum := sum + n % 10;
n := n / 10;
end loop;
end sub;
 
# Factorize a number, write the factors into the buffer,
# return the amount of factors.
sub factorize(n: N, buf: [N]): (count: N) is
count := 0;
# Take care of the factors of 2 first
while n>0 and n & 1 == 0 loop
n := n >> 1;
count := count + 1;
[buf] := 2;
buf := @next buf;
end loop;
# Then do the odd factors
var fac: N := 3;
while n >= fac loop
while n % fac == 0 loop
n := n / fac;
count := count + 1;
[buf] := fac;
buf := @next buf;
end loop;
fac := fac + 2;
end loop;
end sub;
 
# See if a number is a Smith number
sub smith(n: N): (rslt: uint8) is
rslt := 0;
var facs: N[16];
var n_facs := factorize(n, &facs[0]) as @indexof facs;
if n_facs > 1 then
# Only composite numbers are Smith numbers
var dsum := digit_sum(n);
var facsum: N := 0;
var i: @indexof facs := 0;
while i < n_facs loop
facsum := facsum + digit_sum(facs[i]);
i := i + 1;
end loop;
if facsum == dsum then rslt := 1; end if;
end if;
end sub;
 
# Display all Smith numbers below 10000
var i: N := 2;
var count: N := 0;
while i < 10000 loop
if smith(i) != 0 then
count := count + 1;
print_right(i, 5);
if count & 0xF == 0 then print_nl(); end if;
end if;
i := i + 1;
end loop;
print_nl();
print("Found ");
print_i32(count as uint32);
print(" Smith numbers.\n");</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
Found 376 Smith numbers.</pre>
 
=={{header|D}}==
{{trans|Java}} mostly
<langsyntaxhighlight Dlang="d">import std.stdio;
 
void main() {
Line 795 ⟶ 1,493:
}
return sum;
}</langsyntaxhighlight>
 
{{out}}
Line 836 ⟶ 1,534:
9717 9735 9742 9760 9778 9840 9843 9849 9861 9880
9895 9924 9942 9968 9975 9985</pre>
 
=={{header|Delphi}}==
See [[Smith numbers#Pascal|Pascal]].
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* Find the sum of the digits of a number */
proc nonrec digitsum(word n) word:
word sum;
sum := 0;
while n ~= 0 do
sum := sum + n % 10;
n := n / 10
od;
sum
corp
 
/* Find all prime factors and write them into the given array
(which is assumed to be big enough); return the amount of
factors. */
proc nonrec factors(word n; [*] word facs) word:
word count, fac;
count := 0;
 
/* take out factors of 2 */
while n > 0 and n & 1 = 0 do
n := n >> 1;
facs[count] := 2;
count := count + 1
od;
 
/* take out odd factors */
fac := 3;
while n >= fac do
while n % fac = 0 do
n := n / fac;
facs[count] := fac;
count := count + 1;
od;
fac := fac + 2
od;
count
corp
 
/* See if a number is a Smith number */
proc nonrec smith(word n) bool:
[32] word facs; /* 32 factors ought to be enough for everyone */
word dsum, facsum, nfacs, i;
nfacs := factors(n, facs);
if nfacs = 1 then
false /* primes are not Smith numbers */
else
dsum := digitsum(n);
facsum := 0;
for i from 0 upto nfacs-1 do
facsum := facsum + digitsum(facs[i])
od;
dsum = facsum
fi
corp
 
/* Find all Smith numbers below 10000 */
proc nonrec main() void:
word i, count;
count := 0;
for i from 2 upto 9999 do
if smith(i) then
write(i:5);
count := count + 1;
if count & 0xF = 0 then writeln() fi
fi
od;
writeln();
writeln("Found ", count, " Smith numbers.")
corp</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
Found 376 Smith numbers.</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc prim_fact x . pf[] .
pf[] = [ ]
p = 2
repeat
if x mod p = 0
pf[] &= p
x = x div p
else
p += 1
.
until x = 1
.
.
func digsum x .
while x > 0
sum += x mod 10
x = x div 10
.
return sum
.
for i = 2 to 9999
prim_fact i pf[]
if len pf[] >= 2
sum = 0
for e in pf[]
sum += digsum e
.
if digsum i = sum
write i & " "
.
.
.
</syntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Smith do
def number?(n) do
d = decomposition(n)
Line 858 ⟶ 1,693:
IO.puts "#{length(smith)} smith numbers below #{m}:"
IO.puts "First 10: #{Enum.take(smith,10) |> Enum.join(", ")}"
IO.puts "Last 10: #{Enum.take(smith,-10) |> Enum.join(", ")}"</langsyntaxhighlight>
 
{{out}}
Line 867 ⟶ 1,702:
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
// Generate Smith Numbers. Nigel Galloway: November 6th., 2020
let fN g=Seq.unfold(fun n->match n with 0->None |_->Some(n%10,n/10)) g |> Seq.sum
let rec fG(n,g) p=match g%p with 0->fG (n+fN p,g/p) p |_->(n,g)
primes32()|>Seq.pairwise|>Seq.collect(fun(n,g)->[n+1..g-1])|>Seq.takeWhile(fun n->n<10000)
|>Seq.filter(fun g->fN g=fst(primes32()|>Seq.scan(fun n g->fG n g)(0,g)|>Seq.find(fun(_,n)->n=1)))
|>Seq.chunkBySize 20|>Seq.iter(fun n->Seq.iter(printf "%4d ") n; printfn "")
</syntaxhighlight>
{{out}}
<pre>
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483
517 526 535 562 576 588 627 634 636 645 648 654 663 666 690 706 728 729 762 778
825 852 861 895 913 915 922 958 985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581
1626 1633 1642 1678 1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409 2434 2461 2475 2484
2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751 2785 2839 2888 2902 2911 2934 2944 2958
2964 2965 2970 2974 3046 3091 3138 3168 3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595
3615 3622 3649 3663 3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788 4794 4832 4855 4880
4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242 5248 5253 5269 5298 5305 5386 5388 5397
5422 5458 5485 5526 5539 5602 5638 5642 5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036
6054 6084 6096 6115 6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062 7068 7078 7089 7119
7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503 7627 7674 7683 7695 7712 7726 7762 7764
7782 7784 7809 7824 7834 7915 7952 7978 8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253
8257 8277 8307 8347 8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285 9294 9296 9301 9330
9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571 9598 9633 9634 9639 9648 9657 9684 9708
9717 9735 9742 9760 9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
</pre>
=={{header|Factor}}==
<syntaxhighlight lang="text">USING: formatting grouping io kernel math.primes.factors
math.ranges math.text.utils sequences sequences.deep ;
 
Line 879 ⟶ 1,746:
 
10,000 [1,b] [ smith? ] filter 10 group
[ [ "%4d " printf ] each nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 921 ⟶ 1,788:
9895 9924 9942 9968 9975 9985
</pre>
 
=={{header|FOCAL}}==
<syntaxhighlight lang="focal">01.10 S C=0
01.20 T %4
01.30 F I=1,10000;D 4
01.40 T !
01.50 Q
 
02.10 S Z=N
02.20 S S=0
02.30 S Y=FITR(Z/10)
02.40 S S=S+(Z-Y*10)
02.50 S Z=Y
02.60 I (-Z)2.3
 
03.05 S V=0;S Z=N
03.10 S Y=FITR(Z/2)
03.15 I (Z-Y*2)3.3,3.2,3.3
03.20 S V=V+1;S V(V)=2
03.25 S Z=Y;G 3.1
03.30 S X=3
03.35 I (Z-X)3.65,3.4,3.4
03.40 S Y=FITR(Z/X)
03.45 I (Z-Y*X)3.6,3.5,3.6
03.50 S V=V+1;S V(V)=X
03.55 S Z=Y;G 3.35
03.60 S X=X+2;G 3.35
03.65 R
 
04.10 S N=I;D 3
04.20 I (V-1)4.3,4.9,4.3
04.30 D 2;S A=S
04.40 S B=0
04.50 F K=1,V;S N=V(K);D 2;S B=B+S
04.60 I (A-B)4.9,4.7,4.9
04.70 T I;S C=C+1;I (C-FITR(C/13)*13)4.9,4.8,4.9
04.80 T !
04.90 R</syntaxhighlight>
{{out}}
<pre>= 4= 22= 27= 58= 85= 94= 121= 166= 202= 265= 274= 319= 346
= 355= 378= 382= 391= 438= 454= 483= 517= 526= 535= 562= 576= 588
= 627= 634= 636= 645= 648= 654= 663= 666= 690= 706= 728= 729= 762
= 778= 825= 852= 861= 895= 913= 915= 922= 958= 985= 1086= 1111= 1165
= 1219= 1255= 1282= 1284= 1376= 1449= 1507= 1581= 1626= 1633= 1642= 1678= 1736
= 1755= 1776= 1795= 1822= 1842= 1858= 1872= 1881= 1894= 1903= 1908= 1921= 1935
= 1952= 1962= 1966= 2038= 2067= 2079= 2155= 2173= 2182= 2218= 2227= 2265= 2286
= 2326= 2362= 2366= 2373= 2409= 2434= 2461= 2475= 2484= 2515= 2556= 2576= 2578
= 2583= 2605= 2614= 2679= 2688= 2722= 2745= 2751= 2785= 2839= 2888= 2902= 2911
= 2934= 2944= 2958= 2964= 2965= 2970= 2974= 3046= 3091= 3138= 3168= 3174= 3226
= 3246= 3258= 3294= 3345= 3366= 3390= 3442= 3505= 3564= 3595= 3615= 3622= 3649
= 3663= 3690= 3694= 3802= 3852= 3864= 3865= 3930= 3946= 3973= 4054= 4126= 4162
= 4173= 4185= 4189= 4191= 4198= 4209= 4279= 4306= 4369= 4414= 4428= 4464= 4472
= 4557= 4592= 4594= 4702= 4743= 4765= 4788= 4794= 4832= 4855= 4880= 4918= 4954
= 4959= 4960= 4974= 4981= 5062= 5071= 5088= 5098= 5172= 5242= 5248= 5253= 5269
= 5298= 5305= 5386= 5388= 5397= 5422= 5458= 5485= 5526= 5539= 5602= 5638= 5642
= 5674= 5772= 5818= 5854= 5874= 5915= 5926= 5935= 5936= 5946= 5998= 6036= 6054
= 6084= 6096= 6115= 6171= 6178= 6187= 6188= 6252= 6259= 6295= 6315= 6344= 6385
= 6439= 6457= 6502= 6531= 6567= 6583= 6585= 6603= 6684= 6693= 6702= 6718= 6760
= 6816= 6835= 6855= 6880= 6934= 6981= 7026= 7051= 7062= 7068= 7078= 7089= 7119
= 7136= 7186= 7195= 7227= 7249= 7287= 7339= 7402= 7438= 7447= 7465= 7503= 7627
= 7674= 7683= 7695= 7712= 7726= 7762= 7764= 7782= 7784= 7809= 7824= 7834= 7915
= 7952= 7978= 8005= 8014= 8023= 8073= 8077= 8095= 8149= 8154= 8158= 8185= 8196
= 8253= 8257= 8277= 8307= 8347= 8372= 8412= 8421= 8466= 8518= 8545= 8568= 8628
= 8653= 8680= 8736= 8754= 8766= 8790= 8792= 8851= 8864= 8874= 8883= 8901= 8914
= 9015= 9031= 9036= 9094= 9166= 9184= 9193= 9229= 9274= 9276= 9285= 9294= 9296
= 9301= 9330= 9346= 9355= 9382= 9386= 9387= 9396= 9414= 9427= 9483= 9522= 9535
= 9571= 9598= 9633= 9634= 9639= 9648= 9657= 9684= 9708= 9717= 9735= 9742= 9760
= 9778= 9840= 9843= 9849= 9861= 9880= 9895= 9924= 9942= 9968= 9975= 9985</pre>
 
=={{header|Fortran}}==
Line 929 ⟶ 1,864:
The factorisation is represented in a data aggregate, which is returned by function FACTOR. This is a facility introduced with F90, and before that one would have to use a collection of ordinary arrays to identify the list of primes and powers of a factorisation because functions could only return simple variables. Also, earlier compilers did not allow the use of the function's name as a variable within the function, or might allow this but produce incorrect results. However, modern facilities are not always entirely beneficial. Here, the function returns a full set of data for type FACTORED, even though often only the first few elements of the arrays will be needed and the rest could be ignored. It is possible to declare the arrays of type FACTORED to be "allocatable" with their size being determined at run time for each invocation of function FACTOR, at the cost of a lot of additional syntax and statements, plus the common annoyance of not knowing "how big" until ''after'' the list has been produced. Alas, such arrangements incur a performance penalty with ''every'' reference to the allocatable entities. See for example [[Sequence_of_primorial_primes#Run-time_allocation]]
 
For layout purposes, the numbers found were stashed in a line buffer rather than attempt to mess with the latter-day facilities of "non-advancing" output. This should be paramaterised for documentation purposes with say <code>MBUF = 20</code> rather than just using the magic constant of 20, however getting that into the FORMAT statement would require <code>FORMAT(<MBUF>I6)</code> and this <n> facility may not be recognised. Alternatively, one could put <code>FORMAT(666I6)</code> and hope that MBUF would never exceed 666. <langsyntaxhighlight Fortranlang="fortran"> MODULE FACTORISE !Produce a little list...
USE PRIMEBAG !This is a common need.
INTEGER LASTP !Some size allowances.
Line 1,053 ⟶ 1,988:
13 FORMAT (I9," found.") !Just in case.
END DO !On to the next base.
END !That was strange.</langsyntaxhighlight>
 
Output: selecting the base ten result:
Line 1,101 ⟶ 2,036:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub getPrimeFactors(factors() As UInteger, n As UInteger)
Line 1,155 ⟶ 2,090:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,188 ⟶ 2,123:
376 numbers found
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Smith_numbers}}
 
'''Solution'''
 
[[File:Fōrmulæ - Smith numbers 01.png]]
 
'''Test case. Write a program to find all Smith numbers below 10,000'''
 
[[File:Fōrmulæ - Smith numbers 02.png]]
 
[[File:Fōrmulæ - Smith numbers 03.png]]
 
=={{header|Go}}==
{{trans|C}}
<syntaxhighlight lang="go">
<lang Go>
package main
 
Line 1,275 ⟶ 2,224:
fmt.Println()
}
</syntaxhighlight>
</lang>
{{out}}<pre>
All the Smith Numbers less than 10000 are:
Line 1,282 ⟶ 2,231:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes (primeFactors)
import Data.List (unfoldr)
import Data.Tuple (swap)
Line 1,315 ⟶ 2,264:
, "\nLast 12 Smith Numbers below 10k:"
, unwords (show <$> drop (lowSmithCount - 12) lowSmiths)
]</langsyntaxhighlight>
{{Out}}
<pre>Count of Smith Numbers below 10k:
Line 1,328 ⟶ 2,277:
=={{header|J}}==
Implementation:
<langsyntaxhighlight Jlang="j">digits=: 10&#.inv
sumdig=: +/@,@digits
notprime=: -.@(1&p:)
smith=: #~ notprime * (=&sumdig q:)every</langsyntaxhighlight>
 
Task example:
<langsyntaxhighlight Jlang="j"> #smith }.i.10000
376
q:376
Line 1,385 ⟶ 2,334:
9598 9633 9634 9639 9648 9657 9684 9708
9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985</langsyntaxhighlight>
 
(first we count how many smith numbers are in our result, then we look at the prime factors of that count - turns out that 8 columns of 47 numbers each is perfect for this task.)
Line 1,391 ⟶ 2,340:
=={{header|Java}}==
{{works with|Java|7}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class SmithNumbers {
Line 1,435 ⟶ 2,384:
return sum;
}
}</langsyntaxhighlight>
<pre>4
22
Line 1,454 ⟶ 2,403:
{{Trans|Haskell}}
{{Trans|Python}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,623 ⟶ 2,572:
 
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Count of Smith Numbers below 10k:
Line 1,633 ⟶ 2,582:
Last 12 Smith Numbers below 10000:
9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
''' Preliminaries'''
<syntaxhighlight lang="jq">def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else {i:23}
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2)
| .i * .i > $n
end;
 
def sum(s): reduce s as $x (null; . + $x);
 
# emit a stream of the prime factors as per prime factorization
def prime_factors:
. as $num
| def m($p): # emit $p with appropriate multiplicity
$num | while( . % $p == 0; . / $p )
| $p ;
if (. % 2) == 0 then m(2) else empty end,
(range(3; 1 + (./2); 2)
| select(($num % .) == 0 and is_prime)
| m(.));
</syntaxhighlight>
'''The task'''
<syntaxhighlight lang="jq"># input should be an integer
def is_smith:
def sumdigits:
tostring|explode|map([.]|implode|tonumber)| add;
(is_prime|not) and
(sumdigits == sum(prime_factors|sumdigits));
 
"Smith numbers up to 10000:\n",
(range(1; 10000) | select(is_smith))
</syntaxhighlight>
{{out}}
<pre>
Smith numbers up to 10000:
 
4
22
27
58
...
9942
9968
9975
9985
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6
 
function sumdigits(n::Integer)
Line 1,650 ⟶ 2,659:
 
smithnumbers = collect(n for n in 2:10000 if issmith(n))
println("Smith numbers up to 10000:\n$smithnumbers")</langsyntaxhighlight>
 
{{out}}
Line 1,678 ⟶ 2,687:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun getPrimeFactors(n: Int): MutableList<Int> {
Line 1,727 ⟶ 2,736:
}
println("\n\n$count numbers found")
}</langsyntaxhighlight>
 
{{out}}
Line 1,761 ⟶ 2,770:
=={{header|Lua}}==
Slightly long-winded prime factor function but it's a bit faster than the 'easy' way.
<langsyntaxhighlight Lualang="lua">-- Returns a boolean indicating whether n is prime
function isPrime (n)
if n < 2 then return false end
Line 1,811 ⟶ 2,820:
for n = 1, 10000 do
if isSmith(n) then io.write(n .. "\t") end
end</langsyntaxhighlight>
Seems silly to paste in all 376 numbers but rest assured the output agrees with https://oeis.org/A006753
 
Line 1,825 ⟶ 2,834:
At the end we get a list (an inventory object with keys only). Print statement prints all keys (normally data, but if key isn't paired with data,then key is read only data)
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Set Fast !
Line 1,883 ⟶ 2,892:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|MAD}}==
 
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
PRINT COMMENT$ SMITH NUMBERS$
R GENERATE PRIMES UP TO 10,000 USING SIEVE METHOD
BOOLEAN SIEVE
DIMENSION SIEVE(10000)
DIMENSION PRIMES(1500)
THROUGH SET, FOR I=2, 1, I.G.10000
SET SIEVE(I) = 1B
 
THROUGH NXPRIM, FOR P=2, 1, P.G.100
WHENEVER SIEVE(P)
THROUGH MARK, FOR I=P*P, P, I.G.10000
MARK SIEVE(I) = 0B
NXPRIM END OF CONDITIONAL
 
NPRIMS = 0
THROUGH CNTPRM, FOR P=2, 1, P.G.10000
WHENEVER SIEVE(P)
PRIMES(NPRIMS) = P
NPRIMS = NPRIMS + 1
CNTPRM END OF CONDITIONAL
R CHECK SMITH NUMBERS
THROUGH SMITH, FOR I=4, 1, I.GE.10000
WHENEVER .NOT. SIEVE(I)
K = I
PFSUM = 0
THROUGH FACSUM, FOR P=0, 1, P.GE.NPRIMS .OR. K.E.0
L = PRIMES(P)
FACDIV WHENEVER K/L*L.E.K .AND. K.NE.0
PFSUM = PFSUM + DGTSUM.(L)
K = K/L
TRANSFER TO FACDIV
FACSUM END OF CONDITIONAL
WHENEVER PFSUM.E.DGTSUM.(I), PRINT FORMAT NUMFMT,I
SMITH END OF CONDITIONAL
VECTOR VALUES NUMFMT = $I5*$
R GET SUM OF DIGITS OF N
INTERNAL FUNCTION(N)
ENTRY TO DGTSUM.
DSUM = 0
DNUM = N
LOOP WHENEVER DNUM.E.0, FUNCTION RETURN DSUM
DSUM = DSUM + DNUM-DNUM/10*10
DNUM = DNUM/10
TRANSFER TO LOOP
END OF FUNCTION
END OF PROGRAM</syntaxhighlight>
 
{{out}}
<pre style='height: 40ex;'>SMITH NUMBERS
4
22
27
58
85
94
121
166
202
265
274
319
346
355
378
382
391
438
454
483
517
526
535
562
576
588
627
634
636
645
648
654
663
666
690
706
728
729
762
778
825
852
861
895
913
915
922
958
985
1086
1111
1165
1219
1255
1282
1284
1376
1449
1507
1581
1626
1633
1642
1678
1736
1755
1776
1795
1822
1842
1858
1872
1881
1894
1903
1908
1921
1935
1952
1962
1966
2038
2067
2079
2155
2173
2182
2218
2227
2265
2286
2326
2362
2366
2373
2409
2434
2461
2475
2484
2515
2556
2576
2578
2583
2605
2614
2679
2688
2722
2745
2751
2785
2839
2888
2902
2911
2934
2944
2958
2964
2965
2970
2974
3046
3091
3138
3168
3174
3226
3246
3258
3294
3345
3366
3390
3442
3505
3564
3595
3615
3622
3649
3663
3690
3694
3802
3852
3864
3865
3930
3946
3973
4054
4126
4162
4173
4185
4189
4191
4198
4209
4279
4306
4369
4414
4428
4464
4472
4557
4592
4594
4702
4743
4765
4788
4794
4832
4855
4880
4918
4954
4959
4960
4974
4981
5062
5071
5088
5098
5172
5242
5248
5253
5269
5298
5305
5386
5388
5397
5422
5458
5485
5526
5539
5602
5638
5642
5674
5772
5818
5854
5874
5915
5926
5935
5936
5946
5998
6036
6054
6084
6096
6115
6171
6178
6187
6188
6252
6259
6295
6315
6344
6385
6439
6457
6502
6531
6567
6583
6585
6603
6684
6693
6702
6718
6760
6816
6835
6855
6880
6934
6981
7026
7051
7062
7068
7078
7089
7119
7136
7186
7195
7227
7249
7287
7339
7402
7438
7447
7465
7503
7627
7674
7683
7695
7712
7726
7762
7764
7782
7784
7809
7824
7834
7915
7952
7978
8005
8014
8023
8073
8077
8095
8149
8154
8158
8185
8196
8253
8257
8277
8307
8347
8372
8412
8421
8466
8518
8545
8568
8628
8653
8680
8736
8754
8766
8790
8792
8851
8864
8874
8883
8901
8914
9015
9031
9036
9094
9166
9184
9193
9229
9274
9276
9285
9294
9296
9301
9330
9346
9355
9382
9386
9387
9396
9414
9427
9483
9522
9535
9571
9598
9633
9634
9639
9648
9657
9684
9708
9717
9735
9742
9760
9778
9840
9843
9849
9861
9880
9895
9924
9942
9968
9975
9985</pre>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">isSmith := proc(n::posint)
local factors, sumofDigits, sumofFactorDigits, x;
if isprime(n) then
Line 1,901 ⟶ 3,344:
end proc:
 
findSmith(10000);</langsyntaxhighlight>
{{out}}
<pre>[4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165, 1219, 1255, 1282, 1284, 1376, 1449, 1507, 1581, 1626, 1633, 1642, 1678, 1736, 1755, 1776, 1795, 1822, 1842, 1858, 1872, 1881, 1894, 1903, 1908, 1921, 1935, 1952, 1962, 1966, 2038, 2067, 2079, 2155, 2173, 2182, 2218, 2227, 2265, 2286, 2326, 2362, 2366, 2373, 2409, 2434, 2461, 2475, 2484, 2515, 2556, 2576, 2578, 2583, 2605, 2614, 2679, 2688, 2722, 2745, 2751, 2785, 2839, 2888, 2902, 2911, 2934, 2944, 2958, 2964, 2965, 2970, 2974, 3046, 3091, 3138, 3168, 3174, 3226, 3246, 3258, 3294, 3345, 3366, 3390, 3442, 3505, 3564, 3595, 3615, 3622, 3649, 3663, 3690, 3694, 3802, 3852, 3864, 3865, 3930, 3946, 3973, 4054, 4126, 4162, 4173, 4185, 4189, 4191, 4198, 4209, 4279, 4306, 4369, 4414, 4428, 4464, 4472, 4557, 4592, 4594, 4702, 4743, 4765, 4788, 4794, 4832, 4855, 4880, 4918, 4954, 4959, 4960, 4974, 4981, 5062, 5071, 5088, 5098, 5172, 5242, 5248, 5253, 5269, 5298, 5305, 5386, 5388, 5397, 5422, 5458, 5485, 5526, 5539, 5602, 5638, 5642, 5674, 5772, 5818, 5854, 5874, 5915, 5926, 5935, 5936, 5946, 5998, 6036, 6054, 6084, 6096, 6115, 6171, 6178, 6187, 6188, 6252, 6259, 6295, 6315, 6344, 6385, 6439, 6457, 6502, 6531, 6567, 6583, 6585, 6603, 6684, 6693, 6702, 6718, 6760, 6816, 6835, 6855, 6880, 6934, 6981, 7026, 7051, 7062, 7068, 7078, 7089, 7119, 7136, 7186, 7195, 7227, 7249, 7287, 7339, 7402, 7438, 7447, 7465, 7503, 7627, 7674, 7683, 7695, 7712, 7726, 7762, 7764, 7782, 7784, 7809, 7824, 7834, 7915, 7952, 7978, 8005, 8014, 8023, 8073, 8077, 8095, 8149, 8154, 8158, 8185, 8196, 8253, 8257, 8277, 8307, 8347, 8372, 8412, 8421, 8466, 8518, 8545, 8568, 8628, 8653, 8680, 8736, 8754, 8766, 8790, 8792, 8851, 8864, 8874, 8883, 8901, 8914, 9015, 9031, 9036, 9094, 9166, 9184, 9193, 9229, 9274, 9276, 9285, 9294, 9296, 9301, 9330, 9346, 9355, 9382, 9386, 9387, 9396, 9414, 9427, 9483, 9522, 9535, 9571, 9598, 9633, 9634, 9639, 9648, 9657, 9684, 9708, 9717, 9735, 9742, 9760, 9778, 9840, 9843, 9849, 9861, 9880, 9895, 9924, 9942, 9968, 9975, 9985]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">smithQ[n_] := Not[PrimeQ[n]] &&
Total[IntegerDigits[n]] == Total[IntegerDigits /@ Flatten[ConstantArray @@@ FactorInteger[n]],2];
Select[Range[2, 10000], smithQ]</syntaxhighlight>
 
Select[Range[2, 10000], smithQ]</lang>
 
{{out}}
<pre>{4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165, 1219, 1255, 1282, 1284, 1376, 1449, 1507, 1581, 1626, 1633, 1642, 1678, 1736, 1755, 1776, 1795, 1822, 1842, 1858, 1872, 1881, 1894, 1903, 1908, 1921, 1935, 1952, 1962, 1966, 2038, 2067, 2079, 2155, 2173, 2182, 2218, 2227, 2265, 2286, 2326, 2362, 2366, 2373, 2409, 2434, 2461, 2475, 2484, 2515, 2556, 2576, 2578, 2583, 2605, 2614, 2679, 2688, 2722, 2745, 2751, 2785, 2839, 2888, 2902, 2911, 2934, 2944, 2958, 2964, 2965, 2970, 2974, 3046, 3091, 3138, 3168, 3174, 3226, 3246, 3258, 3294, 3345, 3366, 3390, 3442, 3505, 3564, 3595, 3615, 3622, 3649, 3663, 3690, 3694, 3802, 3852, 3864, 3865, 3930, 3946, 3973, 4054, 4126, 4162, 4173, 4185, 4189, 4191, 4198, 4209, 4279, 4306, 4369, 4414, 4428, 4464, 4472, 4557, 4592, 4594, 4702, 4743, 4765, 4788, 4794, 4832, 4855, 4880, 4918, 4954, 4959, 4960, 4974, 4981, 5062, 5071, 5088, 5098, 5172, 5242, 5248, 5253, 5269, 5298, 5305, 5386, 5388, 5397, 5422, 5458, 5485, 5526, 5539, 5602, 5638, 5642, 5674, 5772, 5818, 5854, 5874, 5915, 5926, 5935, 5936, 5946, 5998, 6036, 6054, 6084, 6096, 6115, 6171, 6178, 6187, 6188, 6252, 6259, 6295, 6315, 6344, 6385, 6439, 6457, 6502, 6531, 6567, 6583, 6585, 6603, 6684, 6693, 6702, 6718, 6760, 6816, 6835, 6855, 6880, 6934, 6981, 7026, 7051, 7062, 7068, 7078, 7089, 7119, 7136, 7186, 7195, 7227, 7249, 7287, 7339, 7402, 7438, 7447, 7465, 7503, 7627, 7674, 7683, 7695, 7712, 7726, 7762, 7764, 7782, 7784, 7809, 7824, 7834, 7915, 7952, 7978, 8005, 8014, 8023, 8073, 8077, 8095, 8149, 8154, 8158, 8185, 8196, 8253, 8257, 8277, 8307, 8347, 8372, 8412, 8421, 8466, 8518, 8545, 8568, 8628, 8653, 8680, 8736, 8754, 8766, 8790, 8792, 8851, 8864, 8874, 8883, 8901, 8914, 9015, 9031, 9036, 9094, 9166, 9184, 9193, 9229, 9274, 9276, 9285, 9294, 9296, 9301, 9330, 9346, 9355, 9382, 9386, 9387, 9396, 9414, 9427, 9483, 9522, 9535, 9571, 9598, 9633, 9634, 9639, 9648, 9657, 9684, 9708, 9717, 9735, 9742, 9760, 9778, 9840, 9843, 9849, 9861, 9880, 9895, 9924, 9942, 9968, 9975, 9985}</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (table 5 16 taskOutput),
Stdout ("Found " ++ show (#taskOutput) ++ " Smith numbers.\n")]
where taskOutput = takewhile (<= 10000) smiths
 
table :: num->num->[num]->[char]
table cw w ns = lay (map concat (split (map fmt ns)))
where split [] = []
split ls = take w ls : split (drop w ls)
fmt n = reverse (take cw ((reverse (shownum n)) ++ repeat ' '))
 
smiths :: [num]
smiths = filter smith [1..]
 
smith :: num->bool
smith n = (~ prime) & digsum n = sum (map digsum facs)
where facs = factors n
prime = #facs <= 1
 
digsum :: num->num
digsum 0 = 0
digsum n = n mod 10 + digsum (n div 10)
 
factors :: num->[num]
factors = f [] 2
where f acc d n = acc, if d>n
= f (d:acc) d (n div d), if n mod d = 0
= f acc (d+1) n, otherwise</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
Found 376 Smith numbers.</pre>
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE SmithNumbers;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,974 ⟶ 3,471:
 
ReadChar;
END SmithNumbers.</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strformat
 
func primeFactors(n: int): seq[int] =
Line 2,015 ⟶ 3,512:
if cnt == 10:
cnt = 0
stdout.write("\n")</lang>
echo()</syntaxhighlight>
 
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265
<pre>
4 22 27 58 85 94 121 166 202 265
274 319 346 355 378 382 391 438 454 483
517 526 535 562 576 588 627 634 636 645
Line 2,060 ⟶ 3,557:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use Collection;
 
class Test {
Line 2,109 ⟶ 3,606:
return sum;
}
}</langsyntaxhighlight>
 
<pre>
Line 2,127 ⟶ 3,624:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">isSmith(n)=my(f=factor(n)); if(#f~==1 && f[1,2]==1, return(0)); sum(i=1, #f~, sumdigits(f[i, 1])*f[i, 2]) == sumdigits(n);
select(isSmith, [1..9999])</langsyntaxhighlight>
{{out}}
<pre>%1 = [4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165, 1219, 1255, 1282, 1284, 1376, 1449, 1507, 1581, 1626, 1633, 1642, 1678, 1736, 1755, 1776, 1795, 1822, 1842, 1858, 1872, 1881, 1894, 1903, 1908, 1921, 1935, 1952, 1962, 1966, 2038, 2067, 2079, 2155, 2173, 2182, 2218, 2227, 2265, 2286, 2326, 2362, 2366, 2373, 2409, 2434, 2461, 2475, 2484, 2515, 2556, 2576, 2578, 2583, 2605, 2614, 2679, 2688, 2722, 2745, 2751, 2785, 2839, 2888, 2902, 2911, 2934, 2944, 2958, 2964, 2965, 2970, 2974, 3046, 3091, 3138, 3168, 3174, 3226, 3246, 3258, 3294, 3345, 3366, 3390, 3442, 3505, 3564, 3595, 3615, 3622, 3649, 3663, 3690, 3694, 3802, 3852, 3864, 3865, 3930, 3946, 3973, 4054, 4126, 4162, 4173, 4185, 4189, 4191, 4198, 4209, 4279, 4306, 4369, 4414, 4428, 4464, 4472, 4557, 4592, 4594, 4702, 4743, 4765, 4788, 4794, 4832, 4855, 4880, 4918, 4954, 4959, 4960, 4974, 4981, 5062, 5071, 5088, 5098, 5172, 5242, 5248, 5253, 5269, 5298, 5305, 5386, 5388, 5397, 5422, 5458, 5485, 5526, 5539, 5602, 5638, 5642, 5674, 5772, 5818, 5854, 5874, 5915, 5926, 5935, 5936, 5946, 5998, 6036, 6054, 6084, 6096, 6115, 6171, 6178, 6187, 6188, 6252, 6259, 6295, 6315, 6344, 6385, 6439, 6457, 6502, 6531, 6567, 6583, 6585, 6603, 6684, 6693, 6702, 6718, 6760, 6816, 6835, 6855, 6880, 6934, 6981, 7026, 7051, 7062, 7068, 7078, 7089, 7119, 7136, 7186, 7195, 7227, 7249, 7287, 7339, 7402, 7438, 7447, 7465, 7503, 7627, 7674, 7683, 7695, 7712, 7726, 7762, 7764, 7782, 7784, 7809, 7824, 7834, 7915, 7952, 7978, 8005, 8014, 8023, 8073, 8077, 8095, 8149, 8154, 8158, 8185, 8196, 8253, 8257, 8277, 8307, 8347, 8372, 8412, 8421, 8466, 8518, 8545, 8568, 8628, 8653, 8680, 8736, 8754, 8766, 8790, 8792, 8851, 8864, 8874, 8883, 8901, 8914, 9015, 9031, 9036, 9094, 9166, 9184, 9193, 9229, 9274, 9276, 9285, 9294, 9296, 9301, 9330, 9346, 9355, 9382, 9386, 9387, 9396, 9414, 9427, 9483, 9522, 9535, 9571, 9598, 9633, 9634, 9639, 9648, 9657, 9684, 9708, 9717, 9735, 9742, 9760, 9778, 9840, 9843, 9849, 9861, 9880, 9895, 9924, 9942, 9968, 9975, 9985]</pre>
Line 2,134 ⟶ 3,631:
{{works with|PARI/GP|2.6.0+}}
2.6.0 introduced the <code>forcomposite</code> iterator, removing the need to check each term for primality.
<langsyntaxhighlight lang="parigp">forcomposite(n=4,9999, f=factor(n); if(#f~==1 && f[1,2]==1, next); if(sum(i=1, #f~, sumdigits(f[i, 1])*f[i, 2]) == sumdigits(n), print1(n" ")))</langsyntaxhighlight>
 
{{works with|PARI/GP|2.10.0+}}
2.10.0 gave us <code>forfactored</code> which speeds the process up by sieving for factors.
<langsyntaxhighlight lang="parigp">forfactored(n=4,9999, f=n[2]; if(#f~==1 && f[1,2]==1, next); if(sum(i=1, #f~, sumdigits(f[i, 1])*f[i, 2]) == sumdigits(n[1]), print1(n[1]" ")))</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 2,147 ⟶ 3,644:
 
the function IncDgtSum delivers the next sum of digits very fast (2.6 s for 1 to 1e9 )
<langsyntaxhighlight lang="pascal">program SmithNum;
{$IFDEF FPC}
{$MODE objFPC} //result and useful for x64
Line 2,429 ⟶ 3,926:
write(s:8,' smith-numbers up to ',actualNo.dgtnum:10);
end.
</langsyntaxhighlight>{{out}}
<pre>64-Bit FPC 3.1.1 -O3 -Xs i4330 3.5 Ghz
6 smith-numbers up to 100
Line 2,447 ⟶ 3,944:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/:all/;
my @smith;
forcomposites {
Line 2,453 ⟶ 3,950:
} 10000-1;
say scalar(@smith), " Smith numbers below 10000.";
say "@smith";</langsyntaxhighlight>
{{out}}
<pre>376 Smith numbers below 10000.
Line 2,460 ⟶ 3,957:
{{works with|ntheory|0.71+}}
Version 0.71 of the <code>ntheory</code> module added <code>forfactored</code>, similar to Pari/GP's 2.10.0 addition. For large inputs this can halve the time taken compared to <code>forcomposites</code>.
<langsyntaxhighlight lang="perl">use ntheory ":all";
my $t=0;
forfactored { $t++ if @_ > 1 && sumdigits($_) == sumdigits(join "",@_); } 10**8;
say $t;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
Note that the builtin prime_factors(4) yields {2}, rather than {2,2}, hence the inner loop (admittedly repeat..until style would be better, if only Phix had that).
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<lang Phix>function sum_digits(integer n, integer base=10)
<span style="color: #008080;">function</span> <span style="color: #000000;">sum_digits</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: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
integer res = 0
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
while n do
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
res += remainder(n,base)
<span style="color: #000000;">res</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;">base</span><span style="color: #0000FF;">)</span>
n = floor(n/base)
<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;">base</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function smith(integer n)
<span style="color: #008080;">function</span> <span style="color: #000000;">smith</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
sequence p = prime_factors(n)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
integer sp = 0, w = n
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</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>
for i=1 to length(p) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">sp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sum_digits</span><span style="color: #0000FF;">)),</span>
integer pi = p[i],
<span style="color: #000000;">sn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sum_digits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
spi = sum_digits(pi)
<span style="color: #008080;">return</span> <span style="color: #000000;">sn</span><span style="color: #0000FF;">=</span><span style="color: #000000;">sp</span>
while mod(w,pi)=0 do
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sp += spi
w = floor(w/pi)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</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;">10000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">smith</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">)</span>
end while
<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;">"%d smith numbers found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
end for
<!--</syntaxhighlight>-->
return sum_digits(n)=sp
{{out}}
end function
 
sequence s = {}
for i=1 to 10000 do
if smith(i) then s &= i end if
end for
?length(s)
s[8..-8] = {"..."}
?s</lang>
<pre>
376 smith numbers found: 4, 22, 27, 58, 85, 94, 121, 166, ..., 9861, 9880, 9895, 9924, 9942, 9968, 9975, 9985
376
{4,22,27,58,85,94,121,"...",9880,9895,9924,9942,9968,9975,9985}
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de factor (N)
(make
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N))
Line 2,524 ⟶ 4,012:
(println 'first-10 (head 10 L))
(println 'last-10 (tail 10 L))
(println 'all (length L)) )</langsyntaxhighlight>
{{out}}
<pre>first-10 (4 22 27 58 85 94 121 166 202 265)
last-10 (9843 9849 9861 9880 9895 9924 9942 9968 9975 9985)
all 376</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">smith: procedure options(main);
/* find the digit sum of N */
digitSum: procedure(nn) returns(fixed);
declare (n, nn, s) fixed;
s = 0;
do n=nn repeat(n/10) while(n>0);
s = s + mod(n,10);
end;
return(s);
end digitSum;
 
/* find and count factors of N */
factors: procedure(nn, facs) returns(fixed);
declare (n, nn, cnt, fac, facs(16)) fixed;
cnt = 0;
if nn<=1 then return(0);
 
/* factors of two */
do n=nn repeat(n/2) while(mod(n,2)=0);
cnt = cnt + 1;
facs(cnt) = 2;
end;
 
/* take out odd factors */
do fac=3 repeat(fac+2) while(fac <= n);
do n=n repeat(n/fac) while(mod(n,fac) = 0);
cnt = cnt + 1;
facs(cnt) = fac;
end;
end;
 
return(cnt);
end factors;
 
/* see if a number is a Smith number */
smith: procedure(n) returns(bit);
declare (n, nfacs, facsum, i, facs(16)) fixed;
nfacs = factors(n, facs);
if nfacs <= 1 then
return('0'b); /* primes are not Smith numbers */
 
facsum = 0;
do i=1 to nfacs;
facsum = facsum + digitSum(facs(i));
end;
return(facsum = digitSum(n));
end smith;
 
/* print all Smith numbers up to 10000 */
declare (i, cnt) fixed;
cnt = 0;
do i=2 to 9999;
if smith(i) then do;
put edit(i) (F(5));
cnt = cnt + 1;
if mod(cnt,16) = 0 then put skip;
end;
end;
put skip list('Found', cnt, 'Smith numbers.');
end smith;</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
Found 376 Smith numbers.</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="pli">100H:
 
/* CP/M BDOS FUNCTIONS */
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
/* PRINT NUMBER */
PR$NUM: PROCEDURE (N);
DECLARE S (6) BYTE INITIAL (' $');
DECLARE N ADDRESS, I BYTE;
I = 5;
DIGIT: S(I := I-1) = '0' + N MOD 10;
IF (N := N / 10) > 0 THEN GO TO DIGIT;
DO WHILE I>0;
S(I := I-1) =' ';
END;
CALL PRINT(.S);
END PR$NUM;
 
/* SUM OF DIGITS OF N */
DIGIT$SUM: PROCEDURE (N) BYTE;
DECLARE N ADDRESS, SUM BYTE;
SUM = 0;
DO WHILE N > 0;
SUM = SUM + N MOD 10;
N = N / 10;
END;
RETURN SUM;
END DIGIT$SUM;
 
/* FIND AND COUNT FACTORS OF N */
FACTORS: PROCEDURE (N, FACBUF) BYTE;
DECLARE (N, FACBUF, FAC, FACS BASED FACBUF) ADDRESS;
DECLARE COUNT BYTE;
COUNT = 0;
IF N <= 1 THEN RETURN 0;
 
/* TAKE OUT FACTORS OF TWO */
DO WHILE NOT N;
FACS(COUNT) = 2;
COUNT = COUNT + 1;
N = SHR(N, 1);
END;
 
/* TAKE OUT ODD FACTORS */
FAC = 3;
DO WHILE FAC <= N;
DO WHILE N MOD FAC = 0;
N = N / FAC;
FACS(COUNT) = FAC;
COUNT = COUNT + 1;
END;
FAC = FAC + 2;
END;
 
RETURN COUNT;
END FACTORS;
 
/* SEE IF A NUMBER IS A SMITH NUMBER */
SMITH: PROCEDURE (N) BYTE;
DECLARE FACS (16) ADDRESS;
DECLARE N ADDRESS, (F, NFACS, FACSUM) BYTE;
IF (NFACS := FACTORS(N, .FACS)) <= 1 THEN
RETURN 0; /* PRIMES ARE NOT SMITH NUMBERS */
 
FACSUM = 0;
DO F = 0 TO NFACS-1;
FACSUM = FACSUM + DIGIT$SUM(FACS(F));
END;
 
RETURN FACSUM = DIGIT$SUM(N);
END SMITH;
 
/* PRINT ALL SMITH NUMBERS UP TO 10.000 */
DECLARE (I, COUNT) ADDRESS;
COUNT = 0;
DO I = 2 TO 9$999;
IF SMITH(I) THEN DO;
CALL PR$NUM(I);
COUNT = COUNT + 1;
IF (COUNT AND 0FH) = 0 THEN
CALL PRINT(.(13,10,'$'));
END;
END;
CALL PRINT(.(13,10,'FOUND $'));
CALL PR$NUM(COUNT);
CALL PRINT(.' SMITH NUMBERS.$');
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
FOUND 376 SMITH NUMBERS.</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">DisableDebugger
#ECHO=#True ; #True: Print all results
Global NewList f.i()
Line 2,611 ⟶ 4,305:
Next
Print(~"\n"+Str(sn)+" Smith number up to "+Str(upto))
Return</langsyntaxhighlight>
{{out}}
<pre>.
Line 2,647 ⟶ 4,341:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">from sys import stdout
 
 
Line 2,694 ⟶ 4,388:
 
# entry point
list_smith_numbers(10_000)</langsyntaxhighlight>
{{out}}<pre>
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654 663 666
Line 2,703 ⟶ 4,397:
===Functional===
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Smith numbers'''
 
from itertools import dropwhile
Line 2,813 ⟶ 4,507:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Count of Smith Numbers below 10k:
Line 2,823 ⟶ 4,517:
Last 12 Smith Numbers below 10000:
9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985</pre>
 
=={{header|Quackery}}==
 
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 0
[ over while
swap 10 /mod
rot + again ]
nip ] is digitsum ( n --> n )
 
[]
10000 times
[ i^ primefactors
dup size 2 <
iff drop done
0 swap witheach
[ digitsum + ]
i^ digitsum =
if [ i^ join ] ]
say "There are "
dup size echo say " Smith numbers less than 10000." cr cr
10 split swap
say "They start: " echo cr
-10 split
say "...and end: " echo cr
drop</syntaxhighlight>
 
{{out}}
 
<pre>There are 376 Smith numbers less than 10000.
 
They start: [ 4 22 27 58 85 94 121 166 202 265 ]
...and end: [ 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985 ]
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require math/number-theory)
 
Line 2,851 ⟶ 4,581:
(let-values (([l r] (split-at ns (min (length ns) 15))))
(displayln l)
(loop r)))))</langsyntaxhighlight>
 
{{out}}
Line 2,864 ⟶ 4,594:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;
multi factors ( 1 ) { 1 }
Line 2,892 ⟶ 4,622:
say "{@s.elems} Smith numbers below 10_000";
say 'First 10: ', @s[ ^10 ];
say 'Last 10: ', @s[ *-10 .. * ];</langsyntaxhighlight>
{{out}}
<pre>376 Smith numbers below 10_000
Line 2,900 ⟶ 4,630:
=={{header|REXX}}==
===unoptimized===
<langsyntaxhighlight lang="rexx">/*REXX program finds (and maybe displays) Smith (or joke) numbers up to a given N.*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/
Line 2,930 ⟶ 4,660:
if z\==1 then do; f=f+1; $=$+sumD(z); end /*Residual? Then add Z*/
if f<2 then return 0 /*Prime? Not a Smith#*/
return $ /*else return sum digs.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
 
Line 2,952 ⟶ 4,682:
This REXX version uses a faster version of the &nbsp; '''sumFactr''' &nbsp; function; &nbsp; it's over &nbsp; '''20''' &nbsp; times faster than the
<br>unoptimized version using a (negative) one million for &nbsp; '''N'''.
<langsyntaxhighlight lang="rexx">/*REXX program finds (and maybe displays) Smith (or joke) numbers up to a given N.*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/
Line 2,989 ⟶ 4,719:
if z\==1 then do; f=f+1; $=$+sumD(z); end /*if a residual, then add Z*/
if f<2 then return 0 /*Is prime? It's not Smith#*/
return $ /*else, return sum of digs.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of (negative) one million: &nbsp; &nbsp; <tt> -1000000 </tt>}}
<pre>
Line 2,999 ⟶ 4,729:
{{incorrect|Ring| <br><br> This program does not find &nbsp; (nor show) &nbsp; all the Smith numbers <big> &lt; </big> 10,000. <br><br>}}
 
<langsyntaxhighlight lang="ring">
# Project : Smith numbers
 
Line 3,055 ⟶ 4,785:
end
return sum
</syntaxhighlight>
</lang>
Output:
<pre>
All the Smith Numbers < 1000 are:
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654 663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958 985
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ →STR 0
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB STR→ +
'''NEXT''' NIP
≫ ≫ '<span style="color:blue">∑DIGITS</span>' STO
≪ DUP FACTORS { }
1 PICK3 SIZE '''FOR''' j
1 PICK3 j 1 + GET '''START'''
OVER j GET + '''NEXT''' <span style="color:grey">@ expand the list of factors to address the 4 case</span>
2 '''STEP''' NIP
'''IF''' DUP SIZE 1 == '''THEN'''
DROP2 0
'''ELSE'''
≪ <span style="color:blue">∑DIGITS</span> == ≫ MAP ∑LIST
SWAP <span style="color:blue">∑DIGITS</span> ==
'''END'''
≫ ≫ '<span style="color:blue">SMITH?</span>' STO
≪ { }
4 10000 '''FOR''' n
'''IF''' n <span style="color:blue">SMITH?</span> '''THEN''' n + '''END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654 663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958 985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678 1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962 1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409 2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751 2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168 3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663 3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191 4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788 4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242 5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642 5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115 6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583 6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062 7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503 7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978 8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347 8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851 8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285 9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571 9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985}
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
class Integer
Line 3,078 ⟶ 4,839:
puts "#{res.size} smith numbers below #{n}:
#{res.first(5).join(", ")},... #{res.last(5).join(", ")}"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,086 ⟶ 4,847:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">fn main () {
//We just need the primes below 100
let primes = vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
Line 3,113 ⟶ 4,874:
}
println!("Smith numbers below 10000 ({}) : {:?}",solution.len(), solution);
}</langsyntaxhighlight>
 
{{out}}
Line 3,123 ⟶ 4,884:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object SmithNumbers extends App {
 
def sumDigits(_n: Int): Int = {
Line 3,164 ⟶ 4,925:
}
 
}</langsyntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program smith_numbers;
loop for s in [n : n in [2..9999] | smith(n)] do
putchar(lpad(str s, 5));
if (i +:= 1) mod 16=0 then print; end if;
end loop;
print;
 
proc smith(n);
facs := factors(n);
return #facs /= 1 and +/digits(n) = +/[+/digits(f) : f in facs];
end proc;
 
proc digits(n);
d := [];
loop while n > 0 do
d with:= n mod 10;
n div:= 10;
end loop;
return d;
end proc;
 
proc factors(n);
f := [];
loop while even n do
n div:= 2;
f with:= 2;
end loop;
d := 3;
loop while d <= n do
loop while n mod d = 0 do
n div:= d;
f with:= d;
end loop;
d +:= 2;
end loop;
return f;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985</pre>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var primes = Enumerator({ |callback|
static primes = Hash()
var p = 2
Line 3,203 ⟶ 5,029:
say "#{s.len} Smith numbers below 10_000"
say "First 10: #{s.first(10)}"
say "Last 10: #{s.last(10)}"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,212 ⟶ 5,038:
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">function factor(_n) {
n = _n
a = J(14, 2, .)
Line 3,284 ⟶ 5,110:
+-----------------------------------------------------------------------+
1 | 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985 |
+-----------------------------------------------------------------------+</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">extension BinaryInteger {
@inlinable
public var isSmith: Bool {
Line 3,335 ⟶ 5,161:
print("First 10 smith numbers: \(Array(smiths.prefix(10)))")
print("Last 10 smith numbers below 10,000: \(Array(smiths.suffix(10)))")
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,344 ⟶ 5,170:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc factors {x} {
# list the prime factors of x in ascending order
set result [list]
Line 3,385 ⟶ 5,211:
puts ...[lrange $smiths end-12 end]
puts "([llength $smiths] total)"
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,391 ⟶ 5,217:
...9760 9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
(376 total)</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn num_prime_factors(xx int) int {
mut p := 2
mut pf := 0
mut x := xx
if x == 1 {
return 1
}
for {
if (x % p) == 0 {
pf++
x /= p
if x == 1 {
return pf
}
} else {
p++
}
}
return 0
}
fn prime_factors(xx int, mut arr []int) {
mut p := 2
mut pf := 0
mut x := xx
if x == 1 {
arr[pf] = 1
return
}
for {
if (x % p) == 0 {
arr[pf] = p
pf++
x /= p
if x == 1 {
return
}
} else {
p++
}
}
}
fn sum_digits(xx int) int {
mut x := xx
mut sum := 0
for x != 0 {
sum += x % 10
x /= 10
}
return sum
}
fn sum_factors(arr []int, size int) int {
mut sum := 0
for a := 0; a < size; a++ {
sum += sum_digits(arr[a])
}
return sum
}
fn list_all_smith_numbers(max_smith int) {
mut arr := []int{}
mut a := 0
for a = 4; a < max_smith; a++ {
numfactors := num_prime_factors(a)
arr = []int{len: numfactors}
if numfactors < 2 {
continue
}
prime_factors(a, mut arr)
if sum_digits(a) == sum_factors(arr, numfactors) {
print("${a:4} ")
}
}
}
fn main() {
max_smith := 10000
println("All the Smith Numbers less than $max_smith are:")
list_all_smith_numbers(max_smith)
println('')
}
</syntaxhighlight>
{{out}}<pre>
All the Smith Numbers less than 10000 are:
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654 663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958 985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678 1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962 1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409 2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751 2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168 3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663 3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191 4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788 4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242 5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 56425674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115 6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583 6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062 7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503 7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978 8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347 8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851 8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285 9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571 9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var sumDigits = Fn.new { |n|
var sum = 0
while (n > 0) {
sum = sum + n%10
n = (n/10).floor
}
return sum
}
 
var smiths = []
System.print("The Smith numbers below 10,000 are:")
for (i in 2...10000) {
if (!Int.isPrime(i)) {
var thisSum = sumDigits.call(i)
var factors = Int.primeFactors(i)
var factSum = factors.reduce(0) { |acc, f| acc + sumDigits.call(f) }
if (thisSum == factSum) smiths.add(i)
}
}
Fmt.tprint("$4d", smiths, 16)</syntaxhighlight>
 
{{out}}
<pre>
The Smith numbers below 10,000 are:
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654
663 666 690 706 728 729 762 778 825 852 861 895 913 915 922 958
985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581 1626 1633 1642 1678
1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409
2434 2461 2475 2484 2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751
2785 2839 2888 2902 2911 2934 2944 2958 2964 2965 2970 2974 3046 3091 3138 3168
3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595 3615 3622 3649 3663
3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788
4794 4832 4855 4880 4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242
5248 5253 5269 5298 5305 5386 5388 5397 5422 5458 5485 5526 5539 5602 5638 5642
5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036 6054 6084 6096 6115
6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062
7068 7078 7089 7119 7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503
7627 7674 7683 7695 7712 7726 7762 7764 7782 7784 7809 7824 7834 7915 7952 7978
8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253 8257 8277 8307 8347
8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285
9294 9296 9301 9330 9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571
9598 9633 9634 9639 9648 9657 9684 9708 9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func SumDigits(N); \Return sum of digits in N
int N, S;
[S:= 0;
repeat N:= N/10;
S:= S+rem(0);
until N=0;
return S;
];
 
func SumFactor(N); \Return sum of digits of factors of N
int N0, N, F, S;
[N:= N0; F:= 2; S:= 0;
repeat if rem(N/F) = 0 then \found a factor
[S:= S + SumDigits(F);
N:= N/F;
]
else F:= F+1;
until F > N;
if F = N0 then return 0; \is prime
return S;
];
 
int C, N;
[C:= 0;
Format(5, 0);
for N:= 0 to 10_000-1 do
if SumDigits(N) = SumFactor(N) then
[RlOut(0, float(N));
C:= C+1;
if rem(C/20) = 0 then CrLf(0);
];
]</syntaxhighlight>
 
{{out}}
<pre>
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483
517 526 535 562 576 588 627 634 636 645 648 654 663 666 690 706 728 729 762 778
825 852 861 895 913 915 922 958 985 1086 1111 1165 1219 1255 1282 1284 1376 1449 1507 1581
1626 1633 1642 1678 1736 1755 1776 1795 1822 1842 1858 1872 1881 1894 1903 1908 1921 1935 1952 1962
1966 2038 2067 2079 2155 2173 2182 2218 2227 2265 2286 2326 2362 2366 2373 2409 2434 2461 2475 2484
2515 2556 2576 2578 2583 2605 2614 2679 2688 2722 2745 2751 2785 2839 2888 2902 2911 2934 2944 2958
2964 2965 2970 2974 3046 3091 3138 3168 3174 3226 3246 3258 3294 3345 3366 3390 3442 3505 3564 3595
3615 3622 3649 3663 3690 3694 3802 3852 3864 3865 3930 3946 3973 4054 4126 4162 4173 4185 4189 4191
4198 4209 4279 4306 4369 4414 4428 4464 4472 4557 4592 4594 4702 4743 4765 4788 4794 4832 4855 4880
4918 4954 4959 4960 4974 4981 5062 5071 5088 5098 5172 5242 5248 5253 5269 5298 5305 5386 5388 5397
5422 5458 5485 5526 5539 5602 5638 5642 5674 5772 5818 5854 5874 5915 5926 5935 5936 5946 5998 6036
6054 6084 6096 6115 6171 6178 6187 6188 6252 6259 6295 6315 6344 6385 6439 6457 6502 6531 6567 6583
6585 6603 6684 6693 6702 6718 6760 6816 6835 6855 6880 6934 6981 7026 7051 7062 7068 7078 7089 7119
7136 7186 7195 7227 7249 7287 7339 7402 7438 7447 7465 7503 7627 7674 7683 7695 7712 7726 7762 7764
7782 7784 7809 7824 7834 7915 7952 7978 8005 8014 8023 8073 8077 8095 8149 8154 8158 8185 8196 8253
8257 8277 8307 8347 8372 8412 8421 8466 8518 8545 8568 8628 8653 8680 8736 8754 8766 8790 8792 8851
8864 8874 8883 8901 8914 9015 9031 9036 9094 9166 9184 9193 9229 9274 9276 9285 9294 9296 9301 9330
9346 9355 9382 9386 9387 9396 9414 9427 9483 9522 9535 9571 9598 9633 9634 9639 9648 9657 9684 9708
9717 9735 9742 9760 9778 9840 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
</pre>
 
=={{header|zkl}}==
Uses the code (primeFactors) from [[Prime decomposition#zkl]].
<langsyntaxhighlight lang="zkl">fcn smithNumbers(N=0d10_000){ // -->(Smith numbers to N)
[2..N].filter(fcn(n){
(pfs:=primeFactors(n)).len()>1 and
n.split().sum(0)==primeFactors(n).apply("split").flatten().sum(0)
})
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">sns:=smithNumbers();
sns.toString(*).println(" ",sns.len()," numbers");</langsyntaxhighlight>
{{out}}
<pre>
2,094

edits