;; CS101, Spring 2013 ;; Lab 4 ;; Writing recursive functions over numbers. (display "\n CS101 Lab 4, Spring 2013\n") (display "\n PLEASE WRITE YOUR NAME HERE\n\n") (newline) (newline) (display "Problem 1: Computing the sum of squares from 1 to n.\n\n") ; ; Problem 1: Function SUM-SQUARES ; ; Write a function that consumes a positive natural number and ; returns the sum of squares as follows: 1^2 + 2^2 + ... + n^2 ; ; Think about the smallest possible value in this equation for any ; given value of n, a positive natural number. That number will be ; the base case. ; ; The recursive case will be run for every value of n greater than ; the base case. ; ; Begin by writing a contract, header, purpose, and pre-function ; test suite for function sum-squares. Then write the function and ; test it on different inputs in printf statements. ; ; Use regular numeric recursion in your solution. ; ; Contract: ; Header: ; Purpose: ; Pre-function tests: ; Function definition: ; Post-function printf's: (newline) (newline) (display "Problem 2: Computing the geometric sum from 0 to n.\n\n") ; ; PROBLEM 2: Function GEOM-SUM ; ; A geometric sum has the following form: ; ; 1 + x + x^2 + x^3 + ... + x^n ; ; Recall from math classes that x^0 = 1 and x^1 = x. If you ; try to number the expressions in the equation above, you ; start with an exponent of 0 and end with an exponent of n. ; ; Define a recursive function, GEOM-SUM, that computes sums of ; the form shown above. ; ; Since you are writing a recursive function in which n is the ; recursive parameter, you should choose to write the function ; such that n gets closer to 0 on each recursive call. ; ; This function should consume 2 parameters, the value of x and ; the value of n. ; ; Follow the design recipe when writing this function: ; Write the contract, header, purpose, pre-function tests ; (check-within statements), function definition, and post- ; function printf's. ; ; Use regular numeric recursion in your solution. ; ; Contract: ; Header: ; Purpose: ; Pre-function tests: ; Function definition: ; Post-function printf's: (newline) (newline) (display "Problem 3: Computing an alternating series.\n\n") ; ; Problem 3: Function SUM-ALT-SERIES ; ; The alternating series looks like this: ; ; 1 - 1/2 + 1/3 - 1/4 + 1/5 - ... ; ; Mathematicians tell us that the more terms you include, the ; closer the sum gets to the value ln(2) (i.e., the natural ; logarithm of 2), which is about 0.693. ; ; For this problem, define a RECURSIVE function, called ; SUM-ALT-SERIES, that takes a single input, N. You may ; assume that N is a positive integer. The output of ; SUM-ALT-SERIES should be a sum of the form: ; ; 1 - 1/2 + 1/3 - 1/4 + 1/5 - ... (+/-)1/N ; ; For example, if N is 3, then the output should be the ; result of evaluating: ; ; 1 - 1/2 + 1/3 ; ; which equals approximately 0.833333333333. ; ; Since this function produces a real number for some values ; of N, you should use the CHECK-WITHIN pre-function tester ; that gives the expected result followed by the allowable ; error. ; ; What is the base case for this function? ; ; ==> Notice that the sign of the Nth term depends on the ; value of N, specifically whether N is even or odd. ; This implies that the recursive clause in this ; function will either itself contain a decision ; statement OR you will write 2 recursive clauses. ; ; ==> You can use the built-in odd? or even? function in your ; solution. ; ; ==> When your function runs, try it out on large values ; of N, say 1000. Compare the output in test expressions ; to (log 2), Racket's built-in LOG function, called with ; argument 2 to compute the natural logarithm of 2. As ; the argument to sum-alt-series gets larger, the value ; returned should get closer to (log 2). Use the exact-> ; inexact function in your printfs for large values of n. ; ; Use regular numeric recursion in your solution. ; ; Contract: ; Header: ; Purpose: ; Pre-function tests: ; Function definition: ; Post-function printf's: (newline) (newline)