-1

I am given my C class homework, which is:

A hyperloop track is built of individual pipe segments of certain length. The track starts and ends with a bulkhead, and there is a bulkhead in-between each two pipe segments. The segments are produced by two different manufacturers(s1 and s2). Lengths of the segments(s1,s2), bulkheads(b), and of the desired track(l) are given. The task is to develop a function that will, based on these 4 parameters, decide whether there are valid combinations of segments and bulkheads that will result in the exact length of the desired track, and, if there are, output the number of these combinations.

Note: two different segments may be equal in their lengths, the length of a bulkhead may also be equal to zero.

My opinion is that I should solve a linear equation with 3 variables:

(m)*s1 + (n)*s2 + (m+n+1)*b = l

But I have no idea which method I should use to write an efficient code.

  • There's no finite solution in a linear equations in three variables – Thecave3 Nov 15 '18 at 13:06
  • 2
    It is clear that `m` and `n` must be nonnegative integers. Are there any restrictions on the given values `s1`, `s2`, `b`, and `l`, other than they are nonnegative? Will they be integers, rational numbers, or merely real numbers? If integers, this is a well-studied problem in Diophantine equations. If rationals, by multiplying the equation by the least common denominator you get one in integers. The restrictions will determine your approach. How much do you know about Diophantine, linear equations in two variables? – Rory Daulton Nov 15 '18 at 13:14
  • 1
    @Thecave3: I see only two variables, `m` and `n`. What is the third variable that you see? And if the given constants are rational numbers and `m` and `n` must be nonnegative integers, there will be only finitely many solutions (possibly none). – Rory Daulton Nov 15 '18 at 13:15
  • @RoryDaulton Thank you for your remark. Yes, the lengths are nonnegative integers. I am not familiar with Diophantine equations, but will now revise this topic. Thanks for advice. – Aydin Misirzadeh Nov 15 '18 at 13:20
  • 1
    You probably should look at the [Extended Euclidean algorithm](https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) as an important part of your solution. – Rory Daulton Nov 15 '18 at 13:43
  • 1
    Don't worry about efficient code. Make it run, make it run right, make it run fast. That's the proper order. – duffymo Nov 15 '18 at 14:42
  • This is a `Progtest` homework from `Programming and algorithmics 1` at `CVUT` (Czech technical university). This question was asked while the homework was still open for submission. – Elliott Jul 25 '21 at 06:57

2 Answers2

0

Equation provides criteria for finding combinations. When writing c code for this, it will only require iterating for m and n and checking if it meets l length. Below is pseudo code that can provide combinations

for m = 0 to l/s1
for n = 0 to l/s2
bnum = m + n - 1
if (m*s1 + n*s2 + bnum*b) == l)
print m, n, bnum
anand
  • 163
  • 7
0

First of all, look at your construction project differently:

  • You must start with a bulkhead.
  • Thereafter, each pipe must have a bulkhead after it.

Thus, you need to build a track of length l-b with pipes of length m+b and n+b.

Redistribute the factors, and you'll find that you have an equation in two variables, not 3:

(m+b)*s1 + (n+b)*s2 = l-b

Without loss of generality, assume s2 > s1. Your track will need at least (lower bound) this many pipe segments, enough of the longer one to reach at least the required track length.

ceil( (l-b) / (n+b) )

and no more (upper bound) than

floor( (l-b) / (m+b) )

You can brute-force an iterative solution:

  • solve the equation for s2
  • iterate s1 across the above range
  • for each value of s1, see whether s2 is an integer. If so, record the solution.

This is simple, but inelegant. In actuality, you'll want to use appropriate work on linear Diophantine equations to parameterize all solutions with s1, s2 >= 0.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • @RoryDaulton Thanks -- I started with one solution in which the two were interchangeable, and forgot to remove that when I found a better one based on the pipe lengths. – Prune Nov 19 '18 at 18:07