Using recursion to sum numbers

后端 未结 16 2096
轮回少年
轮回少年 2021-02-05 15:45

I have just been studying the concept of recursion and I thought that I would try a simple example. In the following code, I am attempting to take the numbers: 1, 2, 3, 4, 5, an

相关标签:
16条回答
  • 2021-02-05 16:13

    I'm pretty sure the problem is because you want your recursion to terminate when value == 1, and it's currently terminating when value == 0.

    0 讨论(0)
  • 2021-02-05 16:13
            static int Sum(int[] addends)
        {
            if (addends.Length == 1)
            {
                return addends[0];
            }
            else
            {
                int tailIndex = addends.Length - 1;
                var subArray = addends[0..tailIndex];
                return addends[tailIndex] + Sum(subArray);
            }
    
    
        }
    
    0 讨论(0)
  • 2021-02-05 16:15

    Others already noted the error, and I will elaborate on recursion.

    Although C# does not currently perform tail call optimization (although IL has special tail instruction), it's worth mentioning that tail recursion is generally a good thing.

    Tail recursion is a special case of recursion in which the last operation of the function, the tail call, is a recursive call. Since the last call is the recursive call there is no need to preserve stack frame of the calling function and the compiler can easily use this information to generate machine instruction such that the stack doesn't grow at all. So it can basically turn recursive function into an iterative one.

    Rewriting your code to support tail recursion can be done as follws:

    static int Sum(int result, int value)
    {
        if(value == 0)
            return result;
    
        return Sum(result + 1, value - 1);
    }
    
    0 讨论(0)
  • 2021-02-05 16:16

    Try this code:

    def sumr(n): 
        if n==0:
            return n
        return n+sumr(n-1)
    
    0 讨论(0)
  • 2021-02-05 16:21

    To begin at the end, a recursive Sum method looks like this:

        // version 3
    
        public static int Sum(int startRange, int endRange)
        {
            if (endRange > startRange)
            {
                return endRange + Sum(startRange, endRange - 1);
    
            }
    
            if (endRange < startRange)
            {
                return startRange + Sum(endRange, startRange - 1);
    
            }
    
            return endRange; 
    
        }
    

    Hardcoding the startRange to be 0 gives us:

        // version 2
    
        public static int Sum(int range)
        {
            if (range > 0)
            {
                return range + Sum(0, range - 1);
    
            }
    
            if (range < 0)
            {
                return Sum(range, -1);
    
            }
    
            return range;
    
        }
    

    ...and if you want to limit the method to positive numbers only, there's no need for a sign:

        // version 1
    
        public static unsigned int Sum(unsigned int range)
        {
            if (range > 0)
            {
                return range + Sum(0, range - 1);
    
            }
    
            return range;
    
        }
    

    I hope this helps give more of an insight into summing number ranges via recursion.

    0 讨论(0)
  • 2021-02-05 16:22
    using System;
    using NUnit.Framework;
    
    namespace Recursion
    {
      [TestFixture()]
        public class Test
        {
            [Test()]
            public void TestSum ()
            {
                Assert.AreEqual (Sum (new int[] { }), 0);
                Assert.AreEqual (Sum (new int[] { 0 }), 0);
                Assert.AreEqual (Sum (new int[] { 1 }), 1);
                Assert.AreEqual (Sum (new int[] { 1, 2, 3, 4, 5 }), 15);
            }
    
            public int Sum(int[] head)
            {
                if (head.Length == 0) return 0;
    
                int[] tail = new int[head.Length - 1];
    
                for (int i = 1; i < head.Length; i++) 
                {
                    tail [i-1] = head [i];
                }
    
                return head[0] + Sum (tail);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题