-3
\$\begingroup\$

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

\$\endgroup\$
  • 5
    \$\begingroup\$ Why not simply multiply the equations by 100 and have everything in terms of integers ? The multiples of 0.01 don't add anything \$\endgroup\$ – Ton Hospel Mar 14 '18 at 7:13
  • 1
    \$\begingroup\$ Welcome 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. \$\endgroup\$ – Laikoni Mar 14 '18 at 9:31
  • 1
    \$\begingroup\$ Also, what are the y and z vectors? \$\endgroup\$ – user202729 Mar 14 '18 at 9:34
  • 1
    \$\begingroup\$ @user202729 He means other solution tuples, they aren't extra variables \$\endgroup\$ – Ton Hospel Mar 14 '18 at 12:00
  • 1
    \$\begingroup\$ This made enough sense to me. linkTIO \$\endgroup\$ – Kelly Lowder Mar 14 '18 at 19:08
1
\$\begingroup\$

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]].

\$\endgroup\$
  • \$\begingroup\$ How would you print the value that the tuples produce? \$\endgroup\$ – two black lines in the middle Mar 15 '18 at 8:40
  • \$\begingroup\$ @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! \$\endgroup\$ – Laikoni Mar 15 '18 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.