• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

projecteuler #23 scheme failed attempt (spoiler?)

Jjoshua2

Senior member
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).

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)))))
 
You didn't perform proper unit testing. For example, try printing (factors 1)...(factors 10) and see if you notice anything a little off. The problem is pretty glaring. If you can't figure it out, scroll down for a stronger hint.




























Take a closer look at your ((>= d (sqrt n)) (list)) condition.
 
Thx man. I suppose factors 9 should be 1 3 3 and not just (1 3) or 1 so I found my bug so I'll try again. I don't think 1 3 3 vs 1 3 matters except for factors 4 because that is the only one that changes between abundant and not. 4 is not abundant, so I don't see why changing that changed my answer.

Edit: still not quite working. I'm getting 3906313 now which is not right.
Edit2:Got it! Now I just knew why... (factors n^2 must be return 1 n, not 1 and not 1 n n) why? the difference was not just (factors 4) because the answer was way off not 4 off.
 
Last edited:
Back
Top