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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

A number is an   attractive number   if the number of its prime factors (whether distinct or not) is also prime.

The number   20,   whose prime decomposition is   2 × 2 × 5,   is an   attractive number   because the number of its prime factors   (3)   is also prime.

Show sequence items up to   120.

Let's start with the solution:

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

Source code in the common programming language

(defun attractivep (n)
  (primep (length (factors n))) )

; For primality testing we can use different methods, but since we have to define factors that's what we'll use
(defun primep (n)
  (= (length (factors n)) 1) )

(defun factors (n)
  "Return a list of factors of N."
  (when (> n 1)
    (loop with max-d = (isqrt n)
      for d = 2 then (if (evenp d) (+ d 1) (+ d 2)) do
      (cond ((> d max-d) (return (list n))) ; n is prime
        ((zerop (rem n d)) (return (cons d (factors (truncate n d)))))))))


  

You may also check:How to resolve the algorithm Empty program step by step in the min programming language
You may also check:How to resolve the algorithm Memory layout of a data structure step by step in the Raku programming language
You may also check:How to resolve the algorithm Include a file step by step in the BaCon programming language
You may also check:How to resolve the algorithm Department numbers step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm String append step by step in the Elixir programming language