Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.5K Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Output of a For function

Hi, I was trying to create a function that contains a For loop. Here's the function I defined

SquareSum[n_Integer]/;n>0:= For[i = 0; x = 0, i < n, i++, term = (x + (i + 1))^2; x = term]

of course the for loop I made is just as I wanted but there is no output upon calling this function. I tried adding Print[x] to the code will does display the result but that doesn't count as output. How do I get the value of x from the final stage of loop and display it as proper output upon calling this function.

Attached image will show what I meant with using the Print[x] function.SquareSum[i]

Any ideas. Thanks.

POSTED BY: Waqar ahmed
5 Replies
Posted 5 years ago

Everything in Wolfram Language (Mathematica) is a function. Some functions return a value, some do not. The For[...] does not return a value, i.e. the outcome is Null.

Hence your function calculates everything correctly but does not return the result x. You have to put it explicitly at the end:

(For[i = 0; x = 0, i < n, i++, term = (x + (i + 1))^2; x = term]; x)

BTW: You do not need the intermediate variable term. You can write:

(For[i = 0; x = 0, i < n, i++, x = (x + (i + 1))^2]; x)

BTW: Your function creates global variables i, x and term, which is not desirable. You should write:

SquareSum[n_Integer]/;n>0:= Block[{i,x},
For[i = 0; x = 0, i < n, i++, x = (x + (i + 1))^2];
x];

or Module instead of Block.

BTW: You should not name your variables and functions with a first uppercase letter. This might conflict with WL-defined names. You'd better name your function "squareSum".

POSTED BY: Werner Geiger

Hey Martijn, thanks for the bit. I am new to Wolfram language, without knowing many built-in functions For loop was my first intuition. Thanks for the processing time analysis. btw, I can't use your suggestions in this problem because this was from a challenge problem and this would be called cheating. O well, my method doesn't land that far from Fold.

POSTED BY: Waqar ahmed

Why use For. Mathematica has a build in symbol to do iterative functions, Fold and FoldList, those are much faster.

In[1]:= SquareSum[n_Integer] /; n > 0 := 
 Fold[((#1 + (#2 + 1)))^2 &][0, Range[0, n - 1]]
SquareSumList[n_Integer] /; n > 0 := 
 FoldList[((#1 + (#2 + 1)))^2 &][1, Range[1, n - 1]]
SquareSumO[n_Integer] /; 
  n > 0 := (For[i = 0; x = 0, i < n, i++, term = (x + (i + 1))^2;
   x = term]; x)

In[4]:= (l1 = Table[SquareSumO[i], {i, 1, 10}];) // RepeatedTiming

Out[4]= {0.00020, Null}

In[5]:= (l2 = Table[SquareSum[i], {i, 1, 10}];) // RepeatedTiming

Out[5]= {0.00017, Null}

In[6]:= l3 = SquareSumList[10]; // RepeatedTiming

Out[6]= {0.000026, Null}

In[7]:= l1 == l2 == l3

Out[7]= True
POSTED BY: Martijn Froeling

I did that but without the parenthesis.(^_^') my bad. With parenthesis it worked. Thanks!

POSTED BY: Waqar ahmed

It´s easy.. just put "x" at the end of the function like this:

SquareSum[n_Integer] /; 
  n > 0 := (For[i = 0; x = 0, i < n, i++, term = (x + (i + 1))^2; 
   x = term]; x)
POSTED BY: Claudio Chaib
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard