Sum of digits

Started by cameyo, June 03, 2019, 12:55:27 AM

Previous topic - Next topic

cameyo

Given a number n, find the sum of its digits until sum becomes single digit.
Example: n = 7865 = 7 + 8 + 6 + 5 = 26 ==> 2 + 6 = 8
(define (digitSum n)
  (if (zero? n) 0
    (if (zero? (% n 9)) 9
      (% n 9))))

(digitSum 236753647864)
;-> 7

rickyboy

#1
Getta via il 9.  :)  https://en.wikipedia.org/wiki/Casting_out_nines">https://en.wikipedia.org/wiki/Casting_out_nines
(λx. x x) (λx. x x)

cameyo

#2
Yes, rickyboy. A very elegant solution.

Thanks for the italian translation :-)

TedWalther

#3

(define (digital_root n)
    (+ 1 (% (- n 1) 9))

> (digital_root 236753647864)
7


I heard this referred to as Raman's forumula, but not sure if that is the proper name for it.  Even Wolfram's website didn't give an origin for it that I could find.  It works though.  I tested it on the numbers from 0 to 100.



Update:



I can't verify this, but further web searching indicates this solution may be a special case of Ramanujan's Congruence.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

cameyo

#4
Some proofs:  

https://www.quora.com/How-would-I-prove-that-every-positive-integer-is-congruent-to-the-sum-of-its-base-10-digits-modulo-9">//https://www.quora.com/How-would-I-prove-that-every-positive-integer-is-congruent-to-the-sum-of-its-base-10-digits-modulo-9