2
$\begingroup$

Really confused by how one should use the Module[] command in two variables. For example my function here should return the DigitalRoot of a number; i.e. func[5432] -> 14 -> 5 or for func[23] -> 5.

My example uses the Module command but the solution is wrong;

Fox[a_] := Module[
  {x1, x2},
  x1 = 0; 
  x2 = a; 
  While[
    x1 != 1, 
    x1 = Length[IntegerDigits[x2]]; 
    x2 = Total[InterDigits[x2]]; 
    Return[x2]
  ]
] 

The Module[] and While[] don't appear to loop, so Fox[5432] = 5432 and Fox[23] = 23.

Maybe my understanding of the Module[] command is wrong.

Please help

$\endgroup$
9
$\begingroup$

Your approach works fine, aside from one typo, InterDigits, and the fact that you have a Return inside the While statement, when you rarely need to use Return at all.

Fox[a_] := Module[
    {x1, x2},
    x1 = 0;
    x2 = a;
    While[x1 =!= 1,
        x1 = Length[ IntegerDigits[ x2 ] ];
        x2 = Total[ IntegerDigits[ x2 ] ];
    ];
    (* no need to use Return, just return the value *)
    x2
]
$\endgroup$
6
$\begingroup$

better way..

Fox[a_] :=
 NestWhile[Total@IntegerDigits@# & , a , 
  Length@IntegerDigits@# != 1 & ]

or

Fox[a_] :=
  NestWhile[Total@IntegerDigits@# & , a , # > 10 & ]
$\endgroup$
  • $\begingroup$ I like the functional style! You could use the infix composition @* to make it pointfree, e.g. Total@*IntegerDigits or even (IntegerDigits/*Length/*(Not@*EqualTo[1])) :) $\endgroup$ – Thies Heidecke Apr 17 '18 at 21:22
2
$\begingroup$

I am not sure what your scoping is attempting to accomplish. If your question needs to be solved with a scoping construct, I'm not sure where to go, but you might want to try

singDigitNumberSum[num_] := If[
    Length[IntegerDigits[num]] === 1, 
    num, 
    singDigitNumberSum[Total[IntegerDigits[num]]]
]

with an appropriate $RecursionLimit.

$\endgroup$

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.