How to resolve the algorithm Smith numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smith numbers step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "/math" for Int
import "/fmt" for Fmt
import "/seq" for Lst

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)
    }
}
for (chunk in Lst.chunks(smiths, 16)) Fmt.print("$4d", chunk)

  

You may also check:How to resolve the algorithm Executable library step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the PHP programming language
You may also check:How to resolve the algorithm Kronecker product based fractals step by step in the 11l programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the K programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the AutoHotkey programming language