Last active
March 10, 2018 02:14
-
-
Save raphaelvallat/5d5af7205df720db53be4cc2ee7e7549 to your computer and use it in GitHub Desktop.
Find the divisor of a number closest to another number
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
import numpy as np | |
def find_closest_divisor(n, m): | |
"""Find the divisor of n closest to m | |
""" | |
divisors = np.array([ i for i in range(1, int(np.sqrt(n)+1)) if n % i == 0 ]) | |
divisions = n / divisors | |
return divisions[np.argmin(np.abs(m - divisions))] | |
number, divisor = 1024, 100 | |
new_divisor = find_closest_divisor(number, divisor) | |
print(new_divisor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment