What is wrong with this procedural function to add numbers 1 through n? [on hold]












3












$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?










share|improve this question







New contributor




Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB 11 hours ago


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 use Print[x], you can use just x instead.
    $endgroup$
    – kglr
    22 hours ago












  • $begingroup$
    Thanks alot. Why were the parenthesis necessary?
    $endgroup$
    – Jaigus
    22 hours ago






  • 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
    22 hours ago












  • $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
    22 hours ago


















3












$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?










share|improve this question







New contributor




Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB 11 hours ago


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 use Print[x], you can use just x instead.
    $endgroup$
    – kglr
    22 hours ago












  • $begingroup$
    Thanks alot. Why were the parenthesis necessary?
    $endgroup$
    – Jaigus
    22 hours ago






  • 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
    22 hours ago












  • $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
    22 hours ago
















3












3








3





$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?










share|improve this question







New contributor




Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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






share|improve this question







New contributor




Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 22 hours ago









JaigusJaigus

1333




1333




New contributor




Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jaigus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB 11 hours ago


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.







put on hold as off-topic by m_goldberg, Henrik Schumacher, Lukas Lang, Carl Lange, MarcoB 11 hours ago


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 use Print[x], you can use just x instead.
    $endgroup$
    – kglr
    22 hours ago












  • $begingroup$
    Thanks alot. Why were the parenthesis necessary?
    $endgroup$
    – Jaigus
    22 hours ago






  • 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
    22 hours ago












  • $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
    22 hours ago
















  • 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
    22 hours ago












  • $begingroup$
    Thanks alot. Why were the parenthesis necessary?
    $endgroup$
    – Jaigus
    22 hours ago






  • 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
    22 hours ago












  • $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
    22 hours ago










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
22 hours ago






$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
22 hours ago














$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
22 hours ago




$begingroup$
Thanks alot. Why were the parenthesis necessary?
$endgroup$
– Jaigus
22 hours ago




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
22 hours ago






$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
22 hours ago














$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
22 hours ago






$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
22 hours ago












2 Answers
2






active

oldest

votes


















5












$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







share|improve this answer











$endgroup$









  • 1




    $begingroup$
    Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
    $endgroup$
    – Andreas Rejbrand
    20 hours ago





















6












$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.






share|improve this answer











$endgroup$













  • $begingroup$
    Might be worth to mention PolygonalNumber as built-in solution
    $endgroup$
    – Lukas Lang
    17 hours ago


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5












$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







share|improve this answer











$endgroup$









  • 1




    $begingroup$
    Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
    $endgroup$
    – Andreas Rejbrand
    20 hours ago


















5












$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







share|improve this answer











$endgroup$









  • 1




    $begingroup$
    Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
    $endgroup$
    – Andreas Rejbrand
    20 hours ago
















5












5








5





$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







share|improve this answer











$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








share|improve this answer














share|improve this answer



share|improve this answer








edited 21 hours ago

























answered 22 hours ago









kglrkglr

182k10200415




182k10200415








  • 1




    $begingroup$
    Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
    $endgroup$
    – Andreas Rejbrand
    20 hours ago
















  • 1




    $begingroup$
    Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
    $endgroup$
    – Andreas Rejbrand
    20 hours ago










1




1




$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
20 hours ago






$begingroup$
Indeed, here we employ (a special case of) the formula for the sum of an arithmetic series.
$endgroup$
– Andreas Rejbrand
20 hours ago













6












$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.






share|improve this answer











$endgroup$













  • $begingroup$
    Might be worth to mention PolygonalNumber as built-in solution
    $endgroup$
    – Lukas Lang
    17 hours ago
















6












$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.






share|improve this answer











$endgroup$













  • $begingroup$
    Might be worth to mention PolygonalNumber as built-in solution
    $endgroup$
    – Lukas Lang
    17 hours ago














6












6








6





$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.






share|improve this answer











$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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 21 hours ago

























answered 21 hours ago









m_goldbergm_goldberg

85.9k872196




85.9k872196












  • $begingroup$
    Might be worth to mention PolygonalNumber as built-in solution
    $endgroup$
    – Lukas Lang
    17 hours ago


















  • $begingroup$
    Might be worth to mention PolygonalNumber as built-in solution
    $endgroup$
    – Lukas Lang
    17 hours ago
















$begingroup$
Might be worth to mention PolygonalNumber as built-in solution
$endgroup$
– Lukas Lang
17 hours ago




$begingroup$
Might be worth to mention PolygonalNumber as built-in solution
$endgroup$
– Lukas Lang
17 hours ago



Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy