Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have written a recursive function and would like to re-write the code using Module AND While to compare the timings.

Here is my recursive function for f[n], where 6 n f[n] = f[n-1] + n! for n>0 and f[0]=7.

Clear[f]  
f[0] = 7;  
f[n_Integer /; n>0] := f[n-1]/(6 n) + n!  

Now I want to write it using Module AND While, I could write using Module ONLY, but I am struggling when I need to combine it using While loop.
Here is my code using Module only:

 fm[n_] := Module[{f}, f[0] = 7; f[i_] := f[i-1]/(6 i) + i!; f[n]]

Then if I want to use Module AND While, I get into trouble, here is my code:

 fm[n_] := Module[{fw}, While[n>=0, fw[0] = 7; fw[i_] := fw[i-1]/(6 i); fw[n] ] ]  

The code is not working and I couldn't figure out where does it go wrong. Do I still need to write recursively inside the While loop? And how could I write the correct code?
Any detailed explanations are greatly appreciated. Thanks in advance!

share|improve this question
2  
user, I am repeating myself: please use proper code formatting and not LaTeX in a quote block. See editing help. While editing you can select the code and press Ctrl+K to format it as such. –  Mr.Wizard May 17 '13 at 1:22
    
@Mr.Wizard Thanks for your comment. But how can I write fractions without using Latex. I didn't seem to find any ways to do that in the editing help. –  user71346 May 17 '13 at 1:27
1  
The point is to write code others can copy-paste into Mathematica. Write f[n-1] / (6 n) etc. instead of TeX. –  Michael E2 May 17 '13 at 1:30
add comment

3 Answers

up vote 3 down vote accepted

You mean this?

fw[i_Integer] := Module[{n = 0, last = 7}, While[++n <= i,
   last = 1/(6 n) last + n!;
   ];
  last]
share|improve this answer
    
Thanks very much. It works. Your answer is very helpful. Now I have learned how to write in different ways. –  user71346 May 17 '13 at 2:01
add comment

Since you are interested in learning different ways to write this, here is one that avoids using "variables" entirely, thus removing the need for Module:

fnw[i_Integer] := 
  Last @ NestWhile[{# + 1, 1/(6 #) #2 + #!} & @@ # &, {1, 7}, #[[1]] <= i &]

An expression such as #[[1]] <= i & is a pure function with a single parameter represented by # (Slot). The expression {# + 1, 1/(6 #) #2 + #!} & @@ # & is two such functions strung together, with the longer one applied (@@) to the second, like this:

test = #/#2 & @@ # &;
test[{"a", "b"}]
"a"/"b"
share|improve this answer
add comment

This answer is also not direct answer because there is no Module or While but it is good to know that construct:

f[n_Integer /; n>0] := f[n] = f[n - 1]/(6 n) + n!
f[0] = 7

More here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.