How can I make a module that finds the sum of digits of any given positive integer?
I use this command
F[x_]:= Apply[ Plus, IntegerDigits[x] ]
Is there another module to do it ?
How can I make a module that finds the sum of digits of any given positive integer?
I use this command
F[x_]:= Apply[ Plus, IntegerDigits[x] ]
Is there another module to do it ?
Mathematical v14.0 introduced DigitSum. For example:
DigitSum[2525]
(* 14 *)
DigitSum threads over lists.
DigitSum[{4397, 4268, 782, 2969, 4286}]
(* {23, 20, 17, 26, 20} *)
Find the same results with Total and IntegerDigits:
Total[IntegerDigits[2525]]
(* 14 *)
Total /@ IntegerDigits[{4397, 4268, 782, 2969, 4286}]
(* {23, 20, 17, 26, 20} *)
Another way:
NumberDecompose[2525, {1000, 100, 10, 1}]//Total
(* 14 *)
Or
NumberDecompose[2525, {1000, 100, 10, 1}] // NumberCompose[#, {1, 1, 1, 1}] &
(* 14 *)
f = Total@ NumberDecompose[#, Reverse@PowerRange[1, 10^Floor@Log10@#]] &;
– Syed
Mar 18 '24 at 09:52
f = Total @ ToExpression @ DeleteCases["-"] @ Characters @ ToString @ # &;
f @ 2147
14
f /@ {-4, 0, 4, 44, 444}
{4, 0, 4, 8, 12}
Yes, there are other ways. In Mathematica there are usually many other ways.
Here is a way using DigitCount and Dot:
f1[n_Integer] := DigitCount[n].{1,2,3,4,5,6,7,8,9,0}
f1[2147]
14
It can be easily extended to bases other than 10:
Clear[f1]
f1[n_Integer, b : (_Integer?Positive) : 10] := DigitCount[n, b].Mod[Range[b], b]
In base 7:
f1[2147, 7]
17
Note that there is no need to use Module (or Block), and in fact doing so for concise functions is needless clutter. Module should be used when you need localized Symbols, not merely as cruft to wrap every function you write. Block should be used even more selectively as it temporarily modifies global values. Please see: What are the use cases for different scoping constructs?
Also since I assume you are a new Mathematica user (and even if you're not) I recommend referencing What are the most common pitfalls awaiting new users?.
At some point you may wish to read How to Combine Pattern Constraints and Default Values for Function Arguments for an understanding of the second parameter pattern I used in the extended definition of f1.
dsum[n_] := Last@Accumulate[IntegerDigits[n]]
Test:
{#, dsum @#} & /@ RandomInteger[{500, 4000}, 5] // Column
f = # /. x_ :> Plus @@ ToExpression@Characters[ToString[Abs@x]] &;
f@2147
14
f /@ {-4, 0, 4, 44, 444}
{4, 0, 4, 8, 12}
Total@IntegerDigits[x]– Kuba Dec 19 '13 at 23:58F1button in Mathematica), it is very useful.Totalis basically the same asPlus @@ # &which is the same asApply[Plus, #] &. The main difference, I suppose, is thatTotalallows you to specify the level (for example you can add up all the elements of a matrix, instead of its rows). – amr Dec 20 '13 at 00:12Moduleis not a keyword. it's a regular symbol/function that you could make your own version of. the important syntax is:=, which expects a pattern on the left and basically anything you want on the right. the purpose ofModuleis to create local variables. if you don't need local variables then you don't needModule, you can just write the expression, or e.g.f[x] := (Print[x]; x)– amr Dec 20 '13 at 19:06