International Mathematical Olympiad

July 17, 2009

The Standard Prelude makes tasks like this easy; we will use list-of (fold-of), sum, square, digits, ilog and permutations.

The first task is just a list comprehension that generates three-digit numbers and tests them for the required properties:

> (list-of n
    (n range 100 1000)
    (zero? (modulo n 11))
    (= (/ n 11)
       (sum (map square (digits n)))))
(550 803)

The second task calls for us to dissect a number and stitch it back together, looping until we find the answer:

> (let loop ((n 6))
    (let ((m (+ (* 6 (expt 10 (ilog 10 n))) (quotient n 10))))
      (if (= (* 4 n) m) n
        (loop (+ n 10)))))
153846

The third task is a bit harder. In-position and two-consecutive count the number of contestants in the specified positions; the list comprehension loops over the permutations of (A B C D E), testing each one.

(define (in-position actual predicted)
  (let loop ((a actual) (p predicted) (n 0))
    (cond ((null? a) n)
          ((equal? (car a) (car p))
            (loop (cdr a) (cdr p) (+ n 1)))
          (else (loop (cdr a) (cdr p) n)))))

(define (two-consecutive actual predicted)
  (let loop ((a actual) (p predicted) (n 0))
    (cond ((or (null? a) (null? p)) n)
          ((null? (cdr p)) n)
          ((null? (cdr a))
            (loop actual (cdr p) n))
          ((and (equal? (car a) (car p))
                (equal? (cadr a) (cadr p)))
            (loop (cddr a) p (+ n 1)))
          (else (loop (cdr a) p n)))))

> (list-of p
    (p in (permutations '(a b c d e)))
    (= (in-position p '(a b c d e)) 0)
    (= (two-consecutive p '(a b c d e)) 0)
    (= (in-position p '(d a e c b)) 2)
    (= (two-consecutive p '(d a e c b)) 2))
((e d a c b))

You can run the suggested solution at http://programmingpraxis.codepad.org/JRGmt2wZ.

About these ads

Pages: 1 2

3 Responses to “International Mathematical Olympiad”

  1. iyo said

    Hi, thanks for problems. I think they are really simple to solve (you can use brute force to solve them without any difficulties :-)

    The last one I even solved on paper. I share my solution:

    When you took the second prediction and mark 2 consecutive contestants creating 2 disjoint pairs, you get 3 choices:

    DA EC and B is somewhere else
    DA CB and E is somewhere else
    AE CB and D is somewhere else

    Your goal is to place the “unpaired” contestant in good place.

    1st choice:

    B DA EC – wrong, none of them finished as it was predicted
    DA B EC – B can’t be after A
    DA EC B – all of them finished at it was predicted

    2nd choice:
    E DA CB – the correct answer!!! only C,B are at the right places
    =========================================================
    DA E CB – all of them finished at it was predicted
    DA CB E – C is on the 3rd place – it can’t be because of the first prediction

    3rd choice:
    D AE CB – all of them finished at it was predicted
    AE D CB – A is on the 1st place – it can’t be because of the first prediction
    AE CB D – A is on the 1st place – it can’t be because of the first prediction

  2. swaraj said

    ruby solution for first problem (http://codepad.org/eq5LqB7o)

  3. Vivek N said

    How do you solve these problems with a mathematical approach, for example if N is very large (something like 10^9, where bruteforce won’t work).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 464 other followers

%d bloggers like this: