Created
March 3, 2024 18:48
-
-
Save debdutdeb/f43b67fbb0768f549df90a35d33460fe to your computer and use it in GitHub Desktop.
Code for different number theory algorithms in golang ofc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func gcd(a, b int) int { | |
var fun func(int, int) int | |
fun = func(l, h int) int { | |
r := h % l | |
if r == 0 { | |
return l | |
} | |
if r == 1 { | |
return r | |
} | |
return fun(r, l) | |
} | |
if a > b { | |
return fun(b, a) | |
} | |
return fun(a, b) | |
} | |
// inefficient | |
func primes(n int) []int { | |
primes := []int{1} | |
mul := 1 | |
m := n | |
for i := 2; ; i += 1 { | |
r := m % i | |
if r != 0 { | |
continue | |
} | |
primes = append(primes, i) | |
if i == m { | |
return primes | |
} | |
mul *= i | |
m = n / mul | |
i = 2 | |
} | |
} | |
func main() { | |
a, b := 16, 72 | |
fmt.Println(gcd(a, b)) | |
fmt.Println(primes(9488485)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment