lawndude
08-13-2009, 07:16 AM
This program finds the prime components in a number. It works great except for the number 111111111 on which it hangs. 111111110, 111111112, etc, works fine. Can someone tell me what is going on?
Logic error in VB6 programlawndude 08-13-2009, 07:16 AM This program finds the prime components in a number. It works great except for the number 111111111 on which it hangs. 111111110, 111111112, etc, works fine. Can someone tell me what is going on? emartinho 08-13-2009, 08:19 AM Hi, Not sure what you're program intends to do, have you tried to debug the program by hitting CTRL-BREAK every once in a while to check the values or using breakpoints? From what I can gather it reduces the upper bound of the FOR loop every time the the counter is a number that divides evenly into the entered number. For the other examples you cite, there are multiple divisors, however for 111111111 there are only 5 and the largest is 333. You can use this http://www.farfarfar.com/math/calculators/factoring/ then select "all factors" to see how few divisors there are for 111111111. So we can see that after division by 333, the upper bound of the for loop is 333667 and so the loop will need to run from 333 until it reaches 333667, that's a lot of looping when you factor in the other 2 loops it's running! :D Hope this helps. -EM. Cerian Knight 08-15-2009, 09:29 PM I agree with emartinho. I calculated that it would require hours to finish using '111111111' as a seed, even after I had optimized some of your code: Denominator_Prime = True I = 2 Do While I < Counter If Counter Mod I Then I = I + 1 Else Denominator_Prime = False: Exit Do 'bottleneck is here with 'Mod' Loop Component_Counter = 0 Do While Number Mod Counter = 0 'Mod here is less of an issue Number = Number \ Counter 'do integer division is faster Component_Counter = Component_Counter + 1 Loop The most notable issue with performance is that with '111111111' you are spending most of the time in a loop that repetitively uses the 'Mod' operator, which is a notoriously slow operator. To avoid this, perhaps a whole new approach will be required, but I wouldn't discount the possibility that there is some other way to significantly improve the speed of that loop. In any case, prime factors have been discussed often here. Here is but one example (not necessarily the best) I found, which I have tested: http://www.xtremevbtalk.com/showthread.php?t=96219 But you had better have about 500MB of free RAM before you plug in '111111111'. I didn't...but was able to verify the output matches your program using the smaller number '11111111'. |
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum