How to resolve the algorithm Smith numbers step by step in the Racket programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Smith numbers step by step in the Racket 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 Racket programming language
Source code in the racket programming language
#lang racket
(require math/number-theory)
(define (sum-of-digits n)
(let inr ((n n) (s 0))
(if (zero? n) s (let-values (([q r] (quotient/remainder n 10))) (inr q (+ s r))))))
(define (smith-number? n)
(and (not (prime? n))
(= (sum-of-digits n)
(for/sum ((pe (in-list (factorize n))))
(* (cadr pe) (sum-of-digits (car pe)))))))
(module+ test
(require rackunit)
(check-equal? (sum-of-digits 0) 0)
(check-equal? (sum-of-digits 33) 6)
(check-equal? (sum-of-digits 30) 3)
(check-true (smith-number? 166)))
(module+ main
(let loop ((ns (filter smith-number? (range 1 (add1 10000)))))
(unless (null? ns)
(let-values (([l r] (split-at ns (min (length ns) 15))))
(displayln l)
(loop r)))))
You may also check:How to resolve the algorithm Calendar step by step in the J programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Zeckendorf arithmetic step by step in the Elena programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Stirling numbers of the second kind step by step in the ALGOL 68 programming language