### Author Topic: [challenge] A30 : Sum of Missing Numbers  (Read 1457 times)

0 Members and 1 Guest are viewing this topic.

#### JohnK

• Seagull
• Posts: 10170
##### [challenge] A30 : Sum of Missing Numbers
« on: March 02, 2022, 09:58:49 AM »
Create a function that returns the sum of missing numbers in a list.

Examples
(sumMissing '(1 3 5 7 10))
> 29
;; 2 + 4 + 6 + 8 + 9

(sumMissing '(10 7 5 3 1))
> 29

(sumMissing '(10 20 30 40 50 60))
> 1575
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

#### pBe

• Bull Frog
• Posts: 401
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #1 on: March 02, 2022, 11:34:28 AM »
Code - Auto/Visual Lisp: [Select]
1. (defun sumMissing ( l / lv ms)
2.   (repeat (- (setq lv (apply 'max l))
3.              (apply 'min l)
4.           )
5.     (if (not (member (setq lv (1- lv)) l))
6.       (setq ms (cons lv ms))
7.     )
8.   )
9.   (apply '+ ms)
10. )

Code - Auto/Visual Lisp: [Select]
1. (defun sumMissing ( l / lv ms)
2. (setq ms 0)
3.   (repeat (- (setq lv (apply 'max l))
4.              (apply 'min l)
5.           )
6.     (if (not (member (setq lv (1- lv)) l))
7.       (setq ms (+ ms lv))
8.     )
9.   )
10. ms
11. )
« Last Edit: March 02, 2022, 11:41:03 AM by pBe »

#### JohnK

• Seagull
• Posts: 10170
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #2 on: March 02, 2022, 11:59:05 AM »
pretty straight forward approach so mine was similar.
Code - Auto/Visual Lisp: [Select]
1. (defun summissing (lst)
2.   ;; summissing
3.   ;; this function will sum the missing values in list
4.   ;; of integers.
5.   ;;
6.   ;; (sumMissing '(1 3 5 7 10))
7.   ;; > 29
8.   (setq cntr (apply 'max lst)
9.         min? (apply 'min lst)
10.         sum 0)
11.   (while (>= cntr min?)
12.          (if (not (member cntr lst))
13.            (setq sum (+ sum cntr)))
14.          (setq cntr (1- cntr))
15.          )
16.   sum
17.   )
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

#### gile

• Water Moccasin
• Posts: 2406
• Marseille, France
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #3 on: March 02, 2022, 12:08:27 PM »
Code - Auto/Visual Lisp: [Select]
1. (defun sumMissing (l / i n)
2.   (setq n 0)
3.   (repeat (setq i (1+ (apply 'max l)))
4.     (setq n (+ n (setq i (1- i))))
5.   )
6.   (- n (apply '+ l))
7. ))

_\$ (sumMissing '(1 3 5 7 10))
29
_\$ (sumMissing '(10 7 5 3 1))
29
_\$ (sumMissing '(5 1 10 3 7))
29
_\$ (sumMissing '(10 20 30 40 50 60))
1620
« Last Edit: March 02, 2022, 12:28:54 PM by gile »
Speaking English as a French Frog

#### pBe

• Bull Frog
• Posts: 401
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #4 on: March 02, 2022, 12:31:16 PM »
_\$ (sumMissing '(10 20 30 40 50 60))
1620

What's going on there?

(sumMissing:gile '(45 47 98 104)) --> 5166
(sumMissing '(45 47 98 104)) -->  4176

And this is flawed
Code - Auto/Visual Lisp: [Select]
1. (Defun sumMissing:math (l)
2.   (setq lo (apply 'min l) hi (apply 'max l)
3.   )
4.   (- (* (/ (+ (- hi lo)) 2) (+ lo hi))
5.      (- (apply '+ l) (+ lo hi))
6.   )
7. )

(sumMissing:math '(45 47 98 104))
4176
_\$ (sumMissing:math '(1 3 5 7 10))
29
_\$ (sumMissing:math '(10 20 30 40 50 60))
1610
« Last Edit: March 02, 2022, 12:40:51 PM by pBe »

#### gile

• Water Moccasin
• Posts: 2406
• Marseille, France
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #5 on: March 02, 2022, 12:44:47 PM »
My upper routine is wrong.
This one works as expected:
Code - Auto/Visual Lisp: [Select]
1. (defun sumMissing (l / i n)
2.   (setq n (apply 'max l))
3.   (repeat (- (setq i n) (apply 'min l))
4.     (setq n (+ n (setq i (1- i))))
5.   )
6.   (- n (apply '+ l))
7. )
Speaking English as a French Frog

#### Marc'Antonio Alessi

• Swamp Rat
• Posts: 1336
• Marco
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #6 on: March 02, 2022, 01:14:09 PM »
too late…
Code: [Select]
`(defun SumMissing_A (L / m n o)  (setq m (apply 'max L)  n (apply 'min L) o 0)  (repeat (- m n) (setq o (+ o n) n (1+ n)))  (- (+ o m) (apply '+ L)))`

#### kirby

• Newt
• Posts: 120
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #7 on: March 02, 2022, 01:33:29 PM »
Yet another...
Code - Auto/Visual Lisp: [Select]
1. (defun C:Test-A30 ( / MyTestLists CNT MyList Ans)
2. ; Test routine for Challenge A30
3.
4. (setq MyTestLists (list
5.         '(1 3 5 7 10)           ; 29
6.         '(10 7 5 3 1)           ; 29
7.         '(10 20 30 40 50 60)    ; 1575
8. ))
9.
10. (setq CNT 0)
11. (repeat (length MyTestLists)
12.         (setq MyList (nth CNT MyTestLists))
13.         (prompt "\n  List = ")(princ MyList)
14.
15.         (setq Ans (SumMissing-kirby MyList))
16.         (prompt "\n    --> ")(princ Ans)
17.         (princ)
18.
19.         (setq CNT (1+ CNT))
20. )
21. )
22.
23. (defun SumMissing-kirby (MyList /
24.                         Verbose OutVal CNT Item1 Item2 Diff StartVal CNT1 NextValue
25.                         )
26. ; Sum missing values in a list of integers
27. ; Input:
28. ;       MyList - (list) list of integers, sorted in either ascending or descending order
29. ; Returns:
30. ;       sum of missing values (integer)
31.
32. (setq Verbose 0)        ; 1 to show additional/debug info
33.
34. (setq OutVal 0)
35.
36. ; Loop though list
37. (setq CNT 0)
38. (repeat (1- (length MyList))
39.         (setq Item1 (nth CNT MyList))
40.         (setq Item2 (nth (1+ CNT) MyList))
41.         (setq Diff (abs (- Item2 Item1)))       ; difference between values
42.         (setq StartValue (min Item1 Item2))     ; start with minimum so increment is 1+
43.
44.         (if (eq Verbose 1)
45.                 (prompt "\n  Values ")(princ Item1)(prompt "  ")(princ Item2)(prompt "  Difference = ")(princ Diff)(princ)
46.           )
47.         )
48.
49.         ; loop through gap between values
50.         (if (> Diff 1)
51.                 (setq NextValue StartValue)     ; initialize next value to sum
52.
53.                 (repeat (1- Diff)
54.                         (setq NextValue (1+ NextValue))         ; update next value
55.                         (setq OutVal (+ OutVal NextValue))      ; sum
56.
57.                         (if (eq Verbose 1)
58.                                 (prompt "\n    Next Value = ")(princ NextValue)(prompt "  Running Total = ")(princ OutVal)(princ)
59.                           )
60.                         )
61.                 ) ; close repeat
62.           )
63.         ) ; close if
64.
65.         (setq CNT (1+ CNT))
66. ) ; close repeat
67. OutVal
68. )
69.

Results:
Command: TEST-A30
List = (1 3 5 7 10)
--> 29
List = (10 7 5 3 1)
--> 29
List = (10 20 30 40 50 60)
--> 1575

#### bruno_vdh

• Newt
• Posts: 82
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #8 on: March 02, 2022, 04:48:56 PM »
Hello, by taking up an answer made previously on a French-speaking forum
https://www.developpez.net/forums/d1446541/autres-langages/langages-fonctionnels/lisp/lisp-somme-n-premiers-nombres/#post7863527

Sum of the first N natural integers
Code - Auto/Visual Lisp: [Select]
1. ;; Somme des N premiers entiers naturels / Sum of the first N natural integers
2. (defun Snp (n) (/ (* n (1+ n)) 2))
For the record, it is with this reasoning by recurrence S = 1 + 2 + 3 + ... + (n-1) + n
that the little Carl Friedrich Gauss (nine years old at the time) deduced the following formula S= n(n+1)/2,
concerning the sum of the first N natural integers.

[challenge] A30 : Sum of Missing Numbers
Code: [Select]
`sumMissing(l) = (Snp (max(l)) - (Snp (min(l) - 1)) - (Somme(l))`
Code - Auto/Visual Lisp: [Select]
1. (defun sumMissing (l) (- (Snp (apply 'max l)) (Snp (1- (apply 'min l))) (apply '+ l)))
Code: [Select]
`_\$ (sumMissing '(10 20 30 40 50 60))1575_\$(sumMissing '(1 3 5 7 10))29`

« Last Edit: March 02, 2022, 05:38:35 PM by bruno_vdh »

#### JohnK

• Seagull
• Posts: 10170
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #9 on: March 02, 2022, 09:44:23 PM »
Great post!
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

#### Marc'Antonio Alessi

• Swamp Rat
• Posts: 1336
• Marco
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #10 on: March 03, 2022, 03:37:26 AM »
Code: [Select]
`(defun SumMissing_A2 (L / m n)  (setq m (apply 'max L)  n (apply 'min L))  (- (/ (* (+ n m) (- m n -1)) 2)  (apply '+ L)))`

#### kirby

• Newt
• Posts: 120
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #11 on: March 03, 2022, 10:18:02 AM »
@bruno_vdh

Thanks!  Interesting and efficient solution.

#### apricot125

• Mosquito
• Posts: 13
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #12 on: July 11, 2022, 11:05:06 PM »
Code: [Select]
`(defun SumMissing_A2 (L / m n)  (setq m (apply 'max L)  n (apply 'min L))  (- (/ (* (+ n m) (- m n -1)) 2)  (apply '+ L)))`

_\$ (sumMissing_A2 '(1 3 5 7  7 10))
22
it's may not correct.

#### Marc'Antonio Alessi

• Swamp Rat
• Posts: 1336
• Marco
##### Re: [challenge] A30 : Sum of Missing Numbers
« Reply #13 on: July 12, 2022, 04:23:38 PM »
Code: [Select]
`(defun SumMissing_A2 (L / m n)  (setq m (apply 'max L)  n (apply 'min L))  (- (/ (* (+ n m) (- m n -1)) 2)  (apply '+ L)))`

_\$ (sumMissing_A2 '(1 3 5 7  7 10))
22
it's may not correct.

Code: [Select]
`(defun sumMissing (l / i n) ; gile  (setq n (apply 'max l))  (repeat (- (setq i n) (apply 'min l))    (setq n (+ n (setq i (1- i))))  )  (- n (apply '+ l)))(defun SumMissing_A (L / m n o)  (setq m (apply 'max L)  n (apply 'min L) o 0)  (repeat (- m n) (setq o (+ o n) n (1+ n)))  (- (+ o m) (apply '+ L)))(defun SumMissing_A2 (L / m n)  (setq m (apply 'max L)  n (apply 'min L))  (- (/ (* (+ n m) (- m n -1)) 2)  (apply '+ L)))`
Code: [Select]
`(sumMissing    '(1 3 5 7 7 10)) => 22(sumMissing_A  '(1 3 5 7 7 10)) => 22(sumMissing_A2 '(1 3 5 7 7 10)) => 22`