; Program: A Pile Splitting Problem ; Solution: Originally written by Max Hailperin; modified by ; Bill Marion (2/10/06) ; ; Statement of the Problem: Given n objects in a pile (n >= 1), ; split the pile into two smaller piles. Continue to split each ; pile into two smaller piles until there are n piles of size one. ; At each splitting compute the product of the sum and product of the size ; of the two smaller piles. Once there are n piles, the sum of all the products ; is computed. The result will always be the same no matter ; how each of the piles is split into two smaller piles. The sum ; is a function of n. (Rosen K., 2003; Tanton, J. 2004) ; This computational process allows the user repeatedly to input the number ; of objects in the pile, n, (0 to quit), and, if n >= 1, calls the function ; to compute the sum of the products of the sum and product and displays that sum. (define pile-splitting-2 (lambda () (newline) (display "Enter the number of objects in the pile--or 0 to quit") (newline) (let ((n (read))) (if (= n 0) (begin (newline) (display "finished") (newline)) (begin (let ((sumOfProductsOfSumProd (sum-of-products-of-sum-and-product n))) (newline) (display "The sum of the products of sum and product: ") (display sumOfProductsOfSumProd) (newline)) (pile-splitting-2)))))) ; This computational process generates a recursive process and splits ; the piles randomly into k and n-k piles until there are n piles of ; size 1. Then, the sum of the products of the sum and product is computed. (define sum-of-products-of-sum-and-product ; generates a recursive process and (lambda (n) ; splits randomly (into k and n-k) (if (= n 1) 0 (let ((k (+ 1 (random (- n 1))))) (+ (* (+ k (- n k)) (* k (- n k))) (sum-of-products-of-sum-and-product k) (sum-of-products-of-sum-and-product (- n k)))))))