2
$\begingroup$

I have a programming assignment that asks me to create a block that is built off of components a, b, c, d, and e. The ideal block will have:

  1. Equals amounts of "a" and "b".
  2. Equal amounts of "c" and "d".
  3. When "c" and "e" are added together, they will equal "a".

I must create a function that forces inputs a, b, c, d, e to follow the constraints above. So, for example, if the inputs to my function were: 20a, 25b, 10c, 15d, 3e...

Then, one of the possible output would be: 20a, 20b, 10c, 10d, 10e.

Another possible output would be: 25a, 25b, 10c, 10d, 15e.

What do you think is the best formula or general strategy I could employ to make this work? Think of edge-cases. For example, what is the best thing to do if "c" was larger than "a"? Since "c" and "e", when added, need to make up "a", this is important (assume no negative numbers allowed... there needs to be "something" of "e").

Keep in mind this is a programming project.

Thank you.

  • 0
    One could solve the equation straight-forwardly and weight the inputs to deduce which solution it should go to.2017-02-02
  • 0
    @SimplyBeautifulArt Yes, of course. I have a strategy that is straight-forward. But I was wondering if any of you more math-inclined folk could find, if there is one, a better strategy you would be willing to share. Thank you for your input.2017-02-02
  • 0
    @SimplyBeautifulArt thank you for letting me know. I upvoted before, but I received a message saying that if my reputation is below 15, I cannot upvote, or something similar. However, I am able to now :)2017-02-02

1 Answers 1

2

Here's an interesting one:

$a(n+1)=\frac{a(n)+c(n)+e(n)}2$

$b(n+1)=\frac{a(n)+3b(n)}4$

$c(n+1)=\frac{d(n)+3c(n)}4$

$d(n+1)=\frac{a(n)+d(n)-e(n)}2$

$e(n+1)=\frac{b(n)-c(n)+e(n)}2$

Note that when $n=0$, we use our initial values. As you keep looping this, it will approach a set of values that satisfy the problem without reaching the solution immediately.

  • 0
    Oooh! That's very interesting! How many times do you suggest I should loop? Assume the numbers don't exceed, say, 100.2017-02-02
  • 1
    @user3026388 I honestly don't know. After starting with your problem and doing 3 iterations by hand, I get $$\ $$ $a(3)=20.53125\\b(3)=21.046875\\c(3)=11.890625\\d(3)=9.6875\\e(3)=10.38$ $$\ $$so we're getting somewhere.2017-02-02
  • 0
    @user3026388 I too would like to see the end result, if it's not to much to ask.2017-02-02
  • 0
    of course. Once I get home, I can quickly program this function and output the results after, say, a thousand iterations. Also, why are the equations for "b" and "a" different?2017-02-02
  • 1
    @user3026388 you will notice I designed these specifically to avoid satisfying any of your conditions in one go. :-). Otherwise that's equal to solving straight-forwardly.2017-02-02
  • 0
    Okay, here is the output I got: If a = 25, b = 10, c = 50, d = 12, e = 5, after 1000 iterations, I get: a = 25.3784 b = 25.3784 c = 37.2703 d = 37.2703 e = -11.8919 Hmmm....2017-02-02
  • 0
    On the other hand, if: 1. a = 20, b = 25, c = 10, d = 15, e = 3, Then, after 1000 iterations, 2. a = 20.4324, b = 20.4324, c = 11.5946, d = 11.5946, e = 8.83784. So... it works, not for edge cases though. I am sure I can find a work around!2017-02-02
  • 0
    :D that's cool! Perhaps add five to every number of any are negative? :-)2017-02-02
  • 0
    that's exactly the solution I thought up! Haha, this is solid man!2017-02-02
  • 0
    I have to ask, what kind of sorcery did you employ to figure this formula out. What Imm saying is.. Teach me?2017-02-02
  • 0
    @user3026388 if it approaches something, then $a(n+1)\approx a(n)$, so $a(n)\approx\frac{a(n)+c(n)+e(n)}2$. Do the same for the others and solve2017-02-02
  • 0
    In the end, you should get the original conditions2017-02-02
  • 0
    If you wish to find me for informal chat, check the bottom of my profile description.2017-02-02
  • 0
    That makes sense :). I have one more question. How do you calculate the run-time for this? Big-O notation.2017-02-02
  • 1
    "O", I have no idea. Not a programmer. Sorry2017-02-02
  • 0
    No, that is fine, you have been more than enough help :) thank you!2017-02-02
  • 0
    Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/52949/discussion-between-user3026388-and-simply-beautiful-art).2017-02-02