funcprog: Minor improvement in assignment 5.3.

parent f2739c4d
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
* recursively until a single digit is obtained. * recursively until a single digit is obtained.
*) *)
let rec digitRoot ?(sum=0) number = match number with let rec digitRoot ?(sum=0) number =
_ when number < 10 -> (sum + number) let res = match number with
| _ -> (digitRoot (digitRoot ~sum:(sum + (number mod 10)) (number / 10))) | _ when number < 10 -> (sum + number)
| _ -> (digitRoot ~sum:(sum + (number mod 10)) (number / 10))
in if res < 10 then res else digitRoot res
;; ;;
let test_digitRoot input = let test_digitRoot input =
...@@ -20,4 +22,6 @@ test_digitRoot 65536;; (* = 7 *) ...@@ -20,4 +22,6 @@ test_digitRoot 65536;; (* = 7 *)
test_digitRoot 12345678;; (* = 9 *) test_digitRoot 12345678;; (* = 9 *)
test_digitRoot 18273645;; (* = 9 *) test_digitRoot 18273645;; (* = 9 *)
test_digitRoot 123456789;; (* = 9 *) test_digitRoot 123456789;; (* = 9 *)
test_digitRoot 1234567890123456789;; (* = 9 *)
test_digitRoot 999999999998;; (* = 8 *)
test_digitRoot 5674;; (* = 4 *) test_digitRoot 5674;; (* = 4 *)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment