Assignment 4 - Solutions - NAME SID Assignment 4: Recursion...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
NAME SECTION SID February 16, 2007 Assignment 4: Recursion 100 pts for problems -5 pts for formatting errors (-1 for improper header, -1 for no title, -1 for no page numbers, -1 for no problem headings, -1 for incorrect file name or file format) Problem 1: Basic Recursion (30) 1.1 + + = = . . ) 1 , , ( ) 1 ( 1 ) , , ( w o n d a myArithSum d n a n if a n d a myArithSum +1 function [out] = myArithSum(a,d,n) % NAME % DATE % myArithSum: calculates the sum of the first n terms of the arithmatic % sequence with parameters a and d. % inputs: parameters a and d, and number of terms to be summed n. if eq(n,1) +1 out = a; +1 else out = a + d*(n-1) + myArithSum(a,d,n-1); +1 end end EDU>> myArithSum(1,2,1) ans = 1 +1 EDU>> myArithSum(1,2,3) ans = 9 +1 EDU>> myArithSum(1,2,10) ans = 100 +1 1.2 function [out] = myGeometricSeries(a,r,n) % NAME % DATE % myGeometricSeries: returns the first n terms in the geometric series with % parameters a and r. % inputs: parameters a and r, and number of recursions n
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
NAME SECTION SID February 16, 2007 % outputs: out, and array containing all the terms of the series. if eq(n,1) +1 out = a; +1 else g=myGeometricSeries(a,r,n-1); out = [g, g(n-1) + a*r^(n-1)]; +3 end end >> out=myGeometricSeries(1,2,5) out = 1 3 7 15 31 +3 1.3 function [GCD] = myGCD(u,v) % NAME % DATE % myGCD: returns the greatest common denominator of u and v % inputs: positive integers u and v % outputs: greatest common deniminator if and(and(and(eq(round(u),u),eq(round(v),v)),and(ge(u,0),ge(v,0))),or(gt(u,0),gt(v,0))) if and(eq(u,0),ne(v,0)) GCD = v; elseif and(ne(u,0),eq(v,0)); GCD = u; elseif and(eq(mod(u,2),0), eq(mod(v,2),0)) GCD = 2*myGCD(u/2,v/2); elseif and(eq(mod(u,2),0), ne(mod(v,2),0)) GCD = myGCD(u/2,v); elseif and(ne(mod(u,2),0), eq(mod(v,2),0)) GCD = myGCD(u,v/2); elseif and(and(ne(mod(u,2),0), ne(mod(v,2),0)), ge(u,v)) GCD = myGCD((u-v)/2, v); elseif and(and(ne(mod(u,2),0), ne(mod(v,2),0)), gt(v,u)) GCD = myGCD(u, (v-u)/2); elseif eq(u,v) GCD = u; end else disp( 'input not in domain' ) GCD=[]; end end + 3 EDU>> myGCD(21272873, 38561879) ans = 6977 +2
Background image of page 2
NAME SECTION SID February 16, 2007 1.4 function [] = myChange(price,paid) % NAME % DATE % myChange: counts back change % input: rationals price and paid % outputs: none paid = round(paid*100)/100;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/05/2009 for the course ENGLISH 77 taught by Professor Sengupta during the Spring '07 term at University of California, Berkeley.

Page1 / 11

Assignment 4 - Solutions - NAME SID Assignment 4: Recursion...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online