Category talk:Go-rcu: Difference between revisions

m
Missing comma.
m (Added quotes to 'lang' attribute.)
m (Missing comma.)
 
(2 intermediate revisions by the same user not shown)
Line 4:
import (
"fmt"
"golang.org/x/exp/constraints"
"rcumath"
)
 
type Int = constraints.Integer
// Returns the larger of two ints.
 
func Max(x, y int) int {
// Returns the larger of two intsintegers.
func Max[T Int](x, y intT) intT {
if x > y {
return x
Line 15 ⟶ 18:
}
 
// Returns the smaller of two intsintegers.
func Min[T Int](x, y intT) intT {
if x < y {
return x
Line 23 ⟶ 26:
}
 
// Returns the absolute value of an intinteger.
func Abs[T Int](x intT) intT {
if x < 0 {
return -x
Line 31 ⟶ 34:
}
 
// Returns the greatest common divisor of two intsintegers.
func Gcd[T Int](x, y intT) intT {
for y != 0 {
x, y = y, x%y
Line 39 ⟶ 42:
}
 
// Returns the least common multiple of two intsintegers.
func Lcm[T Int](x, y intT) intT { return Abs(x*y) / Gcd(x, y) }
 
// Returns whether or not an intinteger is prime.
func IsPrime[T Int](n intT) bool {
switch {
case n < 2:
Line 51 ⟶ 54:
case n%3 == 0:
return n == 3
case n%5 == 0:
return n d +== 45
default:
d := 5T(7)
forwheel d*d <:= n []T{4, 2, 4, 2, 4, 6, 2, 6}
if n%d == 0for {
for _, w := returnrange falsewheel {
} if d*d > n {
d += 2 return true
if n%d == 0 {}
returnif falsen%d == 0 {
return false
}
d += w
}
d += 4
}
return true
Line 71 ⟶ 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 intT, processEven bool) []bool {
limit++
// True denotes composite, false denotes prime.
Line 78 ⟶ 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 102 ⟶ 109:
 
// Returns a slice of all primes up to and including 'limit'.
func Primes[T Int](limit intT) []intT {
c := PrimeSieve(limit, false)
if limit < 2 {
return []intT{}
}
primes := []intT{2}
for i := T(3); i < T(len(c)); i += 2 {
if !c[i] {
primes = append(primes, i)
Line 118 ⟶ 125:
// 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 intT) int {
if n < 2 {
return 0
Line 128 ⟶ 135:
k := (n-3)/2 + 1
marked := make([]bool, k) // all false by default
limit := (intT(math.Sqrt(float64(n)))-3)/2 + 1
for i := T(0); i < limit; i++ {
if !marked[i] {
p := 2*i + 3
Line 138 ⟶ 145:
}
}
for i := T(0); i < k; i++ {
if !marked[i] {
count++
Line 147 ⟶ 154:
 
// Returns the prime factors of 'n' in order using a wheel with basis [2, 3, 5].
func PrimeFactors[T Int](n intT) []intT {
if n < 2 {
return []intT{}
}
inc := []intT{4, 2, 4, 2, 4, 6, 2, 6}
var factors []intT
for n%2 == 0 {
factors = append(factors, 2)
Line 165 ⟶ 172:
n = n / 5
}
for k, i := T(7), 0; k*k <= n; {
if n%k == 0 {
factors = append(factors, k)
Line 181 ⟶ 188:
 
// Returns all the divisors of 'n' including 1 and 'n' itself.
func Divisors[T Int](n intT) []intT {
if n < 1 {
return []intT{}
}
var divisors []intT
var divisors2 []intT
i := T(1)
k := T(1)
if n%2 == 1 {
k = 2
Line 209 ⟶ 216:
 
// Returns all the divisors of 'n' excluding 'n'.
func ProperDivisors[T Int](n intT) []intT {
d := Divisors(n)
c := len(d)
if c <= 1 {
return []intT{}
}
return d[0 : len(d)-1]
}
 
// Reverses a slice of intsintegers in place.
func ReverseInts[T Int](s []intT) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
Line 225 ⟶ 232:
}
 
// Returns a slice of an intinteger's digits in base b.
func Digits[T Int](n T, b int) []int {
if n == 0 {
return []int{0}
Line 232 ⟶ 239:
var digits []int
for n > 0 {
digits = append(digits, int(n%T(b)))
n /= T(b)
}
ReverseInts(digits)
Line 239 ⟶ 246:
}
 
// Returns the sum of an intinteger's digits in base b.
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 intsintegers.
func SumInts[T Int](a []intT) intT {
sum := T(0)
for _, i := range a {
sum += i
Line 258 ⟶ 265:
}
 
// Returns the maximum of a slice of ints (64-bit assumed)integers.
func MaxInts[T Int](a []intT) intT {
max := -1 << 63a[0]
for _, i := range a[1:] {
if i > max {
max = i
Line 269 ⟶ 276:
}
 
// Returns the minimum of a slice of ints (64-bit assumed)integers
func MinInts[T Int](a []intT) intT {
min := 1<<63 - 1a[0]
for _, i := range a[1:] {
if i < min {
min = i
Line 280 ⟶ 287:
}
 
// Adds thousand separators to an intinteger.
func Commatize[T Int](n intT) string {
s := fmt.Sprintf("%d", n)
if n < 0 {
Line 296 ⟶ 303:
}
 
// Prints a slice of intsintegers in tabular form with a given row and column size
// and optionally comma separators.
func PrintTable[T Int](s []intT, rowSize, colSize int, commas bool) {
for i := 0; i < len(s); i++ {
if !commas {
9,476

edits