Posts: 2,765
Threads: 138
Joined: Nov 2002
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.
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 3,368
Threads: 195
Joined: Jan 2003
You didn't store anything. He did.
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.
Posts: 2,765
Threads: 138
Joined: Nov 2002
So you saying that i forgot two primes? or did he happen to find two just laying on the ground.
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 3,368
Threads: 195
Joined: Jan 2003
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.
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.
Posts: 358
Threads: 15
Joined: May 2003
Quote: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
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&
Posts: 1,407
Threads: 117
Joined: Dec 2002
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.
Antoni
Posts: 2,765
Threads: 138
Joined: Nov 2002
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
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 3,343
Threads: 83
Joined: Mar 2003
antoni: just take one from your result then.
Posts: 1,407
Threads: 117
Joined: Dec 2002
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.
Antoni
Posts: 358
Threads: 15
Joined: May 2003
Quote:Mine got till the LONG overflow, that is up to and including 7FFFh
And just in 1,5 hour
It wasn't made in QB . 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;
}
|