For Loop and Sum
Clash Royale CLAN TAG#URR8PPP
$begingroup$
I am requested to do a sum of the first fifty positive even numbers by using For
loop.
The For
loop that I created is
For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]
The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created
Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]]
However, it is not a correct input. I am not sure if I should write the Sum
inside the For
loop.
homework
$endgroup$
add a comment |
$begingroup$
I am requested to do a sum of the first fifty positive even numbers by using For
loop.
The For
loop that I created is
For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]
The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created
Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]]
However, it is not a correct input. I am not sure if I should write the Sum
inside the For
loop.
homework
$endgroup$
5
$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class.For
loops are almost never a good way to do anything in Mathematica.
$endgroup$
– John Doty
Mar 3 at 20:32
$begingroup$
If you want the first 50 positive even numbers, I believe that is 2, 4, ..., 100. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
Mar 3 at 22:06
1
$begingroup$
Total@Range[2, 2*50, 2]
would be pretty direct. Note also thatPrint
prints its arguments, but does not return them (in fact, it returnsNull
).
$endgroup$
– MarcoB
Mar 3 at 22:57
7
$begingroup$
Since the days of Carl Friedrich Gauß we know that using aFor
loop to compute50 * 51
is a pretty bad idea....
$endgroup$
– Henrik Schumacher
Mar 3 at 23:51
$begingroup$
Is it the first fifty that you want, or is it 2, 4, ..., 50? The original question had both.
$endgroup$
– mjw
Mar 4 at 2:56
add a comment |
$begingroup$
I am requested to do a sum of the first fifty positive even numbers by using For
loop.
The For
loop that I created is
For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]
The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created
Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]]
However, it is not a correct input. I am not sure if I should write the Sum
inside the For
loop.
homework
$endgroup$
I am requested to do a sum of the first fifty positive even numbers by using For
loop.
The For
loop that I created is
For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]
The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created
Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]]
However, it is not a correct input. I am not sure if I should write the Sum
inside the For
loop.
homework
homework
edited Mar 3 at 22:51
MarcoB
38.2k556114
38.2k556114
asked Mar 3 at 20:11
Laura FigueroaLaura Figueroa
161
161
5
$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class.For
loops are almost never a good way to do anything in Mathematica.
$endgroup$
– John Doty
Mar 3 at 20:32
$begingroup$
If you want the first 50 positive even numbers, I believe that is 2, 4, ..., 100. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
Mar 3 at 22:06
1
$begingroup$
Total@Range[2, 2*50, 2]
would be pretty direct. Note also thatPrint
prints its arguments, but does not return them (in fact, it returnsNull
).
$endgroup$
– MarcoB
Mar 3 at 22:57
7
$begingroup$
Since the days of Carl Friedrich Gauß we know that using aFor
loop to compute50 * 51
is a pretty bad idea....
$endgroup$
– Henrik Schumacher
Mar 3 at 23:51
$begingroup$
Is it the first fifty that you want, or is it 2, 4, ..., 50? The original question had both.
$endgroup$
– mjw
Mar 4 at 2:56
add a comment |
5
$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class.For
loops are almost never a good way to do anything in Mathematica.
$endgroup$
– John Doty
Mar 3 at 20:32
$begingroup$
If you want the first 50 positive even numbers, I believe that is 2, 4, ..., 100. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
Mar 3 at 22:06
1
$begingroup$
Total@Range[2, 2*50, 2]
would be pretty direct. Note also thatPrint
prints its arguments, but does not return them (in fact, it returnsNull
).
$endgroup$
– MarcoB
Mar 3 at 22:57
7
$begingroup$
Since the days of Carl Friedrich Gauß we know that using aFor
loop to compute50 * 51
is a pretty bad idea....
$endgroup$
– Henrik Schumacher
Mar 3 at 23:51
$begingroup$
Is it the first fifty that you want, or is it 2, 4, ..., 50? The original question had both.
$endgroup$
– mjw
Mar 4 at 2:56
5
5
$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class.
For
loops are almost never a good way to do anything in Mathematica.$endgroup$
– John Doty
Mar 3 at 20:32
$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class.
For
loops are almost never a good way to do anything in Mathematica.$endgroup$
– John Doty
Mar 3 at 20:32
$begingroup$
If you want the first 50 positive even numbers, I believe that is 2, 4, ..., 100. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
Mar 3 at 22:06
$begingroup$
If you want the first 50 positive even numbers, I believe that is 2, 4, ..., 100. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
Mar 3 at 22:06
1
1
$begingroup$
Total@Range[2, 2*50, 2]
would be pretty direct. Note also that Print
prints its arguments, but does not return them (in fact, it returns Null
).$endgroup$
– MarcoB
Mar 3 at 22:57
$begingroup$
Total@Range[2, 2*50, 2]
would be pretty direct. Note also that Print
prints its arguments, but does not return them (in fact, it returns Null
).$endgroup$
– MarcoB
Mar 3 at 22:57
7
7
$begingroup$
Since the days of Carl Friedrich Gauß we know that using a
For
loop to compute 50 * 51
is a pretty bad idea....$endgroup$
– Henrik Schumacher
Mar 3 at 23:51
$begingroup$
Since the days of Carl Friedrich Gauß we know that using a
For
loop to compute 50 * 51
is a pretty bad idea....$endgroup$
– Henrik Schumacher
Mar 3 at 23:51
$begingroup$
Is it the first fifty that you want, or is it 2, 4, ..., 50? The original question had both.
$endgroup$
– mjw
Mar 4 at 2:56
$begingroup$
Is it the first fifty that you want, or is it 2, 4, ..., 50? The original question had both.
$endgroup$
– mjw
Mar 4 at 2:56
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Anyone asking you to write For
loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:
rslt = 0;
For[i = 0, i <= 50, i += 2, rslt += i];
rslt
And here's how someone with some familiarity with Mathematica might write it
Plus @@ Range[25]*2
Now, spend the time you were going to waste writing a For
loop by reading the answers to this question Why should I avoid the For loop in Mathematica?
$endgroup$
3
$begingroup$
+1 for the first sentence.
$endgroup$
– Henrik Schumacher
Mar 3 at 23:49
add a comment |
$begingroup$
The first fifty positive integers can be computed with Table
:
Q = 0; (* Initialization *)
T = Table[Q + k, k, 2 Range[50]]
We can sum up the values of T
with Sum
:
Sum[T[[k]], k, Range[50]]
2550
$endgroup$
1
$begingroup$
Perhaps better / more idiomatic than yourSum
approach would be usingTotal[T]
orPlus@@T
.
$endgroup$
– MarcoB
Mar 3 at 22:56
$begingroup$
Thank you! I was looking for something likeTotal
and I saw thePlus
posting above, but haven't yet learned about@@
and such. I would have thoughtMap
might work here (it does not). Anyway, yesTotal[T]
andPlus@@T
are simpler and more elegant!
$endgroup$
– mjw
Mar 4 at 0:23
$begingroup$
I was a latecomer to@@
as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have1, 2, 3
, itsFullForm
would beList[1, 2, 3]
. If you thenApply
Plus
to it, i.e.Plus@@
, you swap theList
head for aPlus
head, to givePlus[1, 2, 3]
. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful!
$endgroup$
– MarcoB
Mar 4 at 1:34
$begingroup$
This is great, thank you! Just as I learned not to useFor
loops (especially on thisFor
um), and toDo
useDo
, I'll look for opportunities toApply
@@
.
$endgroup$
– mjw
Mar 4 at 2:23
add a comment |
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fmathematica.stackexchange.com%2fquestions%2f192541%2ffor-loop-and-sum%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Anyone asking you to write For
loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:
rslt = 0;
For[i = 0, i <= 50, i += 2, rslt += i];
rslt
And here's how someone with some familiarity with Mathematica might write it
Plus @@ Range[25]*2
Now, spend the time you were going to waste writing a For
loop by reading the answers to this question Why should I avoid the For loop in Mathematica?
$endgroup$
3
$begingroup$
+1 for the first sentence.
$endgroup$
– Henrik Schumacher
Mar 3 at 23:49
add a comment |
$begingroup$
Anyone asking you to write For
loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:
rslt = 0;
For[i = 0, i <= 50, i += 2, rslt += i];
rslt
And here's how someone with some familiarity with Mathematica might write it
Plus @@ Range[25]*2
Now, spend the time you were going to waste writing a For
loop by reading the answers to this question Why should I avoid the For loop in Mathematica?
$endgroup$
3
$begingroup$
+1 for the first sentence.
$endgroup$
– Henrik Schumacher
Mar 3 at 23:49
add a comment |
$begingroup$
Anyone asking you to write For
loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:
rslt = 0;
For[i = 0, i <= 50, i += 2, rslt += i];
rslt
And here's how someone with some familiarity with Mathematica might write it
Plus @@ Range[25]*2
Now, spend the time you were going to waste writing a For
loop by reading the answers to this question Why should I avoid the For loop in Mathematica?
$endgroup$
Anyone asking you to write For
loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:
rslt = 0;
For[i = 0, i <= 50, i += 2, rslt += i];
rslt
And here's how someone with some familiarity with Mathematica might write it
Plus @@ Range[25]*2
Now, spend the time you were going to waste writing a For
loop by reading the answers to this question Why should I avoid the For loop in Mathematica?
answered Mar 3 at 20:36
High Performance MarkHigh Performance Mark
636512
636512
3
$begingroup$
+1 for the first sentence.
$endgroup$
– Henrik Schumacher
Mar 3 at 23:49
add a comment |
3
$begingroup$
+1 for the first sentence.
$endgroup$
– Henrik Schumacher
Mar 3 at 23:49
3
3
$begingroup$
+1 for the first sentence.
$endgroup$
– Henrik Schumacher
Mar 3 at 23:49
$begingroup$
+1 for the first sentence.
$endgroup$
– Henrik Schumacher
Mar 3 at 23:49
add a comment |
$begingroup$
The first fifty positive integers can be computed with Table
:
Q = 0; (* Initialization *)
T = Table[Q + k, k, 2 Range[50]]
We can sum up the values of T
with Sum
:
Sum[T[[k]], k, Range[50]]
2550
$endgroup$
1
$begingroup$
Perhaps better / more idiomatic than yourSum
approach would be usingTotal[T]
orPlus@@T
.
$endgroup$
– MarcoB
Mar 3 at 22:56
$begingroup$
Thank you! I was looking for something likeTotal
and I saw thePlus
posting above, but haven't yet learned about@@
and such. I would have thoughtMap
might work here (it does not). Anyway, yesTotal[T]
andPlus@@T
are simpler and more elegant!
$endgroup$
– mjw
Mar 4 at 0:23
$begingroup$
I was a latecomer to@@
as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have1, 2, 3
, itsFullForm
would beList[1, 2, 3]
. If you thenApply
Plus
to it, i.e.Plus@@
, you swap theList
head for aPlus
head, to givePlus[1, 2, 3]
. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful!
$endgroup$
– MarcoB
Mar 4 at 1:34
$begingroup$
This is great, thank you! Just as I learned not to useFor
loops (especially on thisFor
um), and toDo
useDo
, I'll look for opportunities toApply
@@
.
$endgroup$
– mjw
Mar 4 at 2:23
add a comment |
$begingroup$
The first fifty positive integers can be computed with Table
:
Q = 0; (* Initialization *)
T = Table[Q + k, k, 2 Range[50]]
We can sum up the values of T
with Sum
:
Sum[T[[k]], k, Range[50]]
2550
$endgroup$
1
$begingroup$
Perhaps better / more idiomatic than yourSum
approach would be usingTotal[T]
orPlus@@T
.
$endgroup$
– MarcoB
Mar 3 at 22:56
$begingroup$
Thank you! I was looking for something likeTotal
and I saw thePlus
posting above, but haven't yet learned about@@
and such. I would have thoughtMap
might work here (it does not). Anyway, yesTotal[T]
andPlus@@T
are simpler and more elegant!
$endgroup$
– mjw
Mar 4 at 0:23
$begingroup$
I was a latecomer to@@
as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have1, 2, 3
, itsFullForm
would beList[1, 2, 3]
. If you thenApply
Plus
to it, i.e.Plus@@
, you swap theList
head for aPlus
head, to givePlus[1, 2, 3]
. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful!
$endgroup$
– MarcoB
Mar 4 at 1:34
$begingroup$
This is great, thank you! Just as I learned not to useFor
loops (especially on thisFor
um), and toDo
useDo
, I'll look for opportunities toApply
@@
.
$endgroup$
– mjw
Mar 4 at 2:23
add a comment |
$begingroup$
The first fifty positive integers can be computed with Table
:
Q = 0; (* Initialization *)
T = Table[Q + k, k, 2 Range[50]]
We can sum up the values of T
with Sum
:
Sum[T[[k]], k, Range[50]]
2550
$endgroup$
The first fifty positive integers can be computed with Table
:
Q = 0; (* Initialization *)
T = Table[Q + k, k, 2 Range[50]]
We can sum up the values of T
with Sum
:
Sum[T[[k]], k, Range[50]]
2550
edited Mar 4 at 2:52
Conor O'Brien
1034
1034
answered Mar 3 at 22:02
mjwmjw
1,20810
1,20810
1
$begingroup$
Perhaps better / more idiomatic than yourSum
approach would be usingTotal[T]
orPlus@@T
.
$endgroup$
– MarcoB
Mar 3 at 22:56
$begingroup$
Thank you! I was looking for something likeTotal
and I saw thePlus
posting above, but haven't yet learned about@@
and such. I would have thoughtMap
might work here (it does not). Anyway, yesTotal[T]
andPlus@@T
are simpler and more elegant!
$endgroup$
– mjw
Mar 4 at 0:23
$begingroup$
I was a latecomer to@@
as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have1, 2, 3
, itsFullForm
would beList[1, 2, 3]
. If you thenApply
Plus
to it, i.e.Plus@@
, you swap theList
head for aPlus
head, to givePlus[1, 2, 3]
. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful!
$endgroup$
– MarcoB
Mar 4 at 1:34
$begingroup$
This is great, thank you! Just as I learned not to useFor
loops (especially on thisFor
um), and toDo
useDo
, I'll look for opportunities toApply
@@
.
$endgroup$
– mjw
Mar 4 at 2:23
add a comment |
1
$begingroup$
Perhaps better / more idiomatic than yourSum
approach would be usingTotal[T]
orPlus@@T
.
$endgroup$
– MarcoB
Mar 3 at 22:56
$begingroup$
Thank you! I was looking for something likeTotal
and I saw thePlus
posting above, but haven't yet learned about@@
and such. I would have thoughtMap
might work here (it does not). Anyway, yesTotal[T]
andPlus@@T
are simpler and more elegant!
$endgroup$
– mjw
Mar 4 at 0:23
$begingroup$
I was a latecomer to@@
as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have1, 2, 3
, itsFullForm
would beList[1, 2, 3]
. If you thenApply
Plus
to it, i.e.Plus@@
, you swap theList
head for aPlus
head, to givePlus[1, 2, 3]
. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful!
$endgroup$
– MarcoB
Mar 4 at 1:34
$begingroup$
This is great, thank you! Just as I learned not to useFor
loops (especially on thisFor
um), and toDo
useDo
, I'll look for opportunities toApply
@@
.
$endgroup$
– mjw
Mar 4 at 2:23
1
1
$begingroup$
Perhaps better / more idiomatic than your
Sum
approach would be using Total[T]
or Plus@@T
.$endgroup$
– MarcoB
Mar 3 at 22:56
$begingroup$
Perhaps better / more idiomatic than your
Sum
approach would be using Total[T]
or Plus@@T
.$endgroup$
– MarcoB
Mar 3 at 22:56
$begingroup$
Thank you! I was looking for something like
Total
and I saw the Plus
posting above, but haven't yet learned about @@
and such. I would have thought Map
might work here (it does not). Anyway, yes Total[T]
and Plus@@T
are simpler and more elegant!$endgroup$
– mjw
Mar 4 at 0:23
$begingroup$
Thank you! I was looking for something like
Total
and I saw the Plus
posting above, but haven't yet learned about @@
and such. I would have thought Map
might work here (it does not). Anyway, yes Total[T]
and Plus@@T
are simpler and more elegant!$endgroup$
– mjw
Mar 4 at 0:23
$begingroup$
I was a latecomer to
@@
as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have 1, 2, 3
, its FullForm
would be List[1, 2, 3]
. If you then Apply
Plus
to it, i.e. Plus@@
, you swap the List
head for a Plus
head, to give Plus[1, 2, 3]
. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful!$endgroup$
– MarcoB
Mar 4 at 1:34
$begingroup$
I was a latecomer to
@@
as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have 1, 2, 3
, its FullForm
would be List[1, 2, 3]
. If you then Apply
Plus
to it, i.e. Plus@@
, you swap the List
head for a Plus
head, to give Plus[1, 2, 3]
. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful!$endgroup$
– MarcoB
Mar 4 at 1:34
$begingroup$
This is great, thank you! Just as I learned not to use
For
loops (especially on this For
um), and to Do
use Do
, I'll look for opportunities to Apply
@@
.$endgroup$
– mjw
Mar 4 at 2:23
$begingroup$
This is great, thank you! Just as I learned not to use
For
loops (especially on this For
um), and to Do
use Do
, I'll look for opportunities to Apply
@@
.$endgroup$
– mjw
Mar 4 at 2:23
add a comment |
Thanks for contributing an answer to Mathematica 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%2fmathematica.stackexchange.com%2fquestions%2f192541%2ffor-loop-and-sum%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
5
$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class.
For
loops are almost never a good way to do anything in Mathematica.$endgroup$
– John Doty
Mar 3 at 20:32
$begingroup$
If you want the first 50 positive even numbers, I believe that is 2, 4, ..., 100. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
Mar 3 at 22:06
1
$begingroup$
Total@Range[2, 2*50, 2]
would be pretty direct. Note also thatPrint
prints its arguments, but does not return them (in fact, it returnsNull
).$endgroup$
– MarcoB
Mar 3 at 22:57
7
$begingroup$
Since the days of Carl Friedrich Gauß we know that using a
For
loop to compute50 * 51
is a pretty bad idea....$endgroup$
– Henrik Schumacher
Mar 3 at 23:51
$begingroup$
Is it the first fifty that you want, or is it 2, 4, ..., 50? The original question had both.
$endgroup$
– mjw
Mar 4 at 2:56