How would you create n nested loops for math?
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 some random math
return number;
}
This will present the expected output of:
5000
So to readdress the problem, how can I code to allow this for n dimensions? Thanks for your help!
c# loops nested nested-loops
add a comment |
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 some random math
return number;
}
This will present the expected output of:
5000
So to readdress the problem, how can I code to allow this for n dimensions? Thanks for your help!
c# loops nested nested-loops
1
Let Recursion be your friend
– Michael Randall
Nov 23 '18 at 8:48
Yeah, I've thought about recursion, but I cannot figure out how I would do it.
– TimeTravelPenguin
Nov 23 '18 at 8:49
How important is the "number += h + i + j + k" line, because that's really the only thing that makes it slightly tricky (you'd need an array to hold all the counters instead)?
– Dylan Nicholson
Nov 23 '18 at 8:56
This is just filler code for now, I want to do more complex math at a later date
– TimeTravelPenguin
Nov 23 '18 at 9:03
add a comment |
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 some random math
return number;
}
This will present the expected output of:
5000
So to readdress the problem, how can I code to allow this for n dimensions? Thanks for your help!
c# loops nested nested-loops
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 some random math
return number;
}
This will present the expected output of:
5000
So to readdress the problem, how can I code to allow this for n dimensions? Thanks for your help!
c# loops nested nested-loops
c# loops nested nested-loops
edited Nov 23 '18 at 9:22
Dmitry Bychenko
111k1099141
111k1099141
asked Nov 23 '18 at 8:46
TimeTravelPenguinTimeTravelPenguin
287
287
1
Let Recursion be your friend
– Michael Randall
Nov 23 '18 at 8:48
Yeah, I've thought about recursion, but I cannot figure out how I would do it.
– TimeTravelPenguin
Nov 23 '18 at 8:49
How important is the "number += h + i + j + k" line, because that's really the only thing that makes it slightly tricky (you'd need an array to hold all the counters instead)?
– Dylan Nicholson
Nov 23 '18 at 8:56
This is just filler code for now, I want to do more complex math at a later date
– TimeTravelPenguin
Nov 23 '18 at 9:03
add a comment |
1
Let Recursion be your friend
– Michael Randall
Nov 23 '18 at 8:48
Yeah, I've thought about recursion, but I cannot figure out how I would do it.
– TimeTravelPenguin
Nov 23 '18 at 8:49
How important is the "number += h + i + j + k" line, because that's really the only thing that makes it slightly tricky (you'd need an array to hold all the counters instead)?
– Dylan Nicholson
Nov 23 '18 at 8:56
This is just filler code for now, I want to do more complex math at a later date
– TimeTravelPenguin
Nov 23 '18 at 9:03
1
1
Let Recursion be your friend
– Michael Randall
Nov 23 '18 at 8:48
Let Recursion be your friend
– Michael Randall
Nov 23 '18 at 8:48
Yeah, I've thought about recursion, but I cannot figure out how I would do it.
– TimeTravelPenguin
Nov 23 '18 at 8:49
Yeah, I've thought about recursion, but I cannot figure out how I would do it.
– TimeTravelPenguin
Nov 23 '18 at 8:49
How important is the "number += h + i + j + k" line, because that's really the only thing that makes it slightly tricky (you'd need an array to hold all the counters instead)?
– Dylan Nicholson
Nov 23 '18 at 8:56
How important is the "number += h + i + j + k" line, because that's really the only thing that makes it slightly tricky (you'd need an array to hold all the counters instead)?
– Dylan Nicholson
Nov 23 '18 at 8:56
This is just filler code for now, I want to do more complex math at a later date
– TimeTravelPenguin
Nov 23 '18 at 9:03
This is just filler code for now, I want to do more complex math at a later date
– TimeTravelPenguin
Nov 23 '18 at 9:03
add a comment |
1 Answer
1
active
oldest
votes
For arbitrary n
dimensions you can loop with a help of array int address
which represents n
dimensions:
static long addNumbers(int dimensions) {
int address = new int[dimensions];
// size of each dimension; not necessary equals to dimensions
// + 1 : in your code, int the loops you have i <= dimensions, j <= dimensions etc.
int size = dimensions + 1;
long number = 0;
do {
//TODO: some math here
// i == address[0]; j = address[1]; ... etc.
number += address.Sum();
// next address: adding 1 to array
for (int i = 0; i < address.Length; ++i) {
if (address[i] >= size - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
}
}
while (!address.All(index => index == 0)); // all 0 address - items're exhausted
return number;
}
Finally, let's add some Linq to look at the results:
int upTo = 5;
string report = string.Join(Environment.NewLine, Enumerable
.Range(1, upTo)
.Select(i => $"{i} -> {addNumbers(i),6}"));
Console.Write(report);
Outcome:
1 -> 1
2 -> 18
3 -> 288
4 -> 5000 // <- We've got it: 5000 for 4 dimensions
5 -> 97200
Thanks! This seems to be it!
– TimeTravelPenguin
Nov 23 '18 at 9:02
Using array for addressing is a typical way out in case of arbitrary n dimension space, array, collection etc.: stackoverflow.com/questions/53431650/…
– Dmitry Bychenko
Nov 23 '18 at 9:04
Yeah, I just fixed the post! I didn't need the loop to print the console, my mistake!
– TimeTravelPenguin
Nov 23 '18 at 9:19
1
@TimeTravelPenguin: Thank you! I see; I've added Linq for debugging purpose: sincen = 1
case solution (32
) was very suspicious
– Dmitry Bychenko
Nov 23 '18 at 9:26
I've not learnt Linq yet, so it's something new to learn!
– TimeTravelPenguin
Nov 23 '18 at 9:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443274%2fhow-would-you-create-n-nested-loops-for-math%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For arbitrary n
dimensions you can loop with a help of array int address
which represents n
dimensions:
static long addNumbers(int dimensions) {
int address = new int[dimensions];
// size of each dimension; not necessary equals to dimensions
// + 1 : in your code, int the loops you have i <= dimensions, j <= dimensions etc.
int size = dimensions + 1;
long number = 0;
do {
//TODO: some math here
// i == address[0]; j = address[1]; ... etc.
number += address.Sum();
// next address: adding 1 to array
for (int i = 0; i < address.Length; ++i) {
if (address[i] >= size - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
}
}
while (!address.All(index => index == 0)); // all 0 address - items're exhausted
return number;
}
Finally, let's add some Linq to look at the results:
int upTo = 5;
string report = string.Join(Environment.NewLine, Enumerable
.Range(1, upTo)
.Select(i => $"{i} -> {addNumbers(i),6}"));
Console.Write(report);
Outcome:
1 -> 1
2 -> 18
3 -> 288
4 -> 5000 // <- We've got it: 5000 for 4 dimensions
5 -> 97200
Thanks! This seems to be it!
– TimeTravelPenguin
Nov 23 '18 at 9:02
Using array for addressing is a typical way out in case of arbitrary n dimension space, array, collection etc.: stackoverflow.com/questions/53431650/…
– Dmitry Bychenko
Nov 23 '18 at 9:04
Yeah, I just fixed the post! I didn't need the loop to print the console, my mistake!
– TimeTravelPenguin
Nov 23 '18 at 9:19
1
@TimeTravelPenguin: Thank you! I see; I've added Linq for debugging purpose: sincen = 1
case solution (32
) was very suspicious
– Dmitry Bychenko
Nov 23 '18 at 9:26
I've not learnt Linq yet, so it's something new to learn!
– TimeTravelPenguin
Nov 23 '18 at 9:29
add a comment |
For arbitrary n
dimensions you can loop with a help of array int address
which represents n
dimensions:
static long addNumbers(int dimensions) {
int address = new int[dimensions];
// size of each dimension; not necessary equals to dimensions
// + 1 : in your code, int the loops you have i <= dimensions, j <= dimensions etc.
int size = dimensions + 1;
long number = 0;
do {
//TODO: some math here
// i == address[0]; j = address[1]; ... etc.
number += address.Sum();
// next address: adding 1 to array
for (int i = 0; i < address.Length; ++i) {
if (address[i] >= size - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
}
}
while (!address.All(index => index == 0)); // all 0 address - items're exhausted
return number;
}
Finally, let's add some Linq to look at the results:
int upTo = 5;
string report = string.Join(Environment.NewLine, Enumerable
.Range(1, upTo)
.Select(i => $"{i} -> {addNumbers(i),6}"));
Console.Write(report);
Outcome:
1 -> 1
2 -> 18
3 -> 288
4 -> 5000 // <- We've got it: 5000 for 4 dimensions
5 -> 97200
Thanks! This seems to be it!
– TimeTravelPenguin
Nov 23 '18 at 9:02
Using array for addressing is a typical way out in case of arbitrary n dimension space, array, collection etc.: stackoverflow.com/questions/53431650/…
– Dmitry Bychenko
Nov 23 '18 at 9:04
Yeah, I just fixed the post! I didn't need the loop to print the console, my mistake!
– TimeTravelPenguin
Nov 23 '18 at 9:19
1
@TimeTravelPenguin: Thank you! I see; I've added Linq for debugging purpose: sincen = 1
case solution (32
) was very suspicious
– Dmitry Bychenko
Nov 23 '18 at 9:26
I've not learnt Linq yet, so it's something new to learn!
– TimeTravelPenguin
Nov 23 '18 at 9:29
add a comment |
For arbitrary n
dimensions you can loop with a help of array int address
which represents n
dimensions:
static long addNumbers(int dimensions) {
int address = new int[dimensions];
// size of each dimension; not necessary equals to dimensions
// + 1 : in your code, int the loops you have i <= dimensions, j <= dimensions etc.
int size = dimensions + 1;
long number = 0;
do {
//TODO: some math here
// i == address[0]; j = address[1]; ... etc.
number += address.Sum();
// next address: adding 1 to array
for (int i = 0; i < address.Length; ++i) {
if (address[i] >= size - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
}
}
while (!address.All(index => index == 0)); // all 0 address - items're exhausted
return number;
}
Finally, let's add some Linq to look at the results:
int upTo = 5;
string report = string.Join(Environment.NewLine, Enumerable
.Range(1, upTo)
.Select(i => $"{i} -> {addNumbers(i),6}"));
Console.Write(report);
Outcome:
1 -> 1
2 -> 18
3 -> 288
4 -> 5000 // <- We've got it: 5000 for 4 dimensions
5 -> 97200
For arbitrary n
dimensions you can loop with a help of array int address
which represents n
dimensions:
static long addNumbers(int dimensions) {
int address = new int[dimensions];
// size of each dimension; not necessary equals to dimensions
// + 1 : in your code, int the loops you have i <= dimensions, j <= dimensions etc.
int size = dimensions + 1;
long number = 0;
do {
//TODO: some math here
// i == address[0]; j = address[1]; ... etc.
number += address.Sum();
// next address: adding 1 to array
for (int i = 0; i < address.Length; ++i) {
if (address[i] >= size - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
}
}
while (!address.All(index => index == 0)); // all 0 address - items're exhausted
return number;
}
Finally, let's add some Linq to look at the results:
int upTo = 5;
string report = string.Join(Environment.NewLine, Enumerable
.Range(1, upTo)
.Select(i => $"{i} -> {addNumbers(i),6}"));
Console.Write(report);
Outcome:
1 -> 1
2 -> 18
3 -> 288
4 -> 5000 // <- We've got it: 5000 for 4 dimensions
5 -> 97200
edited Nov 23 '18 at 9:21
answered Nov 23 '18 at 8:56
Dmitry BychenkoDmitry Bychenko
111k1099141
111k1099141
Thanks! This seems to be it!
– TimeTravelPenguin
Nov 23 '18 at 9:02
Using array for addressing is a typical way out in case of arbitrary n dimension space, array, collection etc.: stackoverflow.com/questions/53431650/…
– Dmitry Bychenko
Nov 23 '18 at 9:04
Yeah, I just fixed the post! I didn't need the loop to print the console, my mistake!
– TimeTravelPenguin
Nov 23 '18 at 9:19
1
@TimeTravelPenguin: Thank you! I see; I've added Linq for debugging purpose: sincen = 1
case solution (32
) was very suspicious
– Dmitry Bychenko
Nov 23 '18 at 9:26
I've not learnt Linq yet, so it's something new to learn!
– TimeTravelPenguin
Nov 23 '18 at 9:29
add a comment |
Thanks! This seems to be it!
– TimeTravelPenguin
Nov 23 '18 at 9:02
Using array for addressing is a typical way out in case of arbitrary n dimension space, array, collection etc.: stackoverflow.com/questions/53431650/…
– Dmitry Bychenko
Nov 23 '18 at 9:04
Yeah, I just fixed the post! I didn't need the loop to print the console, my mistake!
– TimeTravelPenguin
Nov 23 '18 at 9:19
1
@TimeTravelPenguin: Thank you! I see; I've added Linq for debugging purpose: sincen = 1
case solution (32
) was very suspicious
– Dmitry Bychenko
Nov 23 '18 at 9:26
I've not learnt Linq yet, so it's something new to learn!
– TimeTravelPenguin
Nov 23 '18 at 9:29
Thanks! This seems to be it!
– TimeTravelPenguin
Nov 23 '18 at 9:02
Thanks! This seems to be it!
– TimeTravelPenguin
Nov 23 '18 at 9:02
Using array for addressing is a typical way out in case of arbitrary n dimension space, array, collection etc.: stackoverflow.com/questions/53431650/…
– Dmitry Bychenko
Nov 23 '18 at 9:04
Using array for addressing is a typical way out in case of arbitrary n dimension space, array, collection etc.: stackoverflow.com/questions/53431650/…
– Dmitry Bychenko
Nov 23 '18 at 9:04
Yeah, I just fixed the post! I didn't need the loop to print the console, my mistake!
– TimeTravelPenguin
Nov 23 '18 at 9:19
Yeah, I just fixed the post! I didn't need the loop to print the console, my mistake!
– TimeTravelPenguin
Nov 23 '18 at 9:19
1
1
@TimeTravelPenguin: Thank you! I see; I've added Linq for debugging purpose: since
n = 1
case solution (32
) was very suspicious– Dmitry Bychenko
Nov 23 '18 at 9:26
@TimeTravelPenguin: Thank you! I see; I've added Linq for debugging purpose: since
n = 1
case solution (32
) was very suspicious– Dmitry Bychenko
Nov 23 '18 at 9:26
I've not learnt Linq yet, so it's something new to learn!
– TimeTravelPenguin
Nov 23 '18 at 9:29
I've not learnt Linq yet, so it's something new to learn!
– TimeTravelPenguin
Nov 23 '18 at 9:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443274%2fhow-would-you-create-n-nested-loops-for-math%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Let Recursion be your friend
– Michael Randall
Nov 23 '18 at 8:48
Yeah, I've thought about recursion, but I cannot figure out how I would do it.
– TimeTravelPenguin
Nov 23 '18 at 8:49
How important is the "number += h + i + j + k" line, because that's really the only thing that makes it slightly tricky (you'd need an array to hold all the counters instead)?
– Dylan Nicholson
Nov 23 '18 at 8:56
This is just filler code for now, I want to do more complex math at a later date
– TimeTravelPenguin
Nov 23 '18 at 9:03