3
$\begingroup$

This code below works fine for me.

factor[x_] := 
      Module[{i = 1}, 
       NestList[Module[{result}, result = #*i; i = i + 2; result] &, 1, 
        x]];
factor[10]
{1, 1, 3, 15, 105, 945, 10395, 135135, 2027025, 34459425, 654729075}

But I wanna get it done with Do.

fat[x_] :=    Module[{i = 1},     
  Do[
    Module[{result}, 
      result = result*i; i = i + 2; 
      result
    ],  
    {x}
  ]
]; 
fat[9]

I failed to pull that off. It pops up $RecursionLimit

$\endgroup$
2
  • 1
    $\begingroup$ At each iteration you are creating result again and it does not have any initial value so you are essentially doing: ClearAll[x]; x = x + 1 $\endgroup$
    – Kuba
    Jan 2 '20 at 12:32
  • 1
    $\begingroup$ @Kuba, How can you correct this? $\endgroup$
    – kile
    Jan 2 '20 at 12:44
3
$\begingroup$

Here is a way implement your function with Do.

f[n_] :=
  Module[{result = 1, list = {1}},
    Do[
      result = result*i;
      list = Join[list, {result}],
      {i, 1, 2 n, 2}];
   list]
f[5]

{1, 1, 3, 15, 105, 945}

$\endgroup$
4
  • $\begingroup$ Isn't this essentially the same as my comment in the question? $\endgroup$ Jan 2 '20 at 16:38
  • $\begingroup$ @RohitNamjoshi. The question doesn't have a comment made by you. $\endgroup$
    – m_goldberg
    Jan 2 '20 at 16:41
  • $\begingroup$ Sorry, I meant my comment on Kuba's answer. $\endgroup$ Jan 2 '20 at 16:42
  • 2
    $\begingroup$ @RohitNamjoshi. The two methods are similar, but since but since you posted your solution as a comment to another answer, I didn't see it. If you had written it as an answer (as I think you should have), I would likely not have posted my version. $\endgroup$
    – m_goldberg
    Jan 2 '20 at 16:48
3
$\begingroup$

At each iteration you are creating result again and it does not have any initial value so you are essentially doing:

ClearAll[x]; x = x + 1

Minor issue is that you don't have to manually increment i but use iterator spec {i, 1, 2 x - 1, 2}.

Moreover, if you insist on a procedural approach you need to switch to Table or Sow from Do because Do does not return anything:

fat[x_] := Module[{result = 1},
  Table[   result *= i,   {i, 1, 2 x - 1, 2}  ] // Prepend[1]
]
$\endgroup$
8
  • $\begingroup$ Generate a symbolic continued fraction: t = x; Do[t = 1/(1 + t), {5}]; t But this actually works! $\endgroup$
    – kile
    Jan 2 '20 at 13:41
  • $\begingroup$ If you have to use Do then: fat[x_] := Module[{results = {1}, result = 1}, Do[result = result*i; AppendTo[results, result], {i, 1, 2 x - 1, 2}]; results] ` $\endgroup$ Jan 2 '20 at 14:11
  • $\begingroup$ @kile Except that is a final fraction not each step like above. $\endgroup$
    – Kuba
    Jan 2 '20 at 14:12
  • 1
    $\begingroup$ @rohit Iterating with AppendTo will not scale well. $\endgroup$
    – Kuba
    Jan 2 '20 at 14:13
  • $\begingroup$ @Kuba Yes, thanks. I should have mentioned that. kile take a look at this $\endgroup$ Jan 2 '20 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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