Qbasicnews.com

Full Version: The PRIME NUMBERS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7
Code:
CLS
DIM prime(1 TO 5000) AS INTEGER, cur.prime.amount AS INTEGER
cur.prime.amount% = 1
prime%(1) = 2
i% = 3
1 j% = 0
sqrt.num% = SQR(i%)
2 j% = j% + 1
temp% = prime%(j%)
IF i% MOD temp% = 0 THEN GOTO 4
IF temp% >= sqrt.num% THEN GOTO 3
GOTO 2
3 cur.prime.amount% = cur.prime.amount% + 1
prime%(cur.prime.amount%) = i%
4 IF i% = 32767 THEN GOTO 5
i% = i% + 2
GOTO 1
5 i2& = prime(cur.prime.amount%)
i2& = i2& + 2
biggest.prime& = i2&
i3# = TIMER + 1
6 j% = 1
sqrt.num% = SQR(i2&)
7 temp% = prime(j%)
IF i2& MOD temp% = 0 THEN GOTO 9
IF temp% >= sqrt.num% THEN GOTO 8
j% = j% + 1
GOTO 7
8 biggest.prime& = i2&
9 i2& = i2& + 2
IF TIMER > i3# THEN i3# = TIMER + 1: PRINT biggest.prime&
GOTO 6
'IF INKEY$ = "" THEN GOTO 6
Well, i've always thought french kissing is better than regular kissing, so the possibility's certainly there... But then again, American cheese kicks ass, what with Wisconsin and all so I can't really say now.
Nice job Agamemnus!!

I learned a bunch from your optimization of my code...Here's what I see from a quick look

goto faster than do:loop
putting an array element into a variable then calling that variable is faster than calling the array element repetedly
a^.5 is faster than squ(a)

any others that I missed? Thanks again for the optimization.
Agamemnus: You should review your code, some of the numbers printed end in 5 so they can't be primes...
Actually, SQR(x) is 2x faster than ^.5. I think that's because ^x uses a general method, and SQR(x) is a streamlined version of ^x, with a lot of things canceling out.

Your code is slower because:

1) You have a while loop for your timer
2) You use UNTIL instead of direct IFs...

Of course a good compiler will make ^.5 into SQR(x). But qb45.exe isn't actually that smart..


EDIT: You're right, I fouled up.
EDIT: Fixed...
I like my program that finds primes up to the number specified

Code:
CLS
INPUT n
FOR i = 1 TO n
a = i * 6 - 1
b = i * 6 + 1
IF a <= n THEN
z = 1
DO
  z = z + 1
  IF z = a THEN PRINT a: EXIT DO
  IF a / z = INT(a / z) THEN EXIT DO
LOOP
END IF
IF b <= n THEN
z = 1
DO
  z = z + 1
  IF z = b THEN PRINT b: EXIT DO
  IF b / z = INT(b / z) THEN EXIT DO
LOOP
END IF
NEXT
Let's try it in the spanish way:
The best way to calculate a square root is not calculating it at all..
No divisions or MOD either..
It calculates 16384 primes in 3 seconds in my P4 1,4... to display them it takes a little longer.
Code:
CLS
PRINT "PRIME CALCULATOR BY ANTONI GUAL"
'indicate in stor how many primes you want
CONST stor = 16383 'That's the maximum;you may have to set the /ah switch
REDIM pr(stor) AS LONG
REDIM pr1(stor) AS LONG
REDIM pr2(stor) AS LONG

'pr saves primes found
'pr1 saves primes multiplied by factor pr2
'We save pr2 to stop scanning the array if pr2<pr, this indicates that
' pr> sqr(test&) without having to calculate the square root.
'
'pr() saves primes, pr1()=pr()*pr2()

'init the tables with 2 and test our first candidate, 3
pr(0) = 2
pr1(0) = 2
pr2(0) = 1
maxx& = 2
ind% = 0
test& = 3

t! = TIMER
DO
isprime% = -1
FOR i% = 0 TO ind%
   WHILE pr1(i%) < test&: pr1(i%) = pr1(i%) + pr(i%): pr2(i%) = pr2(i%) + 1: WEND
   IF pr2(i%) <= pr(i%) THEN EXIT FOR
   IF pr1(i%) = test& THEN isprime% = 0: EXIT FOR
NEXT
IF isprime% THEN
  ind% = ind% + 1
  IF (ind% AND 1023) = 0 THEN PRINT ind%; "th prime is:"; test&
  pr(ind%) = test&
  pr1(ind%) = test&
  pr2(ind%) = 1
END IF
test& = test& + 1
LOOP UNTIL ind% = stor

'That's all!

t1! = TIMER - t!
PRINT : PRINT stor + 1; " primes found in"; t1!; " seconds."; : INPUT "Want to print them all? (Y/N)"; a$
IF UCASE$(a$) <> "Y" THEN END

t! = TIMER
'array full, print it...
PRINT
FOR i% = 0 TO stor
  PRINT pr(i%),
NEXT
PRINT : PRINT stor + 1; " primes printed in "; TIMER - t!; "Not so fast..."

ERASE pr, pr1, pr2
i'm lost.
Quote:this works well enough for me.

Code:
INPUT number&

FOR y& = 1 TO number&
  prime% = 0
  FOR x& = 2 TO SQR(y&) - 1
    IF y& / x& = y& \ x& THEN prime% = 1
  NEXT x&
  IF prime% = 0 THEN PRINT y&
NEXT y&

I hope you don't think I picking on you...your code pegs 25, 49, and 121 as prime...all have prime roots, but they themselves are not prime...I hope this isn't good enough for you!!! :-?
the world aint perfect....

hey, for pulling it out of me arse it worked okay, right?
Pages: 1 2 3 4 5 6 7