How to resolve the algorithm Square but not cube step by step in the Common Lisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Square but not cube step by step in the Common Lisp programming language
Table of Contents
Problem Statement
Show the first 30 positive integers which are squares but not cubes of such integers. Optionally, show also the first 3 positive integers which are both squares and cubes, and mark them as such.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Square but not cube step by step in the Common Lisp programming language
Source code in the common programming language
(defun cubep (n)
(loop for i from 1
for c = (* i i i)
while (<= c n)
when (= c n) do (return t)
finally (return nil)))
(defparameter squares (let ((n 0)) (lambda () (incf n) (* n n))))
(destructuring-bind (noncubes cubes)
(loop for s = (funcall squares) then (funcall squares)
while (< (length noncubes) 30)
if (cubep s) collect s into cubes
if (not (cubep s)) collect s into noncubes
finally (return (list noncubes cubes)))
(format t "Squares but not cubes:~%~A~%~%" noncubes)
(format t "Both squares and cubes:~%~A~%~%" cubes))
You may also check:How to resolve the algorithm Loops/For step by step in the bc programming language
You may also check:How to resolve the algorithm Arrays step by step in the NSIS programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Rename a file step by step in the Action! programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the C++ programming language