Write a Lisp program to calculate the sum of the first N positive integers

AI Thread Summary
The discussion revolves around writing a Lisp program to calculate the sum of the first N positive integers. The key insight shared is that the sum can be expressed recursively: the sum of the first N numbers is N plus the sum of the first N-1 numbers. An initial attempt at coding this in Lisp is presented, but it encounters a read error due to syntax issues. The correct approach emphasizes defining a function that checks if N is a positive integer, returning 0 for values less than 1, and recursively calculating the sum for valid inputs. The emphasis is on translating the recursive definition into proper Lisp syntax to avoid errors and achieve the desired functionality.
fernanroy
Messages
6
Reaction score
0
Help! Please

I am trying to write a Lisp program to calculate the sum of the first N positive integers where, for example, when N = 6 the sum of the first N = 21

Any Ideas?
 
Technology news on Phys.org
Sure: the sum of the first N numbers is N plus the sum of the first N-1 numbers. That's a very strong hint. Heck, it's pretty much exactly the answer in LISP... :-p
 
out of whack said:
Sure: the sum of the first N numbers is N plus the sum of the first N-1 numbers. That's a very strong hint. Heck, it's pretty much exactly the answer in LISP... :-p

Would it be something like this?

(defun nsum (n)
(if (integerp n)
(if (or (= n 0) (< n 1))
Please enter the positive integer
(* n(/ 2(+ n 1))))))

Regards
 
This attempt yields the error: *** Read error: The input could not be tokenized at 2, 2. ***

> (defun sum (n1 n2)
“Returns the sum of two positive integers.”
(assert
(and (integerp n1) (>= n1 0))
(n1)
“N1 must be a nonnegative integer, instead it’s ~S.”
(n1)
(assert
(integerp n2)
(n2)
:N2 must be an integer, instead it’s ~S.”
n2)
(if (zerop n1) n2
(sum (1- n1) (1+n2))))
 
Even more obvious hint: nsum of n is defined as 0 if n is less than 1, otherwise it is n plus the nsum of (n-1). This is true in English, right? So write exactly that, but in LISP instead of English.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
25
Views
2K
Replies
8
Views
2K
Replies
89
Views
6K
Replies
16
Views
2K
Replies
13
Views
3K
Replies
8
Views
2K
Back
Top