Category talk:Go-rcu: Difference between revisions

→‎Source code: Added some more functions.
(→‎Source code: Added commatize option to PrintTable function.)
(→‎Source code: Added some more functions.)
Line 111:
}
return primes
}
 
// Returns the prime factors of 'n' in order using a wheel with basis [2, 3, 5].
func PrimeFactors(n int) []int {
if n < 2 {
return []int{}
}
inc := []int{4, 2, 4, 2, 4, 6, 2, 6}
var factors []int
for n%2 == 0 {
factors = append(factors, 2)
n = n / 2
}
for n%3 == 0 {
factors = append(factors, 3)
n = n / 3
}
for n%5 == 0 {
factors = append(factors, 5)
n = n / 5
}
for k, i := 7, 0; k*k <= n; {
if n%k == 0 {
factors = append(factors, k)
n = n / k
} else {
k += inc[i]
i = (i + 1) % 8
}
}
if n > 1 {
factors = append(factors, n)
}
return factors
}
 
// Returns all the divisors of 'n' including 1 and 'n' itself.
func Divisors(n int) []int {
if n < 1 {
return []int{}
}
var divisors []int
var divisors2 []int
i := 1
k := 1
if n%2 == 1 {
k = 2
}
for ; i*i <= n; i += k {
if n%i == 0 {
divisors = append(divisors, i)
j := n / i
if j != i {
divisors2 = append(divisors2, j)
}
}
}
if len(divisors2) > 0 {
ReverseInts(divisors2)
divisors = append(divisors, divisors2...)
}
return divisors
}
 
// Returns all the divisors of 'n' excluding 'n'.
func ProperDivisors(n int) []int {
d := Divisors(n)
c := len(d)
if c <= 1 {
return []int{}
}
return d[0 : len(d)-1]
}
 
Line 142 ⟶ 214:
}
return sum
}
 
// Returns the sum of a slice of ints.
func SumInts(a []int) int {
sum := 0
for _, i := range a {
sum += i
}
return sum
}
 
// Returns the maximum of a slice of ints (64-bit assumed)
func MaxInts(a []int) int {
max := -1 << 63
for _, i := range a {
if i > max {
max = i
}
}
return max
}
 
// Returns the minimum of a slice of ints (64-bit assumed)
func MinInts(a []int) int {
min := 1<<63 - 1
for _, i := range a {
if i < min {
min = i
}
}
return min
}
 
Line 160 ⟶ 263:
}
 
// Prints a slice of ints in tabular form with a given row and column size.
// and optionally comma separators.
func PrintTable(s []int, rowSize, colSize int, commas bool) {
for i := 0; i < len(s); i++ {
9,482

edits