Category talk:Go-rcu: Difference between revisions
m
Missing comma.
(→Source code: Added some more functions.) |
m (Missing comma.) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1:
===Source code===
<
import
"fmt"
"golang.org/x/exp/constraints"
"math"
)
type Int = constraints.Integer
// Returns the larger of two ints.▼
func Max(x, y int) int {▼
if x > y {
return x
Line 12 ⟶ 18:
}
// Returns the smaller of two
func Min[T Int](x, y
if x < y {
return x
Line 20 ⟶ 26:
}
// Returns the absolute value of an
func Abs[T Int](x
if x < 0 {
return -x
Line 28 ⟶ 34:
}
// Returns the greatest common divisor of two
func Gcd[T Int](x, y
for y != 0 {
x, y = y, x%y
Line 36 ⟶ 42:
}
// Returns the least common multiple of two
func Lcm[T Int](x, y
// Returns whether or not an
func IsPrime[T Int](n
switch {
case n < 2:
Line 48 ⟶ 54:
case n%3 == 0:
return n == 3
case n%5 == 0:
default:
d :=
for _, w :=
return false
}
d += w
}
▲ d += 4
}
return true
Line 68 ⟶ 78:
// c[i] is false if 'i' is prime or true if 'i' is composite.
// Optionally processes even numbers >= 4.
func PrimeSieve[T Int](limit
limit++
// True denotes composite, false denotes prime.
Line 75 ⟶ 85:
c[1] = true
if processEven {
for i := T(4); i < limit; i += 2 {
c[i] = true
}
}
p := T(3) // Start from 3.
for {
p2 := p * p
Line 99 ⟶ 109:
// Returns a slice of all primes up to and including 'limit'.
func Primes[T Int](limit
c := PrimeSieve(limit, false)
if limit < 2 {
return []
}
primes := []
for i := T(3); i < T(len(c)); i += 2 {
if !c[i] {
primes = append(primes, i)
Line 111 ⟶ 121:
}
return primes
}
// Sieves for primes up to and including 'n' and returns how many there are.
// Uses an algorithm better suited to counting than the one used in the PrimeSieve method.
func PrimeCount[T Int](n T) int {
if n < 2 {
return 0
}
if n == 2 {
return 1
}
count := 1
k := (n-3)/2 + 1
marked := make([]bool, k) // all false by default
limit := (T(math.Sqrt(float64(n)))-3)/2 + 1
for i := T(0); i < limit; i++ {
if !marked[i] {
p := 2*i + 3
s := (p*p - 3) / 2
for j := s; j < k; j += p {
marked[j] = true
}
}
}
for i := T(0); i < k; i++ {
if !marked[i] {
count++
}
}
return count
}
// Returns the prime factors of 'n' in order using a wheel with basis [2, 3, 5].
func PrimeFactors[T Int](n
if n < 2 {
return []
}
inc := []
var factors []
for n%2 == 0 {
factors = append(factors, 2)
Line 132 ⟶ 172:
n = n / 5
}
for k, i := T(7), 0; k*k <= n; {
if n%k == 0 {
factors = append(factors, k)
Line 148 ⟶ 188:
// Returns all the divisors of 'n' including 1 and 'n' itself.
func Divisors[T Int](n
if n < 1 {
return []
}
var divisors []
var divisors2 []
i := T(1)
k := T(1)
if n%2 == 1 {
k = 2
Line 176 ⟶ 216:
// Returns all the divisors of 'n' excluding 'n'.
func ProperDivisors[T Int](n
d := Divisors(n)
c := len(d)
if c <= 1 {
return []
}
return d[0 : len(d)-1]
}
// Reverses a slice of
func ReverseInts[T Int](s []
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
Line 192 ⟶ 232:
}
// Returns a slice of an
func Digits[T Int](n T, b int) []int {
if n == 0 {
return []int{0}
Line 199 ⟶ 239:
var digits []int
for n > 0 {
digits = append(digits, int(n%T(b)))
n /= T(b)
}
ReverseInts(digits)
Line 206 ⟶ 246:
}
// Returns the sum of an
func DigitSum[T Int](n T, b int) int {
sum := 0
for n > 0 {
sum += int(n % T(b))
n /= T(b)
}
return sum
}
// Returns the sum of a slice of
func SumInts[T Int](a []
sum := T(0)
for _, i := range a {
sum += i
Line 225 ⟶ 265:
}
// Returns the maximum of a slice of
func MaxInts[T Int](a []
max :=
for _, i := range a[1:] {
if i > max {
max = i
Line 236 ⟶ 276:
}
// Returns the minimum of a slice of
func MinInts[T Int](a []
min :=
for _, i := range a[1:] {
if i < min {
min = i
Line 247 ⟶ 287:
}
// Adds thousand separators to an
func Commatize[T Int](n
s := fmt.Sprintf("%d", n)
if n < 0 {
Line 263 ⟶ 303:
}
// Prints a slice of
// and optionally comma separators.
func PrintTable[T Int](s []
for i := 0; i < len(s); i++ {
if !commas {
Line 279 ⟶ 319:
fmt.Println()
}
}</
|