mersenneforum.org QuickBasic help?
 Register FAQ Search Today's Posts Mark Forums Read

 2005-12-12, 01:00 #1 AJ_Letson   Dec 2005 516 Posts QuickBasic help? In QuickBasic 4.5, I've run into a small coding dilemna. I can't figure out what's wrong with my coding for the Lucas-Lehmer test. Code: p = 3 10 p = p + 2 s = 4 m = (2 ^ p) - 1 FOR i = 3 TO p s = s * s s = s - 2 WHILE s >= m s = s - ((s / m) * m) WEND NEXT i IF s = 0 THEN PRINT p END IF GOTO 10 END The program gives me an overflow error at the line "s = s * s", and it gave me the same problem with the "mod" function. Any suggestions?
2005-12-12, 01:39   #2
cjohnsto

Jun 2005

1510 Posts

Quote:
 Originally Posted by AJ_Letson The program gives me an overflow error at the line "s = s * s", and it gave me the same problem with the "mod" function. Any suggestions?
I'm not familiar with QuickBasic but my guess is that 's' is only 32 bits long and your result is longer than 32 bits. You will need to find a more appropriate data type or create one yourself.

Craig.

 2005-12-12, 03:33 #3 dsouza123     Sep 2002 12268 Posts A 32 bit integer gives 9 to 10 digits, a double 15 to 16 digits. UBasic which is available online supports integers with 2600 digits. It will allow you to test bigger numbers.
 2005-12-12, 04:07 #4 HiddenWarrior     Jun 2003 Russia, Novosibirsk 2×107 Posts Your code overflows to 2^31-1 (long type maximal value). If you want to work with bigger numbers, you can try my longmath routines. They coded in Visual Basic 6.0 but can be simply recoded to QBasic. You can find them in code samples at www.vbnet.ru
 2005-12-12, 23:12 #5 AJ_Letson   Dec 2005 5 Posts Okay. Does UBasic have essentially the same command set as QuickBasic? Also, I have no idea how to recode from VB to QuickBasic having never worked with VB.
 2005-12-13, 20:57 #6 dsouza123     Sep 2002 2·331 Posts Here is a sample in UBasic it will do the LL Lucas-Lehmer algorithm with the prime exponent entered. Code: 1000 P=7 1010 input "Enter an odd prime (example 7) : ";P 1020 N=2^P-1 1030 J=1 1040 S=4 1050 J=2 1060 for I=J to P-1 1070 S=(S*S)-2 1080 S=S@N 1090 next I 1100 print "2^";P;"-1 is "; 1110 if S=0 then print "prime." else print "composite." line 1020 could have had the contents of the three following lines. 1020 N=2^P-1:J=1:S=4:J=2 the : allows multiple statements on a line. the @ is the mod operator lines 1000 and 1030 aren't needed just to follow the prime95 help file example To save a UBasic program do ASAVE "Filename.ub" To exit UBasic do system
 2005-12-14, 23:41 #7 AJ_Letson   Dec 2005 5 Posts Thank you muchly; I couldn't figure out the mod function in UBasic. That was the one part that gave me troubles; the WHILE-WEND loop was giving me all sorts of grief, as well as the GOTO command that I had in the If-then loop. Also, to modify your code for the rest of the people who enjoy ancient programming languages (like me!! ) Code: 1000 P=3 1010 P=P+2 1020 N=2^P-1 1030 S=4 1040 J=2 1050 for I=J to P-1 1060 S=(S*S)-2 1070 S=S@N 1080 next I 1090 if S=0 then print "2^";P;"-1 is prime." 1100 goto 1010 This should work, but I'm not sure about it since I've been away from this particular dialect for years- seems a lot like the BASIC editor on the old B/W IBMs. EDIT2: Tested code, revised, yay. Last fiddled with by AJ_Letson on 2005-12-14 at 23:54 Reason: Revised code
 2005-12-15, 02:57 #8 AJ_Letson   Dec 2005 5 Posts Well, unfortunately UBasic overflows after 2^4253-1. However, improved code: Code: 900 P=3 910 P=P+2 920 for K=3 to int(sqrt(P)) 'Is sqrt( correct? 930 if (P/K)=int(P/K) then 910 'this bit is efficient after p=25ish i think 940 next K 950 N=2^P-1 960 S=4 970 J=2 980 for I=J to P-1 990 S=(S*S)-2 1000 S=S@N 1010 next I 1020 rem are gotos in Ubasic illegal from inside for nexts? if not then next line should go before 1010 e.g. 1021 rem same as 1030 1022 rem if S=0 then 910 1030 if S=0 then print "2^";P;"-1 is prime." 1040 goto 910 Unfortunately UBasic overflows after 2^4253-1, as I said earlier; anything I can do to fix this without going into a new dialect?
 2005-12-15, 05:17 #9 dsouza123     Sep 2002 12268 Posts That is a likely limit on the size of 2^p - 1 with UBasic. It does have the isqrt() function.
 2005-12-16, 03:05 #10 AJ_Letson   Dec 2005 1012 Posts Well, it doesn't matter anymore; I've moved on to bigger and better things. (read: C++) However, to improve efficiency, you can stick an int(sqrt( test to test P for primality before going into the LL test itself (See 930 in previous code) Last fiddled with by AJ_Letson on 2005-12-16 at 03:05

All times are UTC. The time now is 09:12.

Sun Dec 5 09:12:25 UTC 2021 up 135 days, 3:41, 0 users, load averages: 1.79, 1.95, 1.77