{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs32w11dis4

cs32w11dis4 - CS32 Introduction to Computer Science II...

This preview shows pages 1–2. Sign up to view the full content.

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi ([email protected] ) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Recursion A recursion is a function-writing technique where the function refers to itself . Assuming you have learned the concept of recursion sufficiently in the lecture, we’ll focus on the tricks in writing a recursive function. The Way to Think About Recursion Writing a recursive function involves the division of a problem into subproblems. A good example is the factorial function that computes n! = n x (n-1) x (n-2) x . .. x 2 x 1 for some arbitrary n 0 . Here is the iterative way of writing factorial : Iterative functions are more intuitive than recursive functions because we can see the whole process -- you see how factorial goes from 1 to n and multiply them up in that for loop. When writing the recursive version of the same function, you need not worry about “going from 1 to n .” Instead, we only worry about “going from n-1 to n ,” believing factorial(n-1) will return the right value . In factorial ’s case, if you know the value of factorial(n-1) , it is very easy to compute factorial(n) -- we only need to multiply n to the value of factorial(n-1) (because n * factorial(n-1) = n * {(n-1)*(n-2)*. ..*2*1} = factorial(n) ). This is what’s meant by “going from n-1 to n .” Translating this into a C++ code, we get the following function: My factorial function on the left simply assumes that factorial(n-1) returns what we want, and computes the value of factorial(n) based on this assumption. When factorial(n-1) is called, then it makes the same assumption and uses the value of factorial(n-2) to compute its value, . .. , and so on. For example, if factorial(5) is called, it relies on the value factorial(4) returns. When factorial(4) is called, it relies on the value factorial(3) returns. When factorial(3) is called, it relies on the value factorial(2)

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

cs32w11dis4 - CS32 Introduction to Computer Science II...

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

View Full Document
Ask a homework question - tutors are online
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.coursehero.com/file/6802460/cs32w11dis4/", "fetch_time": 1519463188000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2018-09/segments/1518891815500.61/warc/CC-MAIN-20180224073111-20180224093111-00697.warc.gz", "warc_record_offset": 819413216, "warc_record_length": 135529, "token_count": 627, "char_count": 2516, "score": 4.5, "int_score": 4, "crawl": "CC-MAIN-2018-09", "snapshot_type": "latest", "language": "en", "language_score": 0.836948037147522, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00037-of-00064.parquet"}