Spoilers below if you count a failed attempt 🙂
I am trying to find the sum of all the numbers under 28123 that cannot be written as the sum of two abundant numbers. My code gets close to the right answer, but I'm not finding out where I went wrong. I am quite sure its not in the factors, list-sum, or abundant? procedures, and I'm not using amicable? or amis which deal with amicable numbers instead of abundant numbers, (but they are correct, I used them in another problem.)
So I run (list-sum (sums lsta lstb 28123)) and get 4190404 which is incorrect. I found a list of what sums lsta lstb 109) should be and I was correct. My highest number is also correct, so my mistake lies in the middle or in my summing function (which hasn't failed me yet).
I am trying to find the sum of all the numbers under 28123 that cannot be written as the sum of two abundant numbers. My code gets close to the right answer, but I'm not finding out where I went wrong. I am quite sure its not in the factors, list-sum, or abundant? procedures, and I'm not using amicable? or amis which deal with amicable numbers instead of abundant numbers, (but they are correct, I used them in another problem.)
So I run (list-sum (sums lsta lstb 28123)) and get 4190404 which is incorrect. I found a list of what sums lsta lstb 109) should be and I was correct. My highest number is also correct, so my mistake lies in the middle or in my summing function (which hasn't failed me yet).
Code:
(define (factors n)
(define (*factors d)
(cond ((>= d (sqrt n)) (list))
((= (modulo n d) 0) (cons d (cons (/ n d)(*factors (+ d 1)))))
(else (*factors (+ d 1)))))
(cons 1 (*factors 2)))
(define (list-sum lst)
(cond
((null? lst)
0)
((pair? (car lst))
(+(list-sum (car lst)) (list-sum (cdr lst))))
(else
(+ (car lst) (list-sum (cdr lst))))))
(define (amicable? int)
(let ((x (list-sum (factors int))))
(if (not (= x int)) (= int (list-sum (factors x))) #f)))
(define (abundant? int)
(< int (list-sum (factors int))))
(define (amis d n)
(define (*start d)
(cond ((>= d n) (list))
((eq? (amicable? d) #t) (cons d (*start (+ d 1))))
(else (*start (+ d 1)))))
(*start d))
(define (abus d n)
(define (*start d)
(cond ((>= d n) (list))
((eq? (abundant? d) #t) (cons d (*start (+ d 1))))
(else (*start (+ d 1)))))
(*start d))
(define lsta
(abus 1 28123))
(define lstb
(reverse lsta))
(define (sumcar lst1 lst2)
(+ (car lst1) (car lst2)))
(define (sums lst1 lst2 ctdn)
(cond ((< ctdn 1) '())
((not (pair? lst2)) (cons ctdn (sums lsta lstb (- ctdn 1))))
((>= (sumcar lst1 lst2) (+ 1 ctdn)) (sums lst1 (cdr lst2) ctdn))
((< (sumcar lst1 lst2) ctdn) (sums (cdr lst1) lst2 ctdn))
(else (sums lsta lstb (- ctdn 1)))))