2023-02-01 15:52:09 +00:00
|
|
|
// Package number
|
|
|
|
// 使用随机数时需要先 调用 rand.seed()函数
|
2023-01-21 11:31:23 +00:00
|
|
|
package number
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-04 16:07:10 +00:00
|
|
|
"golang.org/x/exp/constraints"
|
2023-02-10 16:32:46 +00:00
|
|
|
"math"
|
2023-01-21 11:31:23 +00:00
|
|
|
"math/rand"
|
2023-04-15 16:57:50 +00:00
|
|
|
"strconv"
|
2023-01-21 11:31:23 +00:00
|
|
|
)
|
|
|
|
|
2023-10-27 12:51:46 +00:00
|
|
|
func Range[T constraints.Integer](start, end T, steps ...T) []T {
|
|
|
|
step := T(1)
|
|
|
|
if len(steps) > 0 {
|
|
|
|
step = steps[0]
|
|
|
|
}
|
|
|
|
var l int
|
2023-01-21 11:31:23 +00:00
|
|
|
if step == 0 {
|
2023-10-27 12:51:46 +00:00
|
|
|
l = int(end - start + 1)
|
|
|
|
} else {
|
2023-12-05 13:39:13 +00:00
|
|
|
l = int((end - start + 1) / step)
|
|
|
|
if step*T(l) <= end && step != 1 {
|
|
|
|
l++
|
|
|
|
}
|
2023-01-21 11:31:23 +00:00
|
|
|
}
|
|
|
|
if l < 0 {
|
2023-10-27 12:51:46 +00:00
|
|
|
l = -l
|
2023-01-21 11:31:23 +00:00
|
|
|
}
|
|
|
|
r := make([]T, 0, l)
|
2023-10-27 12:51:46 +00:00
|
|
|
gap := start
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
r = append(r, gap)
|
|
|
|
gap += step
|
2023-01-21 11:31:23 +00:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:48:48 +00:00
|
|
|
// Rand 都为闭区间 [start,end]
|
2023-02-04 16:07:10 +00:00
|
|
|
func Rand[T constraints.Integer](start, end T) T {
|
2023-01-21 11:31:23 +00:00
|
|
|
end++
|
|
|
|
return T(rand.Int63n(int64(end-start))) + start
|
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func Min[T constraints.Integer | constraints.Float](a ...T) T {
|
2023-10-27 12:51:46 +00:00
|
|
|
mins := a[0]
|
2023-01-21 11:31:23 +00:00
|
|
|
for _, t := range a {
|
2023-10-27 12:51:46 +00:00
|
|
|
if mins > t {
|
|
|
|
mins = t
|
2023-01-21 11:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-27 12:51:46 +00:00
|
|
|
return mins
|
2023-01-21 11:31:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func Max[T constraints.Integer | constraints.Float](a ...T) T {
|
2023-10-27 12:51:46 +00:00
|
|
|
maxs := a[0]
|
2023-01-21 11:31:23 +00:00
|
|
|
for _, t := range a {
|
2023-10-27 12:51:46 +00:00
|
|
|
if maxs < t {
|
|
|
|
maxs = t
|
2023-01-21 11:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-27 12:51:46 +00:00
|
|
|
return maxs
|
2023-01-21 11:31:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func Sum[T constraints.Integer | constraints.Float](a ...T) T {
|
2023-01-21 11:31:23 +00:00
|
|
|
s := T(0)
|
|
|
|
for _, t := range a {
|
|
|
|
s += t
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func Add[T constraints.Integer | constraints.Float](i, j T) T {
|
2023-01-27 15:48:48 +00:00
|
|
|
return i + j
|
|
|
|
}
|
2023-02-04 16:07:10 +00:00
|
|
|
func Sub[T constraints.Integer | constraints.Float](i, j T) T {
|
2023-01-28 13:38:34 +00:00
|
|
|
return i - j
|
|
|
|
}
|
2023-01-27 15:48:48 +00:00
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func ToString[T constraints.Integer | constraints.Float](n T) string {
|
2023-01-21 11:31:23 +00:00
|
|
|
return fmt.Sprintf("%v", n)
|
|
|
|
}
|
2023-01-27 15:48:48 +00:00
|
|
|
|
2023-04-15 16:57:50 +00:00
|
|
|
func IntToString[T constraints.Integer](i T) string {
|
|
|
|
return strconv.FormatInt(int64(i), 10)
|
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func Abs[T constraints.Integer | constraints.Float](n T) T {
|
2023-01-27 15:48:48 +00:00
|
|
|
if n >= 0 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
return -n
|
|
|
|
}
|
2023-01-28 13:38:34 +00:00
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func Mul[T constraints.Integer | constraints.Float](i, j T) T {
|
2023-01-28 13:38:34 +00:00
|
|
|
return i * j
|
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func Divide[T constraints.Integer | constraints.Float](i, j T) T {
|
2023-01-28 13:38:34 +00:00
|
|
|
return i / j
|
|
|
|
}
|
2023-02-10 16:32:46 +00:00
|
|
|
|
2024-06-23 13:30:57 +00:00
|
|
|
func Ceil[T constraints.Integer](num1, num2 T) int {
|
|
|
|
return int((num1 + num2 - 1) / num2)
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:42:44 +00:00
|
|
|
func DivideCeil[T constraints.Integer](num1, num2 T) T {
|
|
|
|
return T(math.Ceil(float64(num1) / float64(num2)))
|
2023-02-10 16:32:46 +00:00
|
|
|
}
|
2023-03-19 12:40:08 +00:00
|
|
|
|
|
|
|
type Counter[T constraints.Integer] func() T
|
|
|
|
|
|
|
|
func Counters[T constraints.Integer]() func() T {
|
|
|
|
var count T
|
|
|
|
return func() T {
|
|
|
|
count++
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
}
|