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].


 
Module Checkit {
      Set Fast !
      Form 80, 40
      Refresh
      Function Smith(max=10000) {
            Function SumDigit(a$) {
                  def long sum
                  For i=1 to len(a$) {sum+=val(mid$(a$,i, 1)) }
                  =sum
            }      
                  x=max
                  \\ Euler's Sieve
                        Dim r(x+1)=1
                        k=2
                        k2=k**2
                        While k2<x {
                              For m=k2 to x step k {r(m)=0}
                              Repeat {
                              k++ :  k2=k**2
                              } Until r(k)=1 or k2>x
                        }
            r(0)=0
            smith=0
            smith2=0
            lastI=0
            inventory smithnumbers
            Top=max div 100
            c=4
            For i=4 to max {
                if c> top then  print over $(0,6), ceil(i/max*100);"%" : Refresh : c=1
                c++
                  if r(i)=0 then {
                        smith=sumdigit(str$(i)) : lastI=i
                        smith2=0
                        do {
                              ii=int(sqrt(i))+1
                              do {  ii-- :   while r(ii)<>1 {ii--} } until i mod ii=0 
                               if ii<2 then smith2+=sumdigit(str$(i)):exit
                               smith3=sumdigit(str$(ii))
                              do {
                                   smith2+=smith3
                                    i=i div ii : if ii<2  or i<2 then exit
                              } until  i mod ii<>0  or smith2>smith
                        } until i<2 or smith2>smith
                       If  smith=smith2 then Append smithnumbers, lastI
                  }
            }
            =smithnumbers
      }
      const MaxNumbers=10000
      numbers= Smith(MaxNumbers)
      Print
      Print $(,5), numbers
      Print
      Print format$(" {0} smith numbers found <= {1}", Len(numbers), MaxNumbers)
}
Checkit
 