What is wrong with this procedural function to add numbers 1 through n? [closed]
$begingroup$
I am trying to write a function to add numbers from 1 through h:
function[h_]:= x=0; For[i=1, i=<h, i++, x = x + i]; Print[x]
But I am getting some strange and inconsistent results. Can someone point out what is wrong here?
functions function-construction
$endgroup$
closed as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB Feb 8 at 18:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
I am trying to write a function to add numbers from 1 through h:
function[h_]:= x=0; For[i=1, i=<h, i++, x = x + i]; Print[x]
But I am getting some strange and inconsistent results. Can someone point out what is wrong here?
functions function-construction
$endgroup$
closed as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB Feb 8 at 18:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.
1
$begingroup$
put the right-hand side in parantheses: i.e.,function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
. Btw, you don't need to usePrint[x]
, you can use justx
instead.
$endgroup$
– kglr
Feb 8 at 6:41
$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
Feb 8 at 6:46
1
$begingroup$
without the parentheses, you are definingfunction
asfunction[h_] := x = 0;
and the remaining parts are executed as independent expressions: partFor[i = 1, i <= h, i++, x = x + i]
does not do anything tox
(becauseh
is not given a value) andPrint[x]
is executed separately and prints0
.
$endgroup$
– kglr
Feb 8 at 6:55
$begingroup$
Aahhhh ok. Thanks for making this clear. If you write this as an answer, I'd be happy to give you credit for it?
$endgroup$
– Jaigus
Feb 8 at 6:57
add a comment |
$begingroup$
I am trying to write a function to add numbers from 1 through h:
function[h_]:= x=0; For[i=1, i=<h, i++, x = x + i]; Print[x]
But I am getting some strange and inconsistent results. Can someone point out what is wrong here?
functions function-construction
$endgroup$
I am trying to write a function to add numbers from 1 through h:
function[h_]:= x=0; For[i=1, i=<h, i++, x = x + i]; Print[x]
But I am getting some strange and inconsistent results. Can someone point out what is wrong here?
functions function-construction
functions function-construction
asked Feb 8 at 6:37
JaigusJaigus
1775
1775
closed as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB Feb 8 at 18:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB Feb 8 at 18:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.
1
$begingroup$
put the right-hand side in parantheses: i.e.,function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
. Btw, you don't need to usePrint[x]
, you can use justx
instead.
$endgroup$
– kglr
Feb 8 at 6:41
$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
Feb 8 at 6:46
1
$begingroup$
without the parentheses, you are definingfunction
asfunction[h_] := x = 0;
and the remaining parts are executed as independent expressions: partFor[i = 1, i <= h, i++, x = x + i]
does not do anything tox
(becauseh
is not given a value) andPrint[x]
is executed separately and prints0
.
$endgroup$
– kglr
Feb 8 at 6:55
$begingroup$
Aahhhh ok. Thanks for making this clear. If you write this as an answer, I'd be happy to give you credit for it?
$endgroup$
– Jaigus
Feb 8 at 6:57
add a comment |
1
$begingroup$
put the right-hand side in parantheses: i.e.,function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
. Btw, you don't need to usePrint[x]
, you can use justx
instead.
$endgroup$
– kglr
Feb 8 at 6:41
$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
Feb 8 at 6:46
1
$begingroup$
without the parentheses, you are definingfunction
asfunction[h_] := x = 0;
and the remaining parts are executed as independent expressions: partFor[i = 1, i <= h, i++, x = x + i]
does not do anything tox
(becauseh
is not given a value) andPrint[x]
is executed separately and prints0
.
$endgroup$
– kglr
Feb 8 at 6:55
$begingroup$
Aahhhh ok. Thanks for making this clear. If you write this as an answer, I'd be happy to give you credit for it?
$endgroup$
– Jaigus
Feb 8 at 6:57
1
1
$begingroup$
put the right-hand side in parantheses: i.e.,
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
. Btw, you don't need to use Print[x]
, you can use just x
instead.$endgroup$
– kglr
Feb 8 at 6:41
$begingroup$
put the right-hand side in parantheses: i.e.,
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
. Btw, you don't need to use Print[x]
, you can use just x
instead.$endgroup$
– kglr
Feb 8 at 6:41
$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
Feb 8 at 6:46
$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
Feb 8 at 6:46
1
1
$begingroup$
without the parentheses, you are defining
function
as function[h_] := x = 0;
and the remaining parts are executed as independent expressions: part For[i = 1, i <= h, i++, x = x + i]
does not do anything to x
(because h
is not given a value) and Print[x]
is executed separately and prints 0
.$endgroup$
– kglr
Feb 8 at 6:55
$begingroup$
without the parentheses, you are defining
function
as function[h_] := x = 0;
and the remaining parts are executed as independent expressions: part For[i = 1, i <= h, i++, x = x + i]
does not do anything to x
(because h
is not given a value) and Print[x]
is executed separately and prints 0
.$endgroup$
– kglr
Feb 8 at 6:55
$begingroup$
Aahhhh ok. Thanks for making this clear. If you write this as an answer, I'd be happy to give you credit for it?
$endgroup$
– Jaigus
Feb 8 at 6:57
$begingroup$
Aahhhh ok. Thanks for making this clear. If you write this as an answer, I'd be happy to give you credit for it?
$endgroup$
– Jaigus
Feb 8 at 6:57
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Put the expressions on right-hand-side in parentheses:
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
function[10]
55
Without the parentheses, you are defining function
as function[h_] := x = 0;
and the remaining expressions are not part of the definition of function
.
As mentioned by m_goldberg, there are better ways to define such a function. In addition to the ones in m_goldberg's answer, you can also use
ClearAll[function]
function[h_]:= h (h + 1) / 2
function[10]
55
$endgroup$
1
$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
Feb 8 at 9:00
add a comment |
$begingroup$
This is not an answer within the constraints of you question, but I think you should be made aware that there are much better ways.
A simple and functional way to write your function in the Wolfram Language would be
function[h_] := Total @ Range[h]
Then
function[10]
returns (and prints)
>
55`
This version of function
is not only more concise than your procedural code, it is many times faster.
Of course, the built-in function Sum
is even more concise.
Sum[x, {x, 10}]
55
But Sum
works symbolically, so it be used to define an extremely efficient version of function
.
Block[{x, h}, function[h_] = Sum[x, {x, h}]];
This gives the definition
Definition @ function
function[h_] = 1/2 h (1 + h)
which is about as good as you can get.
$endgroup$
$begingroup$
Might be worth to mentionPolygonalNumber
as built-in solution
$endgroup$
– Lukas Lang
Feb 8 at 11:14
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Put the expressions on right-hand-side in parentheses:
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
function[10]
55
Without the parentheses, you are defining function
as function[h_] := x = 0;
and the remaining expressions are not part of the definition of function
.
As mentioned by m_goldberg, there are better ways to define such a function. In addition to the ones in m_goldberg's answer, you can also use
ClearAll[function]
function[h_]:= h (h + 1) / 2
function[10]
55
$endgroup$
1
$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
Feb 8 at 9:00
add a comment |
$begingroup$
Put the expressions on right-hand-side in parentheses:
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
function[10]
55
Without the parentheses, you are defining function
as function[h_] := x = 0;
and the remaining expressions are not part of the definition of function
.
As mentioned by m_goldberg, there are better ways to define such a function. In addition to the ones in m_goldberg's answer, you can also use
ClearAll[function]
function[h_]:= h (h + 1) / 2
function[10]
55
$endgroup$
1
$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
Feb 8 at 9:00
add a comment |
$begingroup$
Put the expressions on right-hand-side in parentheses:
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
function[10]
55
Without the parentheses, you are defining function
as function[h_] := x = 0;
and the remaining expressions are not part of the definition of function
.
As mentioned by m_goldberg, there are better ways to define such a function. In addition to the ones in m_goldberg's answer, you can also use
ClearAll[function]
function[h_]:= h (h + 1) / 2
function[10]
55
$endgroup$
Put the expressions on right-hand-side in parentheses:
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
function[10]
55
Without the parentheses, you are defining function
as function[h_] := x = 0;
and the remaining expressions are not part of the definition of function
.
As mentioned by m_goldberg, there are better ways to define such a function. In addition to the ones in m_goldberg's answer, you can also use
ClearAll[function]
function[h_]:= h (h + 1) / 2
function[10]
55
edited Feb 8 at 7:42
answered Feb 8 at 7:10
kglrkglr
190k10206425
190k10206425
1
$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
Feb 8 at 9:00
add a comment |
1
$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
Feb 8 at 9:00
1
1
$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
Feb 8 at 9:00
$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
Feb 8 at 9:00
add a comment |
$begingroup$
This is not an answer within the constraints of you question, but I think you should be made aware that there are much better ways.
A simple and functional way to write your function in the Wolfram Language would be
function[h_] := Total @ Range[h]
Then
function[10]
returns (and prints)
>
55`
This version of function
is not only more concise than your procedural code, it is many times faster.
Of course, the built-in function Sum
is even more concise.
Sum[x, {x, 10}]
55
But Sum
works symbolically, so it be used to define an extremely efficient version of function
.
Block[{x, h}, function[h_] = Sum[x, {x, h}]];
This gives the definition
Definition @ function
function[h_] = 1/2 h (1 + h)
which is about as good as you can get.
$endgroup$
$begingroup$
Might be worth to mentionPolygonalNumber
as built-in solution
$endgroup$
– Lukas Lang
Feb 8 at 11:14
add a comment |
$begingroup$
This is not an answer within the constraints of you question, but I think you should be made aware that there are much better ways.
A simple and functional way to write your function in the Wolfram Language would be
function[h_] := Total @ Range[h]
Then
function[10]
returns (and prints)
>
55`
This version of function
is not only more concise than your procedural code, it is many times faster.
Of course, the built-in function Sum
is even more concise.
Sum[x, {x, 10}]
55
But Sum
works symbolically, so it be used to define an extremely efficient version of function
.
Block[{x, h}, function[h_] = Sum[x, {x, h}]];
This gives the definition
Definition @ function
function[h_] = 1/2 h (1 + h)
which is about as good as you can get.
$endgroup$
$begingroup$
Might be worth to mentionPolygonalNumber
as built-in solution
$endgroup$
– Lukas Lang
Feb 8 at 11:14
add a comment |
$begingroup$
This is not an answer within the constraints of you question, but I think you should be made aware that there are much better ways.
A simple and functional way to write your function in the Wolfram Language would be
function[h_] := Total @ Range[h]
Then
function[10]
returns (and prints)
>
55`
This version of function
is not only more concise than your procedural code, it is many times faster.
Of course, the built-in function Sum
is even more concise.
Sum[x, {x, 10}]
55
But Sum
works symbolically, so it be used to define an extremely efficient version of function
.
Block[{x, h}, function[h_] = Sum[x, {x, h}]];
This gives the definition
Definition @ function
function[h_] = 1/2 h (1 + h)
which is about as good as you can get.
$endgroup$
This is not an answer within the constraints of you question, but I think you should be made aware that there are much better ways.
A simple and functional way to write your function in the Wolfram Language would be
function[h_] := Total @ Range[h]
Then
function[10]
returns (and prints)
>
55`
This version of function
is not only more concise than your procedural code, it is many times faster.
Of course, the built-in function Sum
is even more concise.
Sum[x, {x, 10}]
55
But Sum
works symbolically, so it be used to define an extremely efficient version of function
.
Block[{x, h}, function[h_] = Sum[x, {x, h}]];
This gives the definition
Definition @ function
function[h_] = 1/2 h (1 + h)
which is about as good as you can get.
edited Feb 8 at 7:45
answered Feb 8 at 7:16
m_goldbergm_goldberg
88k872199
88k872199
$begingroup$
Might be worth to mentionPolygonalNumber
as built-in solution
$endgroup$
– Lukas Lang
Feb 8 at 11:14
add a comment |
$begingroup$
Might be worth to mentionPolygonalNumber
as built-in solution
$endgroup$
– Lukas Lang
Feb 8 at 11:14
$begingroup$
Might be worth to mention
PolygonalNumber
as built-in solution$endgroup$
– Lukas Lang
Feb 8 at 11:14
$begingroup$
Might be worth to mention
PolygonalNumber
as built-in solution$endgroup$
– Lukas Lang
Feb 8 at 11:14
add a comment |
1
$begingroup$
put the right-hand side in parantheses: i.e.,
function[h_] := (x = 0; For[i = 1, i <= h, i++, x = x + i]; Print[x])
. Btw, you don't need to usePrint[x]
, you can use justx
instead.$endgroup$
– kglr
Feb 8 at 6:41
$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
Feb 8 at 6:46
1
$begingroup$
without the parentheses, you are defining
function
asfunction[h_] := x = 0;
and the remaining parts are executed as independent expressions: partFor[i = 1, i <= h, i++, x = x + i]
does not do anything tox
(becauseh
is not given a value) andPrint[x]
is executed separately and prints0
.$endgroup$
– kglr
Feb 8 at 6:55
$begingroup$
Aahhhh ok. Thanks for making this clear. If you write this as an answer, I'd be happy to give you credit for it?
$endgroup$
– Jaigus
Feb 8 at 6:57