From d1c3f6619b63d33db2bfb6ab08e3a1c99cf1f62d Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Fri, 12 Feb 2021 13:25:14 +0100 Subject: [PATCH] Added Len{,64} gives back the number of digits of a number. --- ints/statistics.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ints/statistics.go b/ints/statistics.go index d3bdbf6..3818b60 100644 --- a/ints/statistics.go +++ b/ints/statistics.go @@ -62,6 +62,28 @@ func IsPalindromic(n int) bool { return true } +// Len returns the number of digits of i (base 10, does not include a sign). +func Len(i int) int { + return Len64(int64(i)) +} + +// Len64 returns the number of digits of i (base 10, does not include a sign). +func Len64(i int64) int { + if i == 0 { + return 1 + } + if i < 0 { + i = 0 - i + } + + digits := 0 + for i > 0 { + i /= 10 + digits++ + } + return digits +} + // Permutations returns all possible permutations of the the set integers. func Permutations(set []int) [][]int { var n = len(set)