# Qbasicnews.com

Full Version: Prime Contest
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Make a program that calculates the value of the 1,000th, the 10,000th and the 100,000th prime. If FB is used the 1,000,000th prime must be calculated too.
The first prime is 2, the second is 3 and so on...
The number of primes up to the integer x can be aproximated by x/(log(x)-1) (QB's log)
The winner is who has the three primes right and faster.

References:
All you wanted to know about primes and never dared to ask http://primes.utm.edu/

The idea is to generate primes and count them up to the required count. There are a lot of optimizations possible in this generation and counting so I hope we will get some interesing sources.

We can open 3 categories according to the speed and memory limitations: Qbasic, Qb4.5 and FreeBASIC

EDITED: Added an additional request for FB, without it even an unoptimized souce takes less than 0.5 second.

EDIT2: For reference, the results are
Code:
```the    1000 th prime is      7919 the   10000 th prime is    104729 the  100000 th prime is   1299709 the 1000000 th prime is  15485863```
of course your program must FIND these results!
err winer? :lol:
Thanks!
Heres my simple FB prime program, using trial divison. It basically combines the only two rules i know, that theres no need to look at even numbers (except 2), and that you only need to check if n is divisible by any prime numbers smaller than sqr(n)

It takes about 0.5 seconds to get the 100,000 prime, and about 12.5 to get the 1,000,000 on my P4 1.8Ghz

I've been reading the link Antoni posted and may try again once i learn some different methods.

Code:
```Dim Shared prime_list(1 To 1000000) As uInteger Dim Shared prime_list_count As uInteger Dim As uInteger prime_val, i, j, n Dim As Integer is_prime prime_list(1) = 2    ' Setup the first two values prime_list(2) = 3 prime_list_count = 2 prime_val = 3 Do   prime_val += 2     ' Add 2, we don't need to look at even numbers   is_prime = -1   n = prime_val   i = 1   j = Int(sqr(n)) + 1 ' Only need to check if n is divisible by any prime smaller than sqr(n)   While prime_list(i) < j     If (n mod prime_list(i)) = 0 Then       is_prime = 0       Exit While     End If     i += 1   Wend   If is_prime Then     prime_list_count += 1     prime_list(prime_list_count) = prime_val   End If Loop Until prime_list_count = 1000000 Print prime_list(1000) Print prime_list(10000) Print prime_list(100000) Print prime_list(1000000)```
Great, we have one entry!
But sieves are faster...
Heres another one, a fairly standard sieve, using a bit array. I tried it just using a regular array which will take much more memory, and i though would be faster, but this bit based method won in the timings.

I also tried to change IsGood to a macro, but that actually slowed it down, I think thats P4 weirdness.

The only real optimization i did for this was the multiples of two, which because i used a bit array, i just iterated through, and masked out all multiple of two numbers.

This one runs in about 3.5 to 4 seconds, so 1/3 of the time of my first attempt, and i'm sure there are better methods yet...

Code:
```Const MaxPrime = 1000000             ' The max value for P Const MaxVal   = MaxPrime * 16       ' The max value for N (a bit of a cheat using * 16...) Const Max32    = (MaxVal \ 32) + 1   ' The number of 32-bit vars needed to store bit array Dim Shared BitsArray(0 To Max32) As uInteger #macro MarkGood(n)   Scope     Dim As uInteger p = (n) \ 32, o = (n) mod 32       BitsArray(p) = BITRESET(BitsArray(p), o)   End Scope #endmacro #macro MarkBad(n)   Scope     Dim As uInteger p = (n) \ 32, o = (n) mod 32       BitsArray(p) = BITSET(BitsArray(p), o)   End Scope #endmacro Function IsGood(ByVal n As uInteger) As Integer   Dim As uInteger p = n \ 32, o = n mod 32     Return NOT BIT(BitsArray(p), o) End Function Dim As uInteger i, n1, n2, count, mask = &H55555555 For i = 0 To Max32 - 1 ' Mark off all multiples of two quickly using a mask of 10...   BitsArray(i) = mask Next i MarkGood(2) ' Restore 2 as a prime MarkBad(1)  ' Make 1 not a prime count = 1 ' start count offset at 1 to account for 2 being a prime For n1 = 3 To MaxVal   If IsGood(n1) Then     count += 1     If count = 1000 Then Print n1     If count = 10000 Then Print n1     If count = 100000 Then Print n1     If count = 1000000 Then       Print n1       Exit For ' We've found the 1 millionth prime, we can quit     End If     For n2 = (n1 + n1) To MaxVal Step n1  ' work from n+n to max marking off multiples       MarkBad(n2)     Next n2   End If Next n1```
This is an interesting source by Rich Geldreich anyone can find at ABC packets. It uses an original idea and it's probably the only way to do it in QBasic 1.1, because of the 160K memory limits.

It ran in 14 secondes in QB1.1 and in 2 seconds compiled in QB4.5

Code:
```'Prime tally using a moving window version of the Erathostenes' Sieve 'Antoni Gual 10/2006 for the comtest at QBN. Qbasic1.1 version   '---------------------------------------------------------------- 'A true bit sieve would be faster, but the memory sizes in QB1.1 'require bold ideas. This one was created for QB by Rich Geldreich in '1992 from an idea in Donald Knuth's TAOCP. ' 'In a normal sieve each prime found is used in turn to mark all its 'composites thus the complete sieve must be hold in memory tor the final 'tally. In Rich's version all primes found so far are used at the same 'time to mark composites in the same moving slice of ths sieve, the 'numbers left unmarked are primes,and they can be counted as the slice 'progresses. 'In fact there is no data representing the sieve slice...only a priority 'queue that keeps the primes and it's factors used in the present sieve 'slice. This queue has to be dimensioned to hold all primes up to the 'square root of the maximum prime, the present size of 4096 would allow 'for primes up to 2^31. 'Additional optimizations; '  Multiples of 2 and 3 are skipped '  A prime p starts to sieve at p*p, because p*a  for a<p will be found '  by a. '  The heap is an udt but is kept in separate arrays for speed. DEFINT A-Z DECLARE SUB PutPrime (a&) DECLARE FUNCTION GetPrime& () CONST heapsize = 4096 'Priority queue DIM heapq(1 TO heapsize) AS LONG DIM HeapQ1(1 TO heapsize) AS LONG DIM HeapQ2(1 TO heapsize) AS LONG DIM SHARED n AS LONG DIM t AS LONG DIM Q AS LONG, Q1 AS LONG, Q2 AS LONG DIM TQ AS LONG, TQ1 AS LONG DIM u AS LONG, primepos AS LONG, cnt AS LONG primepos = 1000 n = 5 d = 2 r = 1 t = 25 heapq(1) = 25 HeapQ1(1) = 10 HeapQ2(1) = 30 cnt = 2 DO   DO     Q = heapq(1)     Q1 = HeapQ1(1)     Q2 = HeapQ2(1)     TQ = Q + Q1     TQ1 = Q2 - Q1     '***Insert Heap(1) into priority queue     i = 1     DO         j = i * 2         IF j <= r THEN                        IF j < r THEN                 IF heapq(j) > heapq(j + 1) THEN                   j = j + 1                 END IF             END IF             IF TQ > heapq(j) THEN                 heapq(i) = heapq(j)                 HeapQ1(i) = HeapQ1(j)                 HeapQ2(i) = HeapQ2(j)                 i = j             ELSE                 EXIT DO             END IF         ELSE             EXIT DO         END IF     LOOP     heapq(i) = TQ     HeapQ1(i) = TQ1     HeapQ2(i) = Q2     '***   LOOP UNTIL n <= Q   DO WHILE n < Q     cnt = cnt + 1     IF cnt < heapsize THEN heapq(cnt - 2) = n     IF cnt = primepos THEN        PRINT USING "The  ####### th prime is ######### "; primepos; n        IF primepos = 100000 THEN PRINT "Ended": SYSTEM        primepos = primepos * 10     END IF     n = n + d     d = 6 - d   LOOP   IF n = t THEN     u = heapq(r + 1)     t = u * u     '***Find location for new entry     j = r + 1     DO       i = j \ 2       IF i = 0 THEN         EXIT DO       END IF       IF heapq(i) <= t THEN         EXIT DO       END IF       heapq(j) = heapq(i)       HeapQ1(j) = HeapQ1(i)       HeapQ2(j) = HeapQ2(i)       j = i     LOOP     '***     heapq(j) = t     IF (u MOD 3) = 2 THEN       HeapQ1(j) = 2 * u     ELSE       HeapQ1(j) = 4 * u     END IF     HeapQ2(j) = 6 * u     r = r + 1        END IF   n = n + d   d = 6 - d LOOP```
I've just been looking at some of the previous posts about primes, seeing what methods other people used. I found a lot by you Antoni!, and some other interesting things I may try and add to my program.

I came across this too which made me laugh

http://members.surfeu.fi/kklaine/primebear.html
Thats a nice one Antoni, works fast, i'm still trying to understand how it works.

I improved my second one, its a bit faster now, but I still need to learn more to make it go even faster. Some of the code wasn't necessary, and i even forgot to ignore multiples of 2.

Code:
```Const MaxPrime = 1000000             ' The max value for P Const MaxVal   = MaxPrime * 16       ' The max value for N (a bit of a cheat using * 16...) Const Max32    = (MaxVal \ 32) + 1   ' The number of 32-bit vars needed to store bit array Dim Shared BitsArray(0 To Max32) As uInteger Dim As uInteger n1, n2, count, p, o, n1x2, n1x3 Dim As uInteger steps(0 To 47) = { _ 2,  4,  2,  4,  6,  2,  6,  4,  2,  4,  6,  6,  2,  6,  4,  2, _ 6,  4,  6,  8,  4,  2,  4,  2,  4,  8,  6,  4,  6,  2,  4,  6, _ 2,  6,  6,  4,  2,  4,  6,  2,  6,  4,  2,  4,  2, 10,  2, 10  _ } ' This lookup table is used to calculate the step, to avoid multiples of 2, 3, 5, and 7   ' any more that that and the table becomes very large (the one inc. 11 is 480 entrys) Dim As Integer curr_step Dim As Integer prime_to_find = 1000 count = 4 ' start count offset to account for 2, 3, 5 and 7 being prime n1 = 11   ' start at 11 due to count starting at 4 While n1 <= MaxVal   p = n1 shr 5  ' \ 32      'p is integer postition in bitarray   o = n1 and 31 ' mod 32    'o is bit offset   If NOT BIT(BitsArray(p), o) Then ' If the bit isn't set then it hasn't been struck out     count += 1     If count = prime_to_find Then       Print Using "###,###,###th prime - ###,###,###"; prime_to_find; n1       If prime_to_find = 1000000 Then Exit While       prime_to_find *= 10     End If     If n1 <= (sqr(MaxVal) + 1) Then ' Only strike out multiples of primes <= sqr(MaxVal)       n1x2 = n1 + n1                ' (the +1 is just to account for any rounding, may not       n1x3 = n1x2 + n1              '  be needed?)       ' we don't need to step by n1, as that will wastefully look at even numbers (odd+odd=even)       ' same goes for start pos, n1 is odd, so 2*n1 not needed, start at 3*n1       For n2 = n1x3 To MaxVal Step n1x2  ' work from 3n to max marking off multiples         p = n2 shr 5  ' \ 32      'p is integer postition in bitarray         o = n2 and 31 ' mod 32    'o is bit offset         BitsArray(p) = BITSET(BitsArray(p), o) ' Set the bit to show its bad       Next n2     End If   End If   ' we can step by set amounts, to avoid multiples of 2, 3 and 5   n1 += steps(curr_step)   curr_step += 1   If curr_step = 48 Then curr_step = 0 Wend```

EDIT: added a check for n1 <= sqr(maxval)
Quote:I came across this too which made me laugh

http://members.surfeu.fi/kklaine/primebear.html

Ok,Arktinen Krokotiili Projekti is the winner! Let's close the contest, nothing more more can be done.. :rotfl: :rotfl: :rotfl:

EDITED:
Their javascript prime finding algorithm is a little slow..
Code:
```function is_x_prime_number(x)     {     var limit=0;     var div=3;     var x_limit = Math.sqrt(x);     while (x%div!=0 && div<x_limit)div+=2;     is_prime = (x%div==0 && x!=div)*1     return is_prime;     }```
A simple trial division...
Pages: 1 2