user
Smith numbers are numbers such that the sum of the decimal digits of the integers that make up that number is the same as the sum of the decimal digits of its prime factors excluding 1.

By definition, all primes are *excluded* as they (naturally) satisfy this condition!

Smith numbers are also known as   *joke*   numbers.

Example

Using the number **166**  
Find the prime factors of **166** which are: **2** x **83**  
Then, take those two prime factors and sum all their decimal digits: **2 + 8 + 3** which is **13**  
Then, take the decimal digits of **166** and add their decimal digits: **1 + 6 + 6** which is **13**  
Therefore, the number **166** is a Smith number.

Task

Write a program to find all Smith numbers *below* 10000.

See also

* from Wikipedia:   [Smith number].
* from MathWorld:   [Smith number].
* from OEIS A6753:   [OEIS sequence A6753].
* from OEIS A104170:   [Number of Smith numbers below 10^n].
* from The Prime pages:   [Smith numbers].
assistant
(formerly Perl 6)

```
constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;
 
multi factors ( 1 ) { 1 }
multi factors ( Int $remainder is copy ) {
  gather for @primes -> $factor {
 
    # if remainder < factor², we're done
    if $factor * $factor > $remainder {
      take $remainder if $remainder > 1;
      last;
    }
 
    # How many times can we divide by this prime?
    while $remainder %% $factor {
        take $factor;
        last if ($remainder div= $factor) === 1;
    }
  }
}
# Code above here is verbatim from RC:Count_in_factors#Raku

sub is_smith_number ( Int $n ) {
  (!$n.is-prime) and ( [+] $n.comb ) == ( [+] factors($n).join.comb );
}

my @s = grep &is_smith_number, 2 ..^ 10_000;
say "{@s.elems} Smith numbers below 10_000";
say 'First 10: ', @s[  ^10      ];
say 'Last  10: ', @s[ *-10 .. * ];

```

Output:

```
376 Smith numbers below 10_000
First 10: (4 22 27 58 85 94 121 166 202 265)
Last  10: (9843 9849 9861 9880 9895 9924 9942 9968 9975 9985)
```