How to resolve the algorithm Smith numbers step by step in the M2000 Interpreter programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Smith numbers step by step in the M2000 Interpreter programming language
Table of Contents
Problem Statement
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.
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.
Write a program to find all Smith numbers below 10000.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Smith numbers step by step in the M2000 Interpreter programming language
Source code in the m2000 programming language
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
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
You may also check:How to resolve the algorithm Even or odd step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the Haskell programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Delphi programming language
You may also check:How to resolve the algorithm Variadic function step by step in the XLISP programming language
You may also check:How to resolve the algorithm Detect division by zero step by step in the RPGIV programming language