0

I'd like to ask:

  • I've got any positive integer number, let's say: 10
  • Then I got random integer numbers (random count of them too), let's say: 5, 7, 8
  • I want to calculate floating-point number such as: 5*x + 7*x + 8*x = 10
  • I got that a this moment, but I want to have it common lower boundary limit (let's say 3), such as:

5 * x > 3
7 * x > 3
8 * x > 3

  • It is possible to correct the numbers (ratios) in order to achieve this state, so in this particular example it will be like this:
sum = 5 + 7 + 8 = 20
x = 10 (total) / 20 (sum)
x = 0.5

results: 5*0.5 + 7*0.5 + 8*0.5 = 2.5 + 3.5 + 4 = 10 (total)

So far so good, but 2.5 is now lower then a given min. limit 3, so how to solve this!

Is it possible, if so, how? (in C# preferably)

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
SmartK8
  • 2,616
  • 1
  • 29
  • 37
  • er no, i think it wont give anything like that because it cant :S if you decide x is 0.5 then 5 times that will never be greater than 3 :S if you want it so that the x for 5 would increase to be greater than 3 then another number would have to decrease to compensate for the rise, in which case, you arent using 5x 7x 8x you are using 5x 7y 8z... – RhysW May 10 '12 at 15:02
  • if the numbers arent all the same you cant call them all x basically – RhysW May 10 '12 at 15:03
  • I guessed, that it would be possible in one pass, but I got deadline, and this bug emerged at the last moment, I can't play with this problem (as I would normally did). How to solve this? Even with somehow doing approximation in each pass. – SmartK8 May 10 '12 at 15:04
  • for example, you would need to increase x by 0.1 for 5x to make it 5x = 3, so either the 8 or the seven would have to decrease, in which case, you couldnt take 0.1 off of the 8's x, becaus eits then 0.3 short of the right answer, you would have to do the increase in 5's x, times 5, divided by 8, or 7, then take that away from the x of that number – RhysW May 10 '12 at 15:05
  • OK, but how to divide that total (10) by given numbers. Even though they can be pushed around (they're pseudo-random anyway). – SmartK8 May 10 '12 at 15:07
  • @SmartK8: I think you need to define what you're looking for. The way you've asked this question, it sounds like you're trying to solve an algebraically impossible equation (`5*x + 7*x + 8*x = 10 | x > 3`). What are you really asking for? – StriplingWarrior May 10 '12 at 15:07
  • I know what to do in my mind, but how to do it generally as an algorithm? Or are you only thinking aloud? – SmartK8 May 10 '12 at 15:08
  • have it run the algorith as you have it, then make it increase 5's x to make its answer greater than 3, now do the new number of 5's x take away the original number of 5's x (to find the total increase in x) and divide that by 5 (to find the single increase in x) then do that single one times one of the other numbers, e.g 7. and take this number away from 7's x so thats its linearly scaled down in a ratio of 5/7ths to still give the right answer – RhysW May 10 '12 at 15:10
  • SW: I'm generating CDRs. I have to generate pseudo-random data transfer for a customer per month. I need N events givin total amount of transfered data X (in kB). For example: 10 events per month, 1-10 kB each giving total 30 kB. So I've generated random numbers, divided in ratio, but it now gives me an occasional 0kB (in some events). This can't be. – SmartK8 May 10 '12 at 15:11
  • smartk my coment above yours explains the math behind changing the ratios, so that one x increases and another decreases to allow each to be over 3, and still meet the answer – RhysW May 10 '12 at 15:17
  • RhysW: I'm trying to 'get' it. My initial idea was to count problematic values (violating the lower limit constraint), make them fixed, and divide the rest of the total, so to divide: total-fixed amount. But I don't know how to unite the overall factor (if it's even possible). I guess I can make it iterating. – SmartK8 May 10 '12 at 15:18
  • sorry its a bit, logic heavy, but basically, the x for 5 will increase by a factor of 5, so the x of 7 must decrease by a factor of 7, otherwise the increase in x in one place without a decrease in another place would give the wrong answer – RhysW May 10 '12 at 15:20
  • RhysW: I see what you're getting at. But I still don't know how to "redistribute" the error, while maintaining the factor (once again, if this is even possible). – SmartK8 May 10 '12 at 15:23
  • I could fix the ratio for the violations (as you said). – SmartK8 May 10 '12 at 15:25
  • ok i completely bombed through this and gave a super explanatory actual alswer for you! i sincerely hope it helps! (btw i realised the mistake i made int he logic of the comments, i correct my mistake in the answer) – RhysW May 10 '12 at 15:30

3 Answers3

1

Let us take your example and see how WE would solve it.

i shall change the use of 5x 7x 8x to the use of 5x 7y 8z because its already been decided that these MUST change to fit the requirements and therefore will not all be the same.

So currently you get a total answer of 10 by having 

5 times 0.5 = 2.5
7 times 0.5 = 3.5
8 times 0.5 = 4

however, 5 times 0.5 is NOT greater than 3

therefore to make 5 times x atleast 3 we must increase the total by 0.5

as 3(the number you atleast want) - 2.5(the number you have) is 0.5

so the TOTAL must increase by 0.5.

as 5 times x must = 3 we can see that x must = 3 divided by 5 which gives us 0.6

now lets recalculate your sum

5 times 0.6 = 3
7 tiems 0.5 = 3.5
8 times 0.5 = 4

all together = 10.5...ah, balls.

ok so we can see that y or z must be made smaller to make sure that the answer is still exactly 10

so lets pick 8 times z = 4 (as 4 is the furthers from 3 and will give us the most lee way)

0.5 divided by 8 = 0.0625

so z must decrease by 0.0625 to counteract the 0.5 increase from 5 times x

so z now = 0.5 - 0.0625 which is 0.4375

lets redo your sum with these new numbers!

5 times 0.6 = 3
7 times 0.5 = 3.5
8 times  0.4375 = 3.5

3 plus 3.5 plus 3.5 = 10! we nailed it! yaaay

i hope this helped! if not well i enjoyed doing it anyway :D

To summarise, we had to increase x so that 5x was atleast 3, but the increase in x meant the answer was greater than 10, therefore either y or z had to decrease by whatver the total increasewas to counteract this, we do that increase divided by one of the other numbers, to figure out how much we need to subtract from z or y to make it = the perfect 10 again

RhysW
  • 455
  • 1
  • 4
  • 16
  • OK, sounds working. But I'm trying to grasp it, and forge it into a algorithm. – SmartK8 May 10 '12 at 15:39
  • make it do it how you do it now, then after its worked it out, see if any are below 3, if they are make it bigger, see how much the total goes up when youve made it bigger, divide this total increase by the biggest number, in this case, 8, and subtract that from whatever 8 is being timesed by, – RhysW May 10 '12 at 15:40
  • What if, 8 times z would result in a < 3 state. Even though it was the most far away. Because this is one example, but in reality it will be hundreds, of numbers. – SmartK8 May 10 '12 at 15:41
  • worry about that after, check what 5x is, if its below 3 then lower what 8z is, if this then goes below 3, then make 8z increase marginally and decrease 7y, if at this point 7y is below 3 you know yu have stumbled across an impossibble sum (as youll go round forever increasing and decreasing) so youll need to throw an escape route where after its been forced back to changing 7y and its below 3 then show a message saying impossibble sum encountered, aborting – RhysW May 10 '12 at 15:42
  • The 'error' distribution to one number is still not it IMO. It would be better to distribute it to all other non-3s, but then I would have to make a check again. – SmartK8 May 10 '12 at 15:43
  • it will be more efficient to just change one number its mathmatically impossibble to change x to be the same for all of them, and = the answer, and all be above 3 for every scenario – RhysW May 10 '12 at 15:44
  • @smartK8 good luck! im off for the day in about 5 minutes, so i hope you get it working! – RhysW May 10 '12 at 15:45
  • Thanks, I must get it working unfortunately :) See you around. – SmartK8 May 10 '12 at 15:46
  • In the end, I did what you've suggested but instead of pushing factors, I generated actual numbers first (number*factor=actual). Then I summed them up and compared to requested total. That's how I gained 'error' (total-sum=error). And then I'm pushing by +1/-1 to all numbers that are within bounds (event after addition/subtraction) until the error is gone, or alternatively no pushing is possible. – SmartK8 May 10 '12 at 17:11
  • @SmartK8 if it works it works! glad i could help! feel free to ask about any other math if need be, i love a good ol math challenge :D – RhysW May 11 '12 at 07:42
0

YOur requirements are contradicting. It is impossible to guarantee that 5*x + 7*x + 8*x = 10 in the same time with 5*x > 3 if x is single value. If you can use 5*x + 7*y + 8*z = 10, then it becomes possible, if you have 3*value_count < 10 but is this other task I suppose

Sasha
  • 8,537
  • 4
  • 49
  • 76
  • 5 7 and 8 arent set in stone, they are random, she also doesnt mean x x x she technically means x y z as one x should shrink and another grow to accomidate incorrections where Nx is not greater than 3 – RhysW May 10 '12 at 15:16
  • Check the question thread, I've reiterated the actual problem. – SmartK8 May 10 '12 at 15:16
  • RhysW: Exactly, basically I need given count of random numbers in a given range with minimal value giving fixed total. – SmartK8 May 10 '12 at 15:17
  • there will however ALWAYS be some cases where it just wont work, where every x would be below the minimal required and there is no way to increase it without changing the answer or the random values given, its a mathmatical certainty that not every equation of this type is solveable – RhysW May 10 '12 at 15:19
  • It was a possibility, but maybe I should take step back. And consider the algorithm for generating the numbers. Maybe there's a way to generate them somehow better, or I can throw away the factor, and just generate actual values. I'm sorry that this problem is kind of evolving in time, but as I said time is ticking for me. :) – SmartK8 May 10 '12 at 15:31
  • @SmartK8 gave you a very comprehensive answer that shows how the x values must fluctuate to accomidate change – RhysW May 10 '12 at 15:36
0

seems you have to solve a task first, only then code it. Let's see your given conditions All your numbers mupltipled to X must be larger then 3, that's why max count of them should be 6, because 7 * 3 is already 21, which is larger then 20. Second, depending on count of your number, we have to calculate minimal value of numbers. If it is 6, we have minimal value rest of division 20 to 6 = 3, if it is 5 then we have 20 % 5 = 4. Only on this two conditions we can calculate "X".

Johnny_D
  • 4,592
  • 3
  • 33
  • 63
  • its not 7*3 its 7 times x has to be greater than 3 It isnt N*3 must be less than the answer, its N*x must be greater than 3 – RhysW May 10 '12 at 15:13