Programming methods to find a sum.

In summary: As you add values, test to see if the sum of the values is still equal to the limit you set. If it is not, add another element to the array and retest. In summary, the programmer is trying to find a sum closest to a value X, between value X and Y. They are using VBA for quick testing which will be put into a VB.Net application. The problem is that the math is not modular and may unnecessarily perform many calculations. They need either an efficient way to loop through common values (ergo, the sum value of 0, 0, 1 shouldn't be recalculated later as 0, 1, 0 or 1, 0, 0) or a simple mathematical approach to finding
  • #1
'Tex
1
0
I need to take input values, and in the most efficient manner possible find a sum closest to a value X, between value X and Y.

The number of input values is variable, and all input is negative. X doesn't have to be lower than Y, and vice versa. That is, X can be -8388608 or -(2^23) and Y can be 0, or X can be -8388609 and Y can be -16777216 or -(2^24).

I am using VBA for quick testing which will be put into a VB.Net application.


Remember, efficiency... the number of tests that would have to be performed via loops would range from around 32768 to well over 16777216 (32^3 and 256^3, respectively)...



The values inputted will be applied to an array. The input may occur as "45" and "63" and mean 45 through 63 as input values, but the array that is calculated, Array(45) maybe -8388600 or some other negative number.


Because of the huge number of calculations that may unnecessarily performed, I need either an efficient way to loop through common values (ergo, the sum value of 0, 0, 1 shouldn't be recalculated later as as 0, 1, 0 or 1, 0, 0) or a simple mathematical approach to finding a sum of appropriate value.

This help would be greatly appreciated. Thank you.



The arithmatic is usually modular, and related to level (game is Diablo 2, although this is the hacking side of it involving 1.09d and extraordinarily large numbers.)

For example, the BoCL value 8 for level X is Level * 8 / x

The math is modular, and signed, for -8,388,608 to positive 8,388,607. So having two BoCL(8) is the same as BoCL(16), but I am using a derivative arithmatic in the game that only applies to the negative values. The sum of these values must be negative, and is not modular. Moreover, let's say I have the values:

10, 10, 0

Well, that isn't BoCL(20) but rather the sum of the modular values for BoCL(10) and BoCL(10). If BoCL(10) = -8388608, then (10, 10, 0) is -16,777,216.

This presents a unique problem, as I cannot simply perform a loop check on the values 1 to X, X being the highest possible value times three. Let's say it's 63 normally.

(63, 0, 0) is not necessarily equal to (32, 31, 0) and thus any operation that derives the answer must take that into account, it cannot simply do BoCL(1) through BoCL(189).



Edit: An efficient implementation could also occur so that the values BoCL(1) to BoCL(Limit * 3)... such that you could take the modular sum you want, say, -8388609, and specify the closest value to 8388607 (that value taken modular). So you could specify that if 8388607 is BoCL(100) then any number of negative values that sum to 100, such as BoCL(25) + BoCL(25) + BoCL(50) would sum to -8388609. Does this make sense?

Code:
BoCL	Value
22	-8126464
23	-7733248
24	-7340032
25	-6946816
26	-6553600
27	-6160384
28	-5767168
29	-5373952
30	-4980736
31	-4587520
32	-4194304
33	-3801088
34	-3407872
35	-3014656
36	-2621440
37	-2228224
38	-1835008
39	-1441792
40	-1048576
41	-655360
42	-262144
43	131072
44	524288
45	917504
46	1310720
47	1703936
48	2097152
49	2490368
50	2883584
51	3276800
52	3670016
53	4063232
54	4456448
55	4849664
56	5242880
57	5636096
58	6029312
59	6422528
60	6815744
61	7208960
62	7602176
63	7995392

So, for the sake of simplicity using 63 as the new limit just for ease... you can say that any two negative values that sum to the value 63, will have a value equal to the negative 63 which is -8,781,824. And this can be tested, using the values 31 and 32, which are -4,587,520, and -4,194,304 respectively.

However, there will be impossible values, such as the values of BoCL(1) to BoCL(21), as that range is positive there are no negative sums for 1 to 21.
 
Last edited:
Computer science news on Phys.org
  • #2
You might find a library function SUM(), that will do the looping as efficiently as possible on your hardware. But it will probably not handle all your special cases.

Just go ahead and do the loops, add one element at a time.
 

1. How do you find the sum of a set of numbers using programming?

There are several programming methods to find the sum of a set of numbers, including using a loop to iterate through the numbers and add them together, using built-in functions like sum(), or creating a custom function to calculate the sum.

2. What is the most efficient programming method for finding a sum?

The most efficient method for finding a sum will depend on the specific programming language and the size of the numbers being added. Generally, built-in functions like sum() or reduce() tend to be more efficient for larger sets of numbers, while loops may be more efficient for smaller sets.

3. How do you handle negative numbers when finding a sum with programming?

Negative numbers can be handled in the same way as positive numbers when finding a sum with programming. You can simply add them to the total sum, as the negative sign will be taken into account during the calculation.

4. Can programming methods to find a sum be used for non-numeric data?

No, programming methods for finding a sum are typically designed for numerical data and may not work for non-numeric data. However, some programming languages may have functions or methods that can be used for other types of data.

5. Is there a limit to the number of numbers that can be added using programming?

The limit for the number of numbers that can be added using programming will depend on the specific programming language and the size of the numbers being added. However, most programming languages can handle large numbers and do not have a strict limit on the number of values that can be added.

Similar threads

  • Programming and Computer Science
Replies
1
Views
659
  • Engineering and Comp Sci Homework Help
Replies
8
Views
950
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Quantum Physics
Replies
3
Views
905
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
938
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
944
  • Programming and Computer Science
Replies
2
Views
854
Back
Top