We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e071e7e commit aff6badCopy full SHA for aff6bad
src/easy/AdditivePersistence.go
@@ -0,0 +1,30 @@
1
+package main
2
+
3
+import (
4
+ "fmt"
5
+ "strconv"
6
+)
7
8
+// AdditivePersistence calculates the additive persistence of a positive integer
9
+func AdditivePersistence(num int) int {
10
+ times := 0
11
+ added := num
12
+ for added > 9 {
13
+ sum := 0
14
+ digits := strconv.Itoa(added)
15
+ for _, digit := range digits {
16
+ digitInt, _ := strconv.Atoi(string(digit))
17
+ sum += digitInt
18
+ }
19
+ added = sum
20
+ times++
21
22
+ return times
23
+}
24
25
+func main() {
26
+ result1 := AdditivePersistence(199)
27
+ fmt.Println(result1)
28
+ result2 := AdditivePersistence(913)
29
+ fmt.Println(result2)
30
0 commit comments