Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gcd, lcm, primeDecomposition #61

Merged
merged 1 commit into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions snippets/go/gcd.snip
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


snippet gcd
func gcd (a, b float64) float64 {
if a > b {
return _gcd(a, b)
}
return _gcd(b, a)
}


func _gcd (a, b float64) float64 {
if b != 0 {
return _gcd(b, math.Mod(a, b))
}
return a
}

23 changes: 23 additions & 0 deletions snippets/go/lcm.snip
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


snippet lcm
func gcd (a, b float64) float64 {
if a > b {
return _gcd(a, b)
}
return _gcd(b, a)
}


func _gcd (a, b float64) float64 {
if b != 0 {
return _gcd(b, math.Mod(a, b))
}
return a
}


func lcm (a, b float64) float64 {
return (a * b) / gcd(a, b)
}

6 changes: 2 additions & 4 deletions snippets/go/minmax.snip
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ snippet max
func max(a int, b int) int {
if a > b {
return a
} else {
return b
}
return b
}


snippet min
func min(a int, b int) int {
if a < b {
return a
} else {
return b
}
return b
}

37 changes: 37 additions & 0 deletions snippets/go/prime_decomp.snip
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


snippet prime_decomp
// PrimeFactorPair each factor and its frequency
type PrimeFactorPair struct {
f int
b int
}


// prime demonposition
func primeDecomp (base int) []PrimeFactorPair {
var result []PrimeFactorPair

for i := 2; i*i <= base; i++ {

if (base % i == 0) {
var cnt int

for base % i == 0 {
base /= i
cnt++
}

factor := PrimeFactorPair{i, cnt}
result = append(result, factor)
}

}

if (base > 1) {
factor := PrimeFactorPair{base, 1}
result = append(result, factor)
}

return result
}