Posts

Showing posts from March 29, 2019

How would you create n nested loops for math?

Image
1 So, I am trying to wrap my head around understanding how you can use a variable to denote how many times a loop is nested . Here is an example I write up to simulate the output of dimensions = 4 : static void Main(string args) { int dimensions = 4; // e.g. for (1, 2, 3, 4), dimensions = 4 Console.WriteLine($"{addNumbers(dimensions)}"); Console.ReadKey(); } static long addNumbers(int dimensions) { long number = 0; // hard coded to be dimensions = 4 for (int h = 0; h <= dimensions; h++) for (int i = 0; i <= dimensions; i++) for (int j = 0; j <= dimensions; j++) for (int k = 0; k <= dimensions; k++) number += h + i + j + k; // just