Last active
October 23, 2019 21:02
-
-
Save peterthorsteinson/ee6939b8e91d5268ce88eb53609d4d44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Program | |
{ | |
static void Main() | |
{ | |
int num = 10; | |
Console.WriteLine(AddUpToLoop(num)); | |
Console.WriteLine(AddUpToRecursive(num, 0)); | |
Func<int, int, int> AddUpToLambda = null; | |
AddUpToLambda = (num, sum) => { return num == 0 ? sum : AddUpToLambda(--num, sum + num + 1); }; | |
Console.WriteLine(AddUpToLambda(num, 0)); | |
} | |
static int AddUpToLoop(int num) | |
{ | |
int sum = 0; | |
for (int i = 1; i <= num; i++) | |
{ | |
sum += i; | |
} | |
return sum; | |
} | |
static int AddUpToRecursive(int num, int sum) | |
{ | |
if (num == 0) return sum; | |
sum += num; | |
num--; | |
return AddUpToRecursive(num, sum); | |
} | |
} |
Author
peterthorsteinson
commented
Oct 23, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment