05-08-2025, 01:39 PM
Another trick for speed would be to adjust your upper limit as you go
To start, you know 1 and 2 are going to work (as all the numbers you are checking are even numbers.) This gives you an upper limit of (number /2 - 1), which can also go into the total.
StartingTotal = (1 + 2 + number /2)
endPoint = (number/2 -1) 'no need to check past this number as we know it's already there.
startPoint = 3 'no need to check 1 or two as they're already assumed to be in there
Then while running the routine, update the endPoint with each match you make
For example with 24, you start knowing this: 1 + 2 + 12
You now check the values from 3 to 11.
When you find 3 is a match, that also gives you 8. (3 * 8) = 24. Your total is now 1 + 2 + 3 + 8 + 12 = 26.
At this point, your total is now greater than 24. You don't need to check any further! It's not a match!
IF the total had been less than 24, you still gained valuable information there, as your endpoint is now 7. You've already found 8 and 12 as factors, so there's no reason to look that high any longer. Instead of a loop from 3 to 11, it's now limited to a range from 3 to 7...
Each value you find would reduce the endpoint of what you need to check for, saving you a ton of loops and speeding the process up considerably.
To start, you know 1 and 2 are going to work (as all the numbers you are checking are even numbers.) This gives you an upper limit of (number /2 - 1), which can also go into the total.
StartingTotal = (1 + 2 + number /2)
endPoint = (number/2 -1) 'no need to check past this number as we know it's already there.
startPoint = 3 'no need to check 1 or two as they're already assumed to be in there
Then while running the routine, update the endPoint with each match you make
For example with 24, you start knowing this: 1 + 2 + 12
You now check the values from 3 to 11.
When you find 3 is a match, that also gives you 8. (3 * 8) = 24. Your total is now 1 + 2 + 3 + 8 + 12 = 26.
At this point, your total is now greater than 24. You don't need to check any further! It's not a match!
IF the total had been less than 24, you still gained valuable information there, as your endpoint is now 7. You've already found 8 and 12 as factors, so there's no reason to look that high any longer. Instead of a loop from 3 to 11, it's now limited to a range from 3 to 7...
Each value you find would reduce the endpoint of what you need to check for, saving you a ton of loops and speeding the process up considerably.