Last active
December 7, 2025 03:40
-
-
Save benprew/85adea9b1e8e261bd485a4a87c308891 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // returns a list of integer factors sorted descending, since we don't care about pairs. | |
| func maximalProperDivisors(n int) (divisors []int) { | |
| primes := constructPrimes(int(math.Sqrt(float64(n)))) | |
| for _, p := range primeFactors(n, primes) { | |
| divisors = append(divisors, n/p) | |
| } | |
| return divisors | |
| } | |
| // find prime numbers up to SQRT of N | |
| func buildPrimes(n int) { | |
| n = int(math.Sqrt(float64(n))) | |
| for i := range n { | |
| if isPrime(i) { | |
| PRIMES = append(PRIMES, i) | |
| } | |
| } | |
| } | |
| func isPrime(n int) bool { | |
| if n < 2 { | |
| return false | |
| } | |
| if n == 2 { | |
| return true | |
| } | |
| if n%2 == 0 { | |
| return false | |
| } | |
| for i := 3; i*i <= n; i += 2 { | |
| if n%i == 0 { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| func maxId(ids []string) (max int) { | |
| for _, id := range ids { | |
| idRange := strings.Split(id, "-") | |
| end := toi(idRange[1]) | |
| if end > max { | |
| max = end | |
| } | |
| } | |
| return max | |
| } | |
| // primes is sorted smallest to largest | |
| func primeFactors(n int, primes []int) (factors []int) { | |
| max := int(math.Sqrt(float64(n))) | |
| for _, p := range primes { | |
| if p > max { | |
| break | |
| } | |
| if n%p == 0 { | |
| factors = append(factors, p) | |
| } | |
| } | |
| return factors | |
| } | |
| // return prime numbers <= n | |
| func constructPrimes(n int) []int { | |
| for i, p := range PRIMES { | |
| if p > n { | |
| return PRIMES[0:i] | |
| } | |
| } | |
| return []int{} | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment