Message Boards Message Boards

0
|
6276 Views
|
6 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Find digital sums and digital roots?

Posted 7 years ago

Hi

I'm a newbie to Mathematica (having played with it for just three days), so apologies for what might seem a dumb question. I can't find anywhere (even in the Help) a function for calculating the digital sum and the digital root of an integer. I can do it programmatically, but surely there is a built-in function or command for these common digit-manipulation tasks. Cheers

Geoffrey Marnell

POSTED BY: Geoffrey Marnell
6 Replies

Thanks Sander. Silly me. I searched the help for "Offset" and got nothing of relevance Clearly, I should have searched for Mod!! Cheers Geoffrey

POSTED BY: Geoffrey Marnell

Thank you Szabolcs, Paul and Sander. This all works perfectly. I am intrigued by "offset". The accompanying documentation doesn't explain how it affects mod calculations. It works, but it puzzles me. Cheers Geoffrey

POSTED BY: Geoffrey Marnell

it does explain it:

enter image description here

one just has to read it...

POSTED BY: Sander Huisman
Posted 7 years ago

Or for the digital root you could use

digitalroot[k_Integer] := Mod[k - 1, 9] + 1
POSTED BY: Paul Cleary

Mod accepts a third argument, making this easier:

digitalroot[k_Integer] := Mod[k, 9, 1]
POSTED BY: Sander Huisman

One way to implement these would be

digitalSum[k_Integer] := Total@IntegerDigits[k]

digitalRoot[k_Integer] := FixedPoint[digitalSum, k]

FixedPoint will repeatedly apply digitalSum until the result does not change. A more explicit implementation could use NestWhile, with a check on the number of digits. But this trick with FixedPoint makes the code shorter.

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract