20051212, 01:00  #1 
Dec 2005
5_{16} 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 LucasLehmer 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 
20051212, 01:39  #2  
Jun 2005
15_{10} Posts 
Quote:
Craig. 

20051212, 03:33  #3 
Sep 2002
1226_{8} 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. 
20051212, 04:07  #4 
Jun 2003
Russia, Novosibirsk
2×107 Posts 
Your code overflows to 2^311 (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

20051212, 23:12  #5 
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.

20051213, 20:57  #6 
Sep 2002
2·331 Posts 
Here is a sample in UBasic it will do the LL LucasLehmer algorithm
with the prime exponent entered. Code:
1000 P=7 1010 input "Enter an odd prime (example 7) : ";P 1020 N=2^P1 1030 J=1 1040 S=4 1050 J=2 1060 for I=J to P1 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." 1020 N=2^P1: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 
20051214, 23:41  #7 
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 WHILEWEND loop was giving me all sorts of grief, as well as the GOTO command that I had in the Ifthen 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^P1 1030 S=4 1040 J=2 1050 for I=J to P1 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 EDIT2: Tested code, revised, yay. Last fiddled with by AJ_Letson on 20051214 at 23:54 Reason: Revised code 
20051215, 02:57  #8 
Dec 2005
5 Posts 
Well, unfortunately UBasic overflows after 2^42531. 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^P1 960 S=4 970 J=2 980 for I=J to P1 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^42531, as I said earlier; anything I can do to fix this without going into a new dialect? 
20051215, 05:17  #9 
Sep 2002
1226_{8} Posts 
That is a likely limit on the size of 2^p  1 with UBasic.
It does have the isqrt() function. 
20051216, 03:05  #10 
Dec 2005
101_{2} 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 20051216 at 03:05 