https://github.com/willdunklin/closest_factors
https://github.com/willdunklin/closest_factors
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/willdunklin/closest_factors
- Owner: willdunklin
- Created: 2021-03-31T17:11:24.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-31T17:22:30.000Z (about 5 years ago)
- Last Synced: 2025-03-05T01:45:18.606Z (over 1 year ago)
- Language: Python
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# closest_factors
Here we're trying to get the nearest factors of a number n.
Specifically we want numbers `a,b` such that `n = a*b`, while minimizing `|a-b|`.
### A use case:
For example, if you want to convert a 1d array of length `n` into the most square 2d matrix possible,
this algorithm efficiently shows what the dimensions `a,b` of that matrix should be for `n`.
## Usage
```
closest_factors(34929)
> (3881, 9)
closest_factors(429538750)
> (343631, 1250)
closest_factors(25)
> (5, 5)
```