Qbasicnews.com
December 06, 2019, 01:09:20 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 ... 3 4 [5]
  Print  
Author Topic: The PRIME NUMBERS  (Read 28793 times)
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #60 on: June 08, 2003, 11:24:39 PM »

Code:
CLS
WIDTH , 50
p = 3
t = TIMER
DO

 FOR i = 2 TO SQR(p)
  IF p / i = INT(p / i) THEN np = 1: EXIT FOR
 NEXT i

 IF np = 1 THEN
  np = 0
 ELSE
  IF c = 1 THEN c = 0:  PRINT USING "Time: ####.##"; TIMER - t: PRINT : LOCATE 1, 1
  PRINT p,
  c = c + 1
  pn = pn + 1
 END IF
 p = p + 2

LOOP UNTIL LEN(INKEY$) = 1

PRINT
PRINT
PRINT pn; " primes found"
PRINT USING "In ####.## sec"; TIMER - t


Antoni Gual: I was testing both of ours on primes up to 100000

Antoni Gual: 1 sec, found 9593 primes
Mine: 8 sec, found 9591 primes

one of ours is messed up. and im gussing it's mine.
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #61 on: June 08, 2003, 11:30:34 PM »

You didn't store anything. He did.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #62 on: June 08, 2003, 11:36:36 PM »

So you saying that i forgot two primes? or did he happen to find two just laying on the ground.
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #63 on: June 09, 2003, 03:07:18 PM »

Well, you didn't count the first prime (2) and the last one, probably.

I was talking about actually going through primes, not every number, like you did..... the reason why yours is slower.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Mango
Wandering Guru
***
Posts: 360



« Reply #64 on: June 09, 2003, 03:39:05 PM »

Quote from: "Joakim"
This one is really neat. It's fast and it's small.

Code:
FOR i& = 0 to 2147483647
  PRINT i&
NEXT


Now, don't tell me it doesn't print out all the prime numers  Tongue



That's pretty good...this one's more than twice as fast though!!!

Code:

PRINT "2";

FOR x& = 3 to 2147483647 STEP 2
PRINT x&;
NEXT x&
Logged
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #65 on: June 09, 2003, 03:53:39 PM »

whitetiger:
The two programs, yours an mine  are both not counting correctly the number of primes they display. You display 2 and don't count it. I don't display 1 (it can't be considered a prime, actually) but i count it. So there is the difference.
Logged

Antoni
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #66 on: June 09, 2003, 04:55:54 PM »

Code:

CLS
WIDTH , 50
p = 2
t = TIMER
DO

FOR i = 2 TO SQR(p)
  IF p = 2 THEN EXIT FOR
  IF p MOD i = 0 THEN np = 1: EXIT FOR
NEXT i

IF np = 1 THEN
  np = 0
ELSE
  'IF c = 1 THEN c = 0:  PRINT USING "Time: ####.##"; TIMER - t: PRINT : LOCATE 1, 1
  PRINT p, :  PRINT USING "Time: ####.##"; TIMER - t: PRINT : LOCATE 1, 1
  'c = c + 1
  pn = pn + 1
END IF
IF p = 2 THEN p = 3 ELSE p = p + 2

LOOP UNTIL p >= 100000'LEN(INKEY$) = 1

PRINT
PRINT
PRINT pn; " primes found"
PRINT USING "In ####.## sec"; TIMER - t


Print and counts number 2
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #67 on: June 09, 2003, 10:11:27 PM »

antoni: just take one from your result then.
Logged

Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #68 on: June 10, 2003, 07:01:10 PM »

Yes, I should do it. I was just using a program that counts 1 as prime as speed reference while developing mine . I just forgot to  modify it before posting.
Logged

Antoni
Mango
Wandering Guru
***
Posts: 360



« Reply #69 on: June 13, 2003, 04:55:13 AM »

Quote from: "Neo"
Mine got till the LONG overflow, that is up to and including 7FFFh Wink
And just in 1,5 hour


It wasn't made in QB Smiley. However, these are the test results with my program made in QB:

1 min: 2,000,000
2 min: 4,031,482
1 h: 240,482,843



Hey Neo...I'm fiddling with C the last couple days...here's a c port of my basic primer finder...on my 1.5 GHz after 6 min, the biggest prime was 190,976,059.  This looks similar to what you reported re flipping the signed 4-byte int after 1.5 hr...Mine uses unsigned, so I get to go all the way to ffffffff, instead of cutting short at 7fffffff.  Cheers.

Code:
#include <iostream>

using namespace std;

int main()
{
    unsigned short inc = 1;
    unsigned long c;
    unsigned long x;
    unsigned long stop;
    unsigned long count=0;
    unsigned long hold=1;
    unsigned short array[6540]={3};
       
   
    cout << "Find primes up to what number?  (4,200,000,000 max)\n";
    cin >> stop;
   
    for (x=3; x<65536;x += 2)  //get small primes, put in array
    {      
        for (c=0; (x % array[c]) && (array[c]*array[c] < x)  ; c++);
        if (x % array[c])
        {
             //cout << x << " ";   //use this line to show all primes
             array[inc++]=x;
        }  
    }
   
    for (x=65537; x<stop;x += 2)  //find large primes
    {
        for (c=0; (x % array[c]) && (array[c]*array[c] < x)  ; c++);
        if (x % array[c])
        {
             //cout << x << " ";   //use this line to show all primes
             if (count++ > hold)
             {
             cout << x << " ";     //print every 100000th prime
             hold += 100000;
             }
        }  
    }
           
    cout << "\n done\n";
    return 0;
}
Logged
Pages: 1 ... 3 4 [5]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!