diff options
| author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-19 11:10:48 -0500 |
|---|---|---|
| committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-19 11:10:48 -0500 |
| commit | 165e55184e79553c74b9c9056fd46f1b37a2b5d9 (patch) | |
| tree | 1bd1f92024708da0e381d7db4eecc2500bbb0829 /factors/prime_factorization.go | |
| parent | 23cc07dd1655df05f6967ce848169ab4c658e707 (diff) | |
factors: refactor code to improve readability
Diffstat (limited to 'factors/prime_factorization.go')
| -rw-r--r-- | factors/prime_factorization.go | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/factors/prime_factorization.go b/factors/prime_factorization.go index f91a2cf..d9f11e5 100644 --- a/factors/prime_factorization.go +++ b/factors/prime_factorization.go @@ -19,17 +19,13 @@ func PrimeFactorize(n uint) PrimeFactorization { return PrimeFactorization{map[uint]uint{0: 1}} } - unfactored := n // number with all found factors removed (divided) + unfactored := n exponents := make(map[uint]uint) - // try each factor starting at 2 - // if unfactored is divisible divide out until it isn't - for f := uint(2); f*f <= unfactored; { - if unfactored%f == 0 { - unfactored /= f - exponents[f] += 1 - } else { - f += 1 + for possibleFactor := uint(2); possibleFactor*possibleFactor <= unfactored; possibleFactor++ { + for unfactored%possibleFactor == 0 { + unfactored /= possibleFactor + exponents[possibleFactor] += 1 } } @@ -90,3 +86,7 @@ func (factors PrimeFactorization) String() string { } return strings.Join(parts, " × ") } + +func isPrime(p uint) bool { + return p > 1 && PrimeFactorize(p).Exponent(p) == 1 +} |
