{"id":254248,"date":"2025-07-12T12:27:56","date_gmt":"2025-07-12T12:27:56","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=254248"},"modified":"2025-07-12T12:27:59","modified_gmt":"2025-07-12T12:27:59","slug":"yang-huis-triangle-pascals-triangle-is-a-special-triangular-arrangement-of-numbers-used-in-many-areas-of-mathematics","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/07\/12\/yang-huis-triangle-pascals-triangle-is-a-special-triangular-arrangement-of-numbers-used-in-many-areas-of-mathematics\/","title":{"rendered":"Yang Hui&#8217;s Triangle (Pascal&#8217;s Triangle) is a special triangular arrangement of numbers used in many areas of mathematics."},"content":{"rendered":"\n<p>Yang Hui&#8217;s Triangle (Pascal&#8217;s Triangle) is a special triangular arrangement of numbers used in many areas of mathematics. The following diagram shows the first 8 rows of the triangle. Each number is the sum of the top two numbers. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 Write a recursive function yanghui(n,m), which returns the value on the position nth row and mth column. In the submission, you should use the test case for (n,m) as (5,3) and (8,5), the results should be respectively 6 and 35. (Don&#8217;t use nested for loop, there will be no credit using previous solution).<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>To solve this problem, you need to create a recursive function <code>yanghui(n, m)<\/code> that returns the value in the nth row and mth column of Yang Hui&#8217;s Triangle (also known as Pascal&#8217;s Triangle).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Idea:<\/h3>\n\n\n\n<p>In Pascal&#8217;s Triangle, each number is the sum of the two numbers directly above it. Specifically, the element at position <code>(n, m)<\/code> (n-th row, m-th column) is the sum of the element at <code>(n-1, m-1)<\/code> and <code>(n-1, m)<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recursive Formula:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The recursive function should return <code>1<\/code> when either <code>m == 1<\/code> or <code>m == n<\/code> (because the edges of Pascal&#8217;s Triangle are always <code>1<\/code>).<\/li>\n\n\n\n<li>Otherwise, it should return the sum of two elements:\n<ul class=\"wp-block-list\">\n<li>The element above-left: <code>yanghui(n-1, m-1)<\/code><\/li>\n\n\n\n<li>The element above-right: <code>yanghui(n-1, m)<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>This approach ensures that you build up the triangle from the top down.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recursive Function Definition:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopyEdit<code>def yanghui(n, m):\n    # Base case: when m is at the edge of the row (first or last element)\n    if m == 1 or m == n:\n        return 1\n    # Recursive case: calculate sum of two elements above\n    return yanghui(n - 1, m - 1) + yanghui(n - 1, m)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Base Case:<\/strong> If <code>m == 1<\/code> or <code>m == n<\/code>, we return <code>1<\/code> because the first and last elements of each row are always <code>1<\/code> in Pascal&#8217;s Triangle.<\/li>\n\n\n\n<li><strong>Recursive Case:<\/strong> For any other position, the value is the sum of the two values directly above it. Hence, the recursive calls are made to <code>yanghui(n - 1, m - 1)<\/code> and <code>yanghui(n - 1, m)<\/code> to compute the sum.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Test Cases:<\/h3>\n\n\n\n<p>Let&#8217;s use the test cases provided: <code>(5, 3)<\/code> and <code>(8, 5)<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Test Case 1: <code>(5, 3)<\/code><\/h4>\n\n\n\n<p>This corresponds to the 5th row and 3rd column in Pascal&#8217;s Triangle:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yamlCopyEdit<code>Row 5: 1  4  6  4  1\n       Index:  1  2  3  4  5\n<\/code><\/pre>\n\n\n\n<p>For <code>(5, 3)<\/code>, we want the 3rd element in the 5th row, which is <code>6<\/code>. The recursive calls would work as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>yanghui(5, 3)<\/code> is <code>yanghui(4, 2) + yanghui(4, 3)<\/code><\/li>\n\n\n\n<li><code>yanghui(4, 2)<\/code> is <code>yanghui(3, 1) + yanghui(3, 2)<\/code><\/li>\n\n\n\n<li><code>yanghui(3, 1)<\/code> is <code>1<\/code> (base case)<\/li>\n\n\n\n<li><code>yanghui(3, 2)<\/code> is <code>2<\/code> (recursive case)<\/li>\n\n\n\n<li><code>yanghui(4, 3)<\/code> is <code>yanghui(3, 2) + yanghui(3, 3)<\/code><\/li>\n\n\n\n<li><code>yanghui(3, 3)<\/code> is <code>1<\/code> (base case)<\/li>\n<\/ul>\n\n\n\n<p>This results in <code>6<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Test Case 2: <code>(8, 5)<\/code><\/h4>\n\n\n\n<p>This corresponds to the 8th row and 5th column:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yamlCopyEdit<code>Row 8: 1  7  21  35  35  21  7  1\n       Index:  1  2  3  4  5  6  7  8\n<\/code><\/pre>\n\n\n\n<p>For <code>(8, 5)<\/code>, we want the 5th element in the 8th row, which is <code>35<\/code>. The recursive calls would work similarly, eventually summing the values that give <code>35<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recursion Example for <code>(5, 3)<\/code>:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Call <code>yanghui(5, 3)<\/code>\n<ul class=\"wp-block-list\">\n<li>Calls <code>yanghui(4, 2)<\/code> and <code>yanghui(4, 3)<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Call <code>yanghui(4, 2)<\/code>\n<ul class=\"wp-block-list\">\n<li>Calls <code>yanghui(3, 1)<\/code> and <code>yanghui(3, 2)<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Call <code>yanghui(3, 1)<\/code> returns 1 (base case)<\/li>\n\n\n\n<li>Call <code>yanghui(3, 2)<\/code>\n<ul class=\"wp-block-list\">\n<li>Calls <code>yanghui(2, 1)<\/code> and <code>yanghui(2, 2)<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Call <code>yanghui(2, 1)<\/code> returns 1 (base case)<\/li>\n\n\n\n<li>Call <code>yanghui(2, 2)<\/code> returns 1 (base case)<\/li>\n<\/ol>\n\n\n\n<p>Eventually, the result is <code>6<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p>This recursive function efficiently calculates any element in Pascal&#8217;s Triangle by breaking down the problem into smaller subproblems, based on the triangle&#8217;s recursive nature. The base case ensures proper stopping conditions, and the recursive calls allow the function to build the values up row by row<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner6-375.jpeg\" alt=\"\" class=\"wp-image-254255\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Yang Hui&#8217;s Triangle (Pascal&#8217;s Triangle) is a special triangular arrangement of numbers used in many areas of mathematics. The following diagram shows the first 8 rows of the triangle. Each number is the sum of the top two numbers. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-254248","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/254248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=254248"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/254248\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=254248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=254248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=254248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}