Solving a Linear Diophantine Equation(see description for examples)

前端 未结 8 944
忘掉有多难
忘掉有多难 2020-12-16 20:27

Let me start off by clarifying that(before you guys dismiss me), this is not a homework problem and I\'m not a university student. :)

EDIT Thanks to

8条回答
  •  没有蜡笔的小新
    2020-12-16 20:57

    Brute-force recursion is an option, depending on how large you will allow the value or number of values to become.

    Assumptions: The user inputs (the multiplicands) are always distinct positive integers. The coefficients to be found must be non-negative integers.

    Algorithm:

    Of the multiplicands, let M be the largest.
    Calculate C=floor(F/M).
    If F=M*C, output solution of the form (0,0,...,C) and decrement C
    If M is the only multiplicand, terminate processing
    Loop from C down to 0 (call that value i)
      Let F' = F - i*M
      Recursively invoke this algorithm:
        The multiplicands will be the current set minus M
        The goal value will be F'
      For each solution output by the recursive call:
         append i to the coefficient list and output the solution
    

提交回复
热议问题