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












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









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






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


















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









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






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
















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









$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













share|improve this question











share|improve this question




share|improve this question










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






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




    $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






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












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
    Feb 8 at 9:00





















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
    Feb 8 at 11:14


















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
    Feb 8 at 9:00


















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
    Feb 8 at 9:00
















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
















  • 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













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
    Feb 8 at 11:14
















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
    Feb 8 at 11:14














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 Feb 8 at 7:45

























answered Feb 8 at 7:16









m_goldbergm_goldberg

88k872199




88k872199












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
















$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



Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

File:DeusFollowingSea.jpg