Skip to content

Commit aff6bad

Browse files
committed
AdditivePersistence resolved
1 parent e071e7e commit aff6bad

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/easy/AdditivePersistence.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy