Skip to content

Instantly share code, notes, and snippets.

@raphaelvallat
Last active March 10, 2018 02:14
Show Gist options
  • Save raphaelvallat/5d5af7205df720db53be4cc2ee7e7549 to your computer and use it in GitHub Desktop.
Save raphaelvallat/5d5af7205df720db53be4cc2ee7e7549 to your computer and use it in GitHub Desktop.
Find the divisor of a number closest to another number
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