Summation of 'for loop' with conditional? Announcing the arrival of Valued Associate #679:...
Is multiple magic items in one inherently imbalanced?
Tannaka duality for semisimple groups
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
The Nth Gryphon Number
What order were files/directories output in dir?
Relating to the President and obstruction, were Mueller's conclusions preordained?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
Why datecode is SO IMPORTANT to chip manufacturers?
Why shouldn't this prove the Prime Number Theorem?
How can god fight other gods?
Did Mueller's report provide an evidentiary basis for the claim of Russian govt election interference via social media?
Is it dangerous to install hacking tools on my private linux machine?
Most effective melee weapons for arboreal combat? (pre-gunpowder technology)
Constant factor of an array
I got rid of Mac OSX and replaced it with linux but now I can't change it back to OSX or windows
Why is a lens darker than other ones when applying the same settings?
How to ask rejected full-time candidates to apply to teach individual courses?
Where is the Next Backup Size entry on iOS 12?
Co-worker has annoying ringtone
Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?
What is a more techy Technical Writer job title that isn't cutesy or confusing?
What does 丫 mean? 丫是什么意思?
What does it mean that physics no longer uses mechanical models to describe phenomena?
How much damage would a cupful of neutron star matter do to the Earth?
Summation of 'for loop' with conditional?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Proof for a summation-procedure using the matrix of Eulerian numbers?Solving Summation ExpressionsHelp me understand this algorithm problem.Finding a recurrence for a sumSimplifying $sum_{i=1}^{n-2}i(n-1-i)$Write the following for loop as a double summationSummation with arithmetic seriesCalculating part of a summation function as a constant?Is summation the equivalent of a for loop? (Programming)Conditional summation with or without complex roots
$begingroup$
I am trying to convert instances of nested 'for' loops into a summation expression. The current code fragment I have is:
for i = 1 to n:
for j = 1 to n:
if (i*j >= n):
for k = 1 to n:
sum++
endif
Basically, the 'if' conditional is confusing me. I know that the loops prior will be called n^2 times, but the third loop is only called when $i*j >= n$. How would I write the third summation to account for this, and then evaluate the overall loop's time complexity?
summation
$endgroup$
add a comment |
$begingroup$
I am trying to convert instances of nested 'for' loops into a summation expression. The current code fragment I have is:
for i = 1 to n:
for j = 1 to n:
if (i*j >= n):
for k = 1 to n:
sum++
endif
Basically, the 'if' conditional is confusing me. I know that the loops prior will be called n^2 times, but the third loop is only called when $i*j >= n$. How would I write the third summation to account for this, and then evaluate the overall loop's time complexity?
summation
$endgroup$
$begingroup$
This is often context dependent. If, say, you wanted to iterate over the entries $a_{ij}$ of an $n times n$ matrix, then the first two loops in your code sample could be adequately expressed as $$sum_{ij geq n} a_{ij}$$. Perhaps if you provided more context about why you have this problem we will be able to provide more helpful answers.
$endgroup$
– Brian
Mar 26 at 1:18
$begingroup$
You want to convert the nested loops into a summation, but then you say you want to evaluate the time complexity of your code, which does not require you to convert the loops into a summation, and, in fact, does not require you to find what valuesum
has after the loops.
$endgroup$
– Fabio Somenzi
Mar 26 at 3:15
add a comment |
$begingroup$
I am trying to convert instances of nested 'for' loops into a summation expression. The current code fragment I have is:
for i = 1 to n:
for j = 1 to n:
if (i*j >= n):
for k = 1 to n:
sum++
endif
Basically, the 'if' conditional is confusing me. I know that the loops prior will be called n^2 times, but the third loop is only called when $i*j >= n$. How would I write the third summation to account for this, and then evaluate the overall loop's time complexity?
summation
$endgroup$
I am trying to convert instances of nested 'for' loops into a summation expression. The current code fragment I have is:
for i = 1 to n:
for j = 1 to n:
if (i*j >= n):
for k = 1 to n:
sum++
endif
Basically, the 'if' conditional is confusing me. I know that the loops prior will be called n^2 times, but the third loop is only called when $i*j >= n$. How would I write the third summation to account for this, and then evaluate the overall loop's time complexity?
summation
summation
asked Mar 26 at 0:59
bpryanbpryan
83
83
$begingroup$
This is often context dependent. If, say, you wanted to iterate over the entries $a_{ij}$ of an $n times n$ matrix, then the first two loops in your code sample could be adequately expressed as $$sum_{ij geq n} a_{ij}$$. Perhaps if you provided more context about why you have this problem we will be able to provide more helpful answers.
$endgroup$
– Brian
Mar 26 at 1:18
$begingroup$
You want to convert the nested loops into a summation, but then you say you want to evaluate the time complexity of your code, which does not require you to convert the loops into a summation, and, in fact, does not require you to find what valuesum
has after the loops.
$endgroup$
– Fabio Somenzi
Mar 26 at 3:15
add a comment |
$begingroup$
This is often context dependent. If, say, you wanted to iterate over the entries $a_{ij}$ of an $n times n$ matrix, then the first two loops in your code sample could be adequately expressed as $$sum_{ij geq n} a_{ij}$$. Perhaps if you provided more context about why you have this problem we will be able to provide more helpful answers.
$endgroup$
– Brian
Mar 26 at 1:18
$begingroup$
You want to convert the nested loops into a summation, but then you say you want to evaluate the time complexity of your code, which does not require you to convert the loops into a summation, and, in fact, does not require you to find what valuesum
has after the loops.
$endgroup$
– Fabio Somenzi
Mar 26 at 3:15
$begingroup$
This is often context dependent. If, say, you wanted to iterate over the entries $a_{ij}$ of an $n times n$ matrix, then the first two loops in your code sample could be adequately expressed as $$sum_{ij geq n} a_{ij}$$. Perhaps if you provided more context about why you have this problem we will be able to provide more helpful answers.
$endgroup$
– Brian
Mar 26 at 1:18
$begingroup$
This is often context dependent. If, say, you wanted to iterate over the entries $a_{ij}$ of an $n times n$ matrix, then the first two loops in your code sample could be adequately expressed as $$sum_{ij geq n} a_{ij}$$. Perhaps if you provided more context about why you have this problem we will be able to provide more helpful answers.
$endgroup$
– Brian
Mar 26 at 1:18
$begingroup$
You want to convert the nested loops into a summation, but then you say you want to evaluate the time complexity of your code, which does not require you to convert the loops into a summation, and, in fact, does not require you to find what value
sum
has after the loops.$endgroup$
– Fabio Somenzi
Mar 26 at 3:15
$begingroup$
You want to convert the nested loops into a summation, but then you say you want to evaluate the time complexity of your code, which does not require you to convert the loops into a summation, and, in fact, does not require you to find what value
sum
has after the loops.$endgroup$
– Fabio Somenzi
Mar 26 at 3:15
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
Hint:
The "for" cycle in $k$ is very easy to turn into something simpler...
As for the other parts, see if you can split the problem into easier steps. For example, what happens for $i=1$? And what happens for $i=2$? And $i=3$? And...
$endgroup$
add a comment |
$begingroup$
We obtain
begin{align*}
color{blue}{sum_{i=1}^n}&color{blue}{sum_{j=1}^n[i jgeq n]sum_{k=1}^n1}tag{1}\
&=nsum_{i=1}^nsum_{j=1}^n[i jgeq n]\
&=nleft(sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor}atop{imid n}}^n1+sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor+1}atop{inot mid n}}^n1right)tag{2}\
&=nleft(sum_{i=1}^nsum_{j=leftlfloor n/i rightrfloor+1}^n1+sum_{{i=1}atop{imid n}}^n1right)\
&=nleft(sum_{i=1}^nleft(n-leftlfloorfrac{n}{i}rightrfloorright)+tau(n)right)tag{3}\
&=nleft(n^2-sum_{i=1}^nleftlfloorfrac{n}{i}rightrfloor+tau(n)right)\
&,,color{blue}{=nleft(n^2-sum_{i=1}^{n-1}tau(i)right)}tag{4}\
&,,color{blue}{sim n^3-n^2left(log n+2gamma-1right)+Oleft(n^{3/2}right)}tag{5}
end{align*}
Comment:
In (1) we use Iverson brackets to represent the condition $ijgeq n$.
In (2) we rewrite the expression using the floor function. Note the $pm 1$ technicality regarding the range of summation.
In (3) we simplify the inner sum of the left-hand term and use the $tau$-function which counts the number of divisors.
In (4) we use an identity stated in A006218 to get rid of the floor function.
In (5) we use an asymptotic expansion which is also stated in OEIS/A006218.
$endgroup$
add a comment |
$begingroup$
At the basic level, for loops are also rewritable as conditionals. Anyways, on to the meat of the problem. The conditional can be removed if you replace 1 in the previous loop with ceil(n/i)
. So it can be rewritten as:
for i = 1 to n:
for j = ceil(n/i) to n:
for k = 1 to n:
sum++
Then the inner 2 loops compress to:
sum+=n*(n-ceil(n/i)+1)
Which then gets called n times, but the sum can be wriiten as:
$$nsum_{i=1}^{n}(n-lceilfrac{n}{i}rceil+1)$$
As to the time complexity of the code as wriiten..., it'll take, okay I don't quite know that part.
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
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
},
noCode: 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%2fmath.stackexchange.com%2fquestions%2f3162547%2fsummation-of-for-loop-with-conditional%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Hint:
The "for" cycle in $k$ is very easy to turn into something simpler...
As for the other parts, see if you can split the problem into easier steps. For example, what happens for $i=1$? And what happens for $i=2$? And $i=3$? And...
$endgroup$
add a comment |
$begingroup$
Hint:
The "for" cycle in $k$ is very easy to turn into something simpler...
As for the other parts, see if you can split the problem into easier steps. For example, what happens for $i=1$? And what happens for $i=2$? And $i=3$? And...
$endgroup$
add a comment |
$begingroup$
Hint:
The "for" cycle in $k$ is very easy to turn into something simpler...
As for the other parts, see if you can split the problem into easier steps. For example, what happens for $i=1$? And what happens for $i=2$? And $i=3$? And...
$endgroup$
Hint:
The "for" cycle in $k$ is very easy to turn into something simpler...
As for the other parts, see if you can split the problem into easier steps. For example, what happens for $i=1$? And what happens for $i=2$? And $i=3$? And...
answered Mar 26 at 1:18
ErtxiemErtxiem
941212
941212
add a comment |
add a comment |
$begingroup$
We obtain
begin{align*}
color{blue}{sum_{i=1}^n}&color{blue}{sum_{j=1}^n[i jgeq n]sum_{k=1}^n1}tag{1}\
&=nsum_{i=1}^nsum_{j=1}^n[i jgeq n]\
&=nleft(sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor}atop{imid n}}^n1+sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor+1}atop{inot mid n}}^n1right)tag{2}\
&=nleft(sum_{i=1}^nsum_{j=leftlfloor n/i rightrfloor+1}^n1+sum_{{i=1}atop{imid n}}^n1right)\
&=nleft(sum_{i=1}^nleft(n-leftlfloorfrac{n}{i}rightrfloorright)+tau(n)right)tag{3}\
&=nleft(n^2-sum_{i=1}^nleftlfloorfrac{n}{i}rightrfloor+tau(n)right)\
&,,color{blue}{=nleft(n^2-sum_{i=1}^{n-1}tau(i)right)}tag{4}\
&,,color{blue}{sim n^3-n^2left(log n+2gamma-1right)+Oleft(n^{3/2}right)}tag{5}
end{align*}
Comment:
In (1) we use Iverson brackets to represent the condition $ijgeq n$.
In (2) we rewrite the expression using the floor function. Note the $pm 1$ technicality regarding the range of summation.
In (3) we simplify the inner sum of the left-hand term and use the $tau$-function which counts the number of divisors.
In (4) we use an identity stated in A006218 to get rid of the floor function.
In (5) we use an asymptotic expansion which is also stated in OEIS/A006218.
$endgroup$
add a comment |
$begingroup$
We obtain
begin{align*}
color{blue}{sum_{i=1}^n}&color{blue}{sum_{j=1}^n[i jgeq n]sum_{k=1}^n1}tag{1}\
&=nsum_{i=1}^nsum_{j=1}^n[i jgeq n]\
&=nleft(sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor}atop{imid n}}^n1+sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor+1}atop{inot mid n}}^n1right)tag{2}\
&=nleft(sum_{i=1}^nsum_{j=leftlfloor n/i rightrfloor+1}^n1+sum_{{i=1}atop{imid n}}^n1right)\
&=nleft(sum_{i=1}^nleft(n-leftlfloorfrac{n}{i}rightrfloorright)+tau(n)right)tag{3}\
&=nleft(n^2-sum_{i=1}^nleftlfloorfrac{n}{i}rightrfloor+tau(n)right)\
&,,color{blue}{=nleft(n^2-sum_{i=1}^{n-1}tau(i)right)}tag{4}\
&,,color{blue}{sim n^3-n^2left(log n+2gamma-1right)+Oleft(n^{3/2}right)}tag{5}
end{align*}
Comment:
In (1) we use Iverson brackets to represent the condition $ijgeq n$.
In (2) we rewrite the expression using the floor function. Note the $pm 1$ technicality regarding the range of summation.
In (3) we simplify the inner sum of the left-hand term and use the $tau$-function which counts the number of divisors.
In (4) we use an identity stated in A006218 to get rid of the floor function.
In (5) we use an asymptotic expansion which is also stated in OEIS/A006218.
$endgroup$
add a comment |
$begingroup$
We obtain
begin{align*}
color{blue}{sum_{i=1}^n}&color{blue}{sum_{j=1}^n[i jgeq n]sum_{k=1}^n1}tag{1}\
&=nsum_{i=1}^nsum_{j=1}^n[i jgeq n]\
&=nleft(sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor}atop{imid n}}^n1+sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor+1}atop{inot mid n}}^n1right)tag{2}\
&=nleft(sum_{i=1}^nsum_{j=leftlfloor n/i rightrfloor+1}^n1+sum_{{i=1}atop{imid n}}^n1right)\
&=nleft(sum_{i=1}^nleft(n-leftlfloorfrac{n}{i}rightrfloorright)+tau(n)right)tag{3}\
&=nleft(n^2-sum_{i=1}^nleftlfloorfrac{n}{i}rightrfloor+tau(n)right)\
&,,color{blue}{=nleft(n^2-sum_{i=1}^{n-1}tau(i)right)}tag{4}\
&,,color{blue}{sim n^3-n^2left(log n+2gamma-1right)+Oleft(n^{3/2}right)}tag{5}
end{align*}
Comment:
In (1) we use Iverson brackets to represent the condition $ijgeq n$.
In (2) we rewrite the expression using the floor function. Note the $pm 1$ technicality regarding the range of summation.
In (3) we simplify the inner sum of the left-hand term and use the $tau$-function which counts the number of divisors.
In (4) we use an identity stated in A006218 to get rid of the floor function.
In (5) we use an asymptotic expansion which is also stated in OEIS/A006218.
$endgroup$
We obtain
begin{align*}
color{blue}{sum_{i=1}^n}&color{blue}{sum_{j=1}^n[i jgeq n]sum_{k=1}^n1}tag{1}\
&=nsum_{i=1}^nsum_{j=1}^n[i jgeq n]\
&=nleft(sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor}atop{imid n}}^n1+sum_{i=1}^nsum_{{j=leftlfloor n/i rightrfloor+1}atop{inot mid n}}^n1right)tag{2}\
&=nleft(sum_{i=1}^nsum_{j=leftlfloor n/i rightrfloor+1}^n1+sum_{{i=1}atop{imid n}}^n1right)\
&=nleft(sum_{i=1}^nleft(n-leftlfloorfrac{n}{i}rightrfloorright)+tau(n)right)tag{3}\
&=nleft(n^2-sum_{i=1}^nleftlfloorfrac{n}{i}rightrfloor+tau(n)right)\
&,,color{blue}{=nleft(n^2-sum_{i=1}^{n-1}tau(i)right)}tag{4}\
&,,color{blue}{sim n^3-n^2left(log n+2gamma-1right)+Oleft(n^{3/2}right)}tag{5}
end{align*}
Comment:
In (1) we use Iverson brackets to represent the condition $ijgeq n$.
In (2) we rewrite the expression using the floor function. Note the $pm 1$ technicality regarding the range of summation.
In (3) we simplify the inner sum of the left-hand term and use the $tau$-function which counts the number of divisors.
In (4) we use an identity stated in A006218 to get rid of the floor function.
In (5) we use an asymptotic expansion which is also stated in OEIS/A006218.
answered Mar 26 at 12:58
Markus ScheuerMarkus Scheuer
64.7k460154
64.7k460154
add a comment |
add a comment |
$begingroup$
At the basic level, for loops are also rewritable as conditionals. Anyways, on to the meat of the problem. The conditional can be removed if you replace 1 in the previous loop with ceil(n/i)
. So it can be rewritten as:
for i = 1 to n:
for j = ceil(n/i) to n:
for k = 1 to n:
sum++
Then the inner 2 loops compress to:
sum+=n*(n-ceil(n/i)+1)
Which then gets called n times, but the sum can be wriiten as:
$$nsum_{i=1}^{n}(n-lceilfrac{n}{i}rceil+1)$$
As to the time complexity of the code as wriiten..., it'll take, okay I don't quite know that part.
$endgroup$
add a comment |
$begingroup$
At the basic level, for loops are also rewritable as conditionals. Anyways, on to the meat of the problem. The conditional can be removed if you replace 1 in the previous loop with ceil(n/i)
. So it can be rewritten as:
for i = 1 to n:
for j = ceil(n/i) to n:
for k = 1 to n:
sum++
Then the inner 2 loops compress to:
sum+=n*(n-ceil(n/i)+1)
Which then gets called n times, but the sum can be wriiten as:
$$nsum_{i=1}^{n}(n-lceilfrac{n}{i}rceil+1)$$
As to the time complexity of the code as wriiten..., it'll take, okay I don't quite know that part.
$endgroup$
add a comment |
$begingroup$
At the basic level, for loops are also rewritable as conditionals. Anyways, on to the meat of the problem. The conditional can be removed if you replace 1 in the previous loop with ceil(n/i)
. So it can be rewritten as:
for i = 1 to n:
for j = ceil(n/i) to n:
for k = 1 to n:
sum++
Then the inner 2 loops compress to:
sum+=n*(n-ceil(n/i)+1)
Which then gets called n times, but the sum can be wriiten as:
$$nsum_{i=1}^{n}(n-lceilfrac{n}{i}rceil+1)$$
As to the time complexity of the code as wriiten..., it'll take, okay I don't quite know that part.
$endgroup$
At the basic level, for loops are also rewritable as conditionals. Anyways, on to the meat of the problem. The conditional can be removed if you replace 1 in the previous loop with ceil(n/i)
. So it can be rewritten as:
for i = 1 to n:
for j = ceil(n/i) to n:
for k = 1 to n:
sum++
Then the inner 2 loops compress to:
sum+=n*(n-ceil(n/i)+1)
Which then gets called n times, but the sum can be wriiten as:
$$nsum_{i=1}^{n}(n-lceilfrac{n}{i}rceil+1)$$
As to the time complexity of the code as wriiten..., it'll take, okay I don't quite know that part.
answered Mar 27 at 16:25
Roddy MacPheeRoddy MacPhee
927118
927118
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fmath.stackexchange.com%2fquestions%2f3162547%2fsummation-of-for-loop-with-conditional%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
$begingroup$
This is often context dependent. If, say, you wanted to iterate over the entries $a_{ij}$ of an $n times n$ matrix, then the first two loops in your code sample could be adequately expressed as $$sum_{ij geq n} a_{ij}$$. Perhaps if you provided more context about why you have this problem we will be able to provide more helpful answers.
$endgroup$
– Brian
Mar 26 at 1:18
$begingroup$
You want to convert the nested loops into a summation, but then you say you want to evaluate the time complexity of your code, which does not require you to convert the loops into a summation, and, in fact, does not require you to find what value
sum
has after the loops.$endgroup$
– Fabio Somenzi
Mar 26 at 3:15