Google Treasure Hunt 2008 Puzzle 4

April 14, 2009

This puzzle comes from the 2008 Google Treasure Hunt:

Find the smallest number that can be expressed as the sum of 7 consecutive prime numbers, the sum of 17 consecutive prime numbers, the sum of 41 consecutive prime numbers, the sum of 541 consecutive prime numbers, and is itself a prime number.

For example, 41 is the smallest prime number that can be expressed as the sum of 3 consecutive primes (11 + 13 + 17 = 41) and the sum of 6 consecutive primes (2 + 3 + 5 + 7 + 11 + 13 = 41).

Your task is to find the number. When you are finished, you can read or run a suggested solution, or post your solution or discuss the exercise in the comments below.

Advertisement

Pages: 1 2

2 Responses to “Google Treasure Hunt 2008 Puzzle 4”

  1. jcs said

    Here is a solution that makes no assumptions about the size of the solution and does not compute unnecessary sums. The primes module provides a stream (as in SICP) of primes and the primitive prime?; push, pop, and dotimes are Scheme implementations of the like-named Common Lisp macros.


    (use-modules (private primes)) ;for stream-of-primes, prime?

    ;;; Queue object that holds n consecutive primes and sums them
    (define make-summing-queue
      (lambda (n)
        (let ((front '()) (back '()) (primes stream-of-primes))
          (define popq ;just throw away the head of queue
            (lambda ()
              (unless (pop front)
                (set! front (reverse back))
                (set! back '())
                (pop front))))
          (define popp ;return the next prime
            (lambda ()
              (let ((p (str-car primes)))
                (set! primes (str-cdr primes))
                p)))
          (dotimes (i n)
            (push (popp) back))
          (lambda (cmd)
            (case cmd
              ((next) (push (popp) back) (popq)) ;shift sum
              ((fb) (format #t "front: ~s, back: ~s\n" front back))
              ((show) (princ (append front (reverse back))))
              ((sum) (apply + (append front back))))))))

    ;;; Find sums of primes

    (define queues (list (make-summing-queue 541)
                         (make-summing-queue 41)
                         (make-summing-queue 17)
                         (make-summing-queue 7)))

    (define run
      (lambda (queues)
        (let ((first (car queues)))
          (while (not (prime? (first 'sum)))
            (first 'next))
          (let iter ((goal (first 'sum)) (rest (cdr queues)))
            (cond ((null? rest) (first 'sum))
                  ((= goal ((car rest) 'sum)) (iter goal (cdr rest)))
                  ((< goal ((car rest) 'sum)) (first 'next) (run queues))
                  (else ((car rest) 'next) (iter goal rest)))))))

    ;; (for-each (lambda (q) (q 'show)) queues)
    ;; to see lists of primes

  2. FalconNL said

    In Haskell:

    import Data.List
    import Data.Numbers.Primes
    
    main = print $ test [541,41,17,7,1]
    
    consecutivePrimes n = map (sum . take n) $ tails primes
    
    test = head . foldl1 (\a b -> filter (\x -> elem x $ takeWhile (<= x) b) a) . map consecutivePrimes

Leave a Reply

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

Gravatar
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 131 other followers