How to handle large Primes ( longer than 16 digits ) ?
How to handle large Primes ( longer than 16 digits ) ?
(OP)
I have written a simple QBASI program that checks if a number is Prime.
But the program cannot handle numbers larger than 16 digits.
Is there a way I can use QBASI to check for Primes greater than 16 digits long?
My program below:
10 REM Prime Tester 7B
20 COLOR 15, 0, 0
30 DIM P AS _FLOAT
40 DIM N AS _FLOAT
60 INPUT "Number"; N
65 START = TIMER
70 IF N <= 0 GOTO 999
75 IF N = 1 THEN PRINT "Sorry! 1 is in the Unitary Group": GOTO 20
80 FOR P = 2 TO SQR(N)
90 IF N MOD P = 0 THEN PRINT "Composite": GOTO 130
100 NEXT P
110 COLOR 31, 0, 0
120 PRINT "Prime"
130 ELAPSED = TIMER - START
140 COLOR 15, 0, 0
150 PRINT "elapsed time is"; ELAPSED, "seconds"
160 BEEP
170 GOTO 20
999 END
Your help appreciated.
Regards
pya
But the program cannot handle numbers larger than 16 digits.
Is there a way I can use QBASI to check for Primes greater than 16 digits long?
My program below:
10 REM Prime Tester 7B
20 COLOR 15, 0, 0
30 DIM P AS _FLOAT
40 DIM N AS _FLOAT
60 INPUT "Number"; N
65 START = TIMER
70 IF N <= 0 GOTO 999
75 IF N = 1 THEN PRINT "Sorry! 1 is in the Unitary Group": GOTO 20
80 FOR P = 2 TO SQR(N)
90 IF N MOD P = 0 THEN PRINT "Composite": GOTO 130
100 NEXT P
110 COLOR 31, 0, 0
120 PRINT "Prime"
130 ELAPSED = TIMER - START
140 COLOR 15, 0, 0
150 PRINT "elapsed time is"; ELAPSED, "seconds"
160 BEEP
170 GOTO 20
999 END
Your help appreciated.
Regards
pya
RE: How to handle large Primes ( longer than 16 digits ) ?
yes, read this : https://www.tek-tips.com/viewthread.cfm?qid=22099
RE: How to handle large Primes ( longer than 16 digits ) ?
Thanx for the reply.
Unfortunately, I am still learning QBASIC and had a hard time understanding all the code.
Is there any tutorial that will guide me through it.
Regards
pya
RE: How to handle large Primes ( longer than 16 digits ) ?
The idea of the link I mentioned, is to represent numbers as strings, but then you have to write your own functions like MOD() and SQRT() which you are using in your program, so that they work with large numerical strings.