-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.go
47 lines (39 loc) · 885 Bytes
/
operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package numstream
import (
"math"
)
//Sum is an n-ary addition operation.
func Sum(ns ...int) int {
l := len(ns)
if l == 1 {
return ns[0]
}
return ns[0] + Sum(ns[1:]...)
}
//Add is a binary addition operation.
func Add(m, n int) int {
return m + n
}
//IncrementBy returns a Unary addition function.
func IncrementByX(n int) Unary {
return Partial2(Add, n)
}
//Increment is a Reduce incrementer for use in generators
var Increment = ReduceUnary(IncrementByX(1))
//FactorOut returns the second argument after factoring out the first argument as many times as possible
func FactorOut(n, m int) int {
if n == 0 {
return m
}
for m%n == 0 {
m /= n
}
return m
}
//FactorOutX returns a FactorOut Unary bound to a factor
func FactorOutX(n int) Unary {
return Partial2(FactorOut, n)
}
func GreatestPossiblePrimeFactor(n int) int {
return int(math.Sqrt(float64(n)))
}