1

I need to find the k in the range [1, 10], which is the least positive integer such that binomial(k, 2)≥ m, where m≥3 - integer. The binomial() function is the binominal coefficient.

My attempt is:

After some algebraic steps, I have found the minization task: min k(k-1) - 2m ≥ 0, s.t. m≥3. I have defined the objective function and gradient. In the objective function I fixed the m=3 and my problem is how to define integer domain for the variable m.

from scipy.optimize import line_search

# objective function
def objective(k):
  m = 3
  return k*(k-1)-2*m

# gradient for the objective function
def gradient(k):
    return 2.0 * k - 1

# define range
r_min, r_max = 1, 11

# prepare inputs
inputs = arange(r_min, r_max, 1)
# compute targets
targets = [objective(k) for k in inputs]

# define the starting point
point = 1.0
# define the direction to move
direction = 1.0
# print the initial conditions
print('start=%.1f, direction=%.1f' % (point, direction))
# perform the line search
result = line_search(objective, gradient, point, direction)
print(result)

I have see the

LineSearchWarning: The line search algorithm did not converge

Question. How to define the objective function in Python?

2
  • Do you have to turn your problem into an optimization problem (for example because it's an assignment and you have to do it like that), or would you accept solutions to your problem that do not take that path? I ask because you can actually continue the "algebraic steps", find explicitly the set of all k that satisfy your inequality (which is a rather simple step), intersect it with the wanted range and take the min. Commented Apr 18, 2022 at 8:34
  • @BlackBeans, It is a self-study assignment and I tried to show my attempt, but I am looking for gerenal solution in Python, and I don't need to turn the problem into an optimization problem. Commented Apr 18, 2022 at 9:14

1 Answer 1

3

You are look to minimise k such that k(k-1)-2m≥0, with additional constraints on k on which we'll come back later. You can explicitly solve this inequation, by solving the corresponding equation first, that is, finding the roots of P:=X²-X-2m. The quadratic formulas give the roots (1±√(1+4m²))/2. Since P(x)→∞ as x→±∞, you know that the x that satisfy your inequation are the ones above the greatest root, and below the lowest root. Since you are only interested in positive solutions, and since 1-√(1+m²)<0, the set of wanted solutions is [(1+√(1+m²))/2,∞). Among these solutions, the smallest integer is the ceil of (1+√(1+m²))/2 which is strictly greater than 1. Let k=ceil((1+sqrt(1+m**2))/2) be that integer. If k≤10, then your problem has a solution, which is k. Otherwise, your problem has no solutions. In Python, you get the following:

import math

def solve(m):
  k = math.ceil((1+math.sqrt(1+m**2))/2)
  return k if k <= 10 else None
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.