How to resolve the algorithm Happy numbers step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Happy numbers step by step in the Common Lisp programming language

Table of Contents

Problem Statement

From Wikipedia, the free encyclopedia:

Find and print the first   8   happy numbers. Display an example of your output here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Happy numbers step by step in the Common Lisp programming language

Source code in the common programming language

(defun sqr (n)
  (* n n))

(defun sum-of-sqr-dgts (n)
  (loop for i = n then (floor i 10)
        while (plusp i)
        sum (sqr (mod i 10))))
 
(defun happy-p (n &optional cache)
  (or (= n 1) 
      (unless (find n cache)
        (happy-p (sum-of-sqr-dgts n)
                 (cons n cache)))))
 
(defun happys (&aux (happys 0))
  (loop for i from 1
        while (< happys 8)
        when (happy-p i)
        collect i and do (incf happys)))
 
(print (happys))


  

You may also check:How to resolve the algorithm Kernighans large earthquake problem step by step in the Bash programming language
You may also check:How to resolve the algorithm Modular inverse step by step in the FunL programming language
You may also check:How to resolve the algorithm Element-wise operations step by step in the Ruby programming language
You may also check:How to resolve the algorithm Rename a file step by step in the REBOL programming language
You may also check:How to resolve the algorithm Dining philosophers step by step in the JoCaml programming language