summaryrefslogtreecommitdiff
path: root/week1/PyramidProblem.cs
blob: 1be67d91db88c747fb193551e3b3027f625cb585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace ALGA {
	public class PyramidProblem {
		public static int triangular_number_recursive(int n) {
			if (n <= 0) return 0;
			return n + triangular_number_recursive(n - 1);
		}

		public static int triangular_number_iterative(int n) {
			if (n <= 0) return 0;
			int total = 0;
			for (int i = 1; i <= n; i++)
				total += i;
			return total;
		}

		public static int triangular_number_function(int n) {
			if (n <= 0) return 0;
			return (n + 1) * n / 2;
		}
	}
}