dNY Posted December 5, 2022 Share Posted December 5, 2022 Hello! If I have, let's say 1000 numbers or 10k numbers, and they all add up to 100 when combined, what would be the most efficient way to verify that in fact the sum of all of them is 100? I was thinking of a Merkle Tree, but I'm not really sure how to apply it in this case. I could just do n + n1 + n2 + n3....+ n10k, but that's not really scalable nor efficient. Thanks beforehand for any help. Link to comment Share on other sites More sharing options...

Sensei Posted December 6, 2022 Share Posted December 6, 2022 (edited) Can you use multi-threading? Can you use MMX/SSE/AVX? https://www.google.com/search?q=sse+array+sum https://www.google.com/search?q=avx+array+sum Can you use GPU? What do you know about these numbers, i.e., are they only positive ("unsigned"), or positive and negative too ("signed")? What is resolution of these numbers? i.e. 8, 16, 32, 64 bits, or more? What do you know about the array, i.e., is it sorted or not? f.e. if you know that they are all positives, you can stop counting as soon as 100 is exceeded.. f.e. if you know that they are all positives, and array is sorted, you can stop as soon as counting from last to first exceeds 100. Notice that summing 1000 numbers with 32-bit signed/unsigned resolution will require a 64-bit accumulator.. // This is the wrong code! int data[ 1000 ] = { ... initialized... }; int accumulator = 0; for( int i = 0; i < 1000; i++ ) { accumulator += data[ i ]; } By mistake, the resolution of the accumulator can be easily exceeded (overflow, easy to handle in assembler, hard to handle in higher level languages). Edited December 6, 2022 by Sensei Link to comment Share on other sites More sharing options...

WalidKhan Posted January 20, 2023 Share Posted January 20, 2023 If you have a large number of numbers that need to be verified to add up to a specific value, one efficient way to accomplish this would be to use a running sum algorithm. The basic idea behind this algorithm is to keep a running total of the numbers as you iterate through them. You start with an initial value (in this case, 0) and add each number to the running total. At the end of the iteration, you check if the running total is equal to the expected sum (in this case, 100). Here is an example of how this algorithm can be implemented in JavaScript: function verifySum(numbers, expectedSum) { let runningTotal = 0; for (let i = 0; i < numbers.length; i++) { runningTotal += numbers[i]; } return runningTotal === expectedSum; } This algorithm is efficient because it only needs to iterate through the numbers once and it only needs to keep track of one variable (the running total). This makes it a linear time complexity algorithm and it's relatively fast even when working with large numbers. Another approach to achieve this is using Hash algorithms like SHA-256, you can hash all the numbers and then concatenate them and hash the result again, and it will be a hash that represents the sum of all the numbers. Both approaches are very efficient in terms of computational time and memory usage, and they can be easily scaled to large numbers. I hope this helps! Let me know if you have any other questions. Link to comment Share on other sites More sharing options...

## Recommended Posts

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account## Sign in

Already have an account? Sign in here.

Sign In Now