Print the positive non-zero integer n-tuple(s) that solve an inequality within a bound

-3

0

Suppose I have a linear inequality like

x0A0 + x1A1 + ... + xnAn <= C

with xi a non-zero positive integer and Ai and C a positive non-zero multiple of 0.01. Find all the positive integer n-tuples {x1,...,xn} such that:

D <= x0A0 + x1A1 + ... + xnAn <= C

where D is also a positive non-zero multiple of 0.01.


Challenge: Find the shortest code to produce the n-tuple solution(s) for any Ai, C, D.


Example: Let A0,A1=1, C = 2.5, and D=1.5. This gives

1.5 <= x01+x11 <= 2.5

Here, the (only) 2-tuple solution is {1,1}:

enter image description here

two black lines in the middle

Posted 2018-03-14T06:45:09.580

Reputation: 127

5Why not simply multiply the equations by 100 and have everything in terms of integers ? The multiples of 0.01 don't add anything – Ton Hospel – 2018-03-14T07:13:48.393

1Welcome to PPCG! I think this challenge has potential but needs some clarifications. Do I understand correctly that the program takes C, D and a vector A as input and then should return all vectors X? Adding some examples and test cases would also help to understand the challenge. – Laikoni – 2018-03-14T09:31:47.137

1Also, what are the y and z vectors? – user202729 – 2018-03-14T09:34:13.217

1@user202729 He means other solution tuples, they aren't extra variables – Ton Hospel – 2018-03-14T12:00:32.457

@TonHospel, you can do that. – two black lines in the middle – 2018-03-14T17:28:52.760

@Laikoni, yes, exactly like you said, "the program takes C, D and a vector A as input and then should return all vectors X." – two black lines in the middle – 2018-03-14T17:29:21.290

@user202729, the X,Y,...,Z vectors are the possible multiple solutions. There may be just one. There may be 187. Depends on the input, which, as mentioned above, is C, D, and a vector A. – two black lines in the middle – 2018-03-14T17:31:44.447

1

This made enough sense to me. linkTIO

– Kelly Lowder – 2018-03-14T19:08:34.927

Answers

1

Haskell, 64 bytes

(d#c)a=[x|x<-mapM(\_->[1..c])a,y<-[sum$zipWith(*)x a],y>=d,y<=c]

Try it online! Defines a funktion (#) which takes D as first argument, C as second argument and A as a list as last argument and returns a list of possible X as lists. E.g. (#) 1.5 3.5 [1,1] yields the three solutions [[1.0,1.0],[1.0,2.0],[2.0,1.0]].


61 bytes with D and C integers:

(d#c)a=[x|x<-mapM(\_->[1..c])a,elem(sum$zipWith(*)x a)[d..c]]

Try it online! E.g. (#) 2 5 [1,1,2] yields [[1,1,1],[1,2,1],[2,1,1]].

Laikoni

Posted 2018-03-14T06:45:09.580

Reputation: 23 676

How would you print the value that the tuples produce? – two black lines in the middle – 2018-03-15T08:40:27.250

@twoblacklinesinthemiddle I miss read your question before. In the first program the resulting value for a tuple x is y, so you can return (x,y) instead of just x: Try it online!

– Laikoni – 2018-03-15T09:21:22.137