Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

95% of /prog/ can't write this simple program

Name: Anonymous 2008-05-12 15:10

You are given an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). Each number appears only once in the array, except for one number, which occurs twice. Assume that you can access each element of the array only once.

Task: In your language of choice, write an function to find the repeated number without using auxiliary storage.

Name: Anonymous 2008-05-12 15:20

>>1

(define (sum l)
  (define (sum-iter l n)
    (if (null? l) n
    (sum-iter (cdr l) (+ n (car l))))
(sum-iter l 0)

(define (solution l)
  (* (car (reverse l)) (+ (car (reverse l)) 1) .5))

Something like this?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List