Skip to content

Commit a199df0

Browse files
committed
add count unique digits
1 parent 58342df commit a199df0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Math/count_unique_digits,go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func countNumbersWithUniqueDigits(n int) int {
8+
// Base case: n = 0, only 1 possible number (0)
9+
if n == 0 {
10+
return 1
11+
}
12+
13+
// For n > 0, initialize count to 10 (digits 0-9)
14+
count := 10
15+
16+
// Calculate count for 2 to n digits
17+
for i := 2; i <= n; i++ {
18+
// For each number of digits, calculate the number of possible unique numbers
19+
// First digit can be 1-9 (9 choices), subsequent digits can be any of the remaining 9-1 = 8 choices
20+
// Multiply the choices together to get the total number of possible unique numbers
21+
// Add this count to the total count
22+
currCount := 9
23+
for j := 1; j < i; j++ {
24+
currCount *= 10 - j
25+
}
26+
count += currCount
27+
}
28+
29+
return count
30+
}
31+
32+
func main() {
33+
// Test case with n = 2
34+
fmt.Println(countNumbersWithUniqueDigits(2)) // Output: 91
35+
}

0 commit comments

Comments
 (0)