![]() |
|
Exiting FOR NEXT, maybe a bug? Version 4.1.0 on Linux - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: Exiting FOR NEXT, maybe a bug? Version 4.1.0 on Linux (/showthread.php?tid=3673) |
Exiting FOR NEXT, maybe a bug? Version 4.1.0 on Linux - Circlotron - 05-08-2025 Had a go at calculating some Perfect Numbers. If I EXIT the FOR NEXT and print the number and the sum of it's factors (which should be equal) it works just fine. (After the first four it might take forever because it is a big number.) But if I print the number and the sum of it's factors just before I EXIT the FOR NEXT as well as after then I get several extra unrelated numbers. The line that causes the issue is commented out, but put it in and things go bad. I would have thought that it made no difference except for printing the numbers twice. What's going on? Code: (Select All)
RE: Exiting FOR NEXT, maybe a bug? Version 4.1.0 on Linux - SMcNeill - 05-08-2025 (05-08-2025, 12:49 PM)Circlotron Wrote: Had a go at calculating some Perfect Numbers. If I EXIT the FOR NEXT and print the number and the sum of it's factors (which should be equal) it works just fine. (After the first four it might take forever because it is a big number.) But if I print the number and the sum of it's factors just before I EXIT the FOR NEXT as well as after then I get several extra unrelated numbers. The line that causes the issue is commented out, but put it in and things go bad. I would have thought that it made no difference except for printing the numbers twice. What's going on? False positives found by not adding all the numbers up completely. For example, you're getting 24 as a result. Its factors are 1, 2, 3, 4, 6, 8, 12. 1 + 2 + 3 + 4 + 6 + 8 = 24 <-- Inside the loop, it would see this as being a FALSE positive result. By letting the loop run completely, it also adds that 12 and gets: 1 + 2 + 3 + 4 + 6 + 8 + 12 = 36, which is NOT a perfect number of 24. If you want an early exit condition, check for if the div_total > number, then you can stop checking any further as you know it's not going to work. RE: Exiting FOR NEXT, maybe a bug? Version 4.1.0 on Linux - SMcNeill - 05-08-2025 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. RE: Exiting FOR NEXT, maybe a bug? Version 4.1.0 on Linux - SMcNeill - 05-08-2025 Code: (Select All)
That's about the best I can see off the top of my head for speeding things up, and that's still not fast, but it's doing a lot of math each pass like this. RE: Exiting FOR NEXT, maybe a bug? Version 4.1.0 on Linux - Circlotron - 05-09-2025 Oh yeah, I get it now. Not letting the loop try every relevant trial value. That End if looks useful too. Makes things a lot tidier. Only just getting back into BASIC after a 25 year hiatus and there are a few things I had forgotten. Look back at some old code and wonder however did I manage that?! |