C Board  

Go Back   C Board > Community Boards > Contests Board

Reply
 
LinkBack Thread Tools Display Modes
Old 11-30-2008, 11:28 PM   #31
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
Quote:
Originally Posted by laserlight View Post
Right, I misread the rubric. Looks like you still have zero valid entries, heh.
yeah, I just made the req'd numbers large enough that it was non-trivial to test for primality, because there arent that many small prime numbers and I wanted optimizations that only pay off for large primes to have a fighting chance. I am actually going to update my baseline entry to include some of your code. I want to see if pregenerating the possible divisors improves performance over testing a few extra composite divisors.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 11-30-2008, 11:36 PM   #32
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by abachler
yeah, I just made the req'd numbers large enough that it was non-trivial to test for primality, because there arent that many small prime numbers and I wanted optimizations that only pay off for large primes to have a fighting chance.
Just to see how brute force will fare I modified my entry such that it only generates all the prime factors for all composites less than the 100000th prime larger than 2^32, and it took just under 30 seconds to run on my computer (which is a little less powerful than yours, but not enough ).
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 12-01-2008, 07:28 AM   #33
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
That would definately beat the baseline, which takes 143 seconds using all odd factors less than the root. The assumption of course is that checking compsite odd numbers is completely worthless because if the number being tested where divisible by a composite, then it woudl be divisible by a prime smaller than that composite which has already been checked. So the actual check of the composite itself does nothing but waste compute cycles. Especially for larger primes where the majority of composite numbers are proven composite in fewer than 256 calculations (a naive assumption) but primes take over 32768 calculatons, so reducing the workload for verifying that a prime is in fact prime will have a significant impact on total throughput.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 12-01-2008, 09:23 AM   #34
Reverse Engineer
 
maxorator's Avatar
 
Join Date: Aug 2005
Location: Estonia
Posts: 2,236
I made a simple one for under 2^32 which finishes in 132 seconds. Over 2^32 requires some thinking though.

Edit: Oh wait! When I used square root (dumb of me to forget that) I got 100,000 under 2^32 in 0.7sec and 1,000,000 under 2^32 in 18sec. Still no help when doing over 2^32 though.
__________________
The duck is irrelevant to my point.

Last edited by maxorator; 12-01-2008 at 09:42 AM.
maxorator is offline   Reply With Quote
Old 12-01-2008, 12:17 PM   #35
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
Well, generating a list of primes less than the maximum root you expect to encounter should allow for an optimized check of the numbers larger than 2^32 as laserlight and i discussed. I am currently adding LL's code to my own to increase the speed of the base algorithm, which should allow for faster throughput.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 12-01-2008, 02:14 PM   #36
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
My submission. Takes 3.8 seconds on my system.
Attached Files
File Type: cpp PrimeFunc.cpp (1.4 KB, 104 views)
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-01-2008, 03:01 PM   #37
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
Quote:
Originally Posted by brewbuck View Post
My submission. Takes 3.8 seconds on my system.
It doesnt compile, rand() apparently isnt defined in cmath, so ill include it and also, typedefing __int64 to bignum doesnt work, as the test program expects the actual function declaration to be PrimeFunc(__int64*), not PrimeFunc(BigNum*); ill fix that as well.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 12-01-2008, 03:10 PM   #38
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by abachler View Post
It doesnt compile, rand() apparently isnt defined in cmath, so ill include it and also, typedefing __int64 to bignum doesnt work, as the test program expects the actual function declaration to be PrimeFunc(__int64*), not PrimeFunc(BigNum*); ill fix that as well.
Okie dokie.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-01-2008, 06:06 PM   #39
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
process terminated after 15 minutes, no results returned...

the only changes I made to your code are -
Code:
#include <math.h>
#include <stdlib.h>

//typedef __int64 Bignum;
#define Bignum __int64
I am assuming you are doing somethign that implicitly requires the actual bignum type, so perhaps you could rewrite your code so it will compile using the following main (modified from your intrnal one.

Code:
#ifdef USE_BUILTIN_MAIN
int main()
{
	__int64 output[100000];

	PrimeFunc(output);
	return 0;
}
#endif
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet

Last edited by abachler; 12-01-2008 at 06:43 PM.
abachler is offline   Reply With Quote
Old 12-01-2008, 07:48 PM   #40
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
ok, here is the base code updated to use laserlights routine to generate the divisors, it then specifically uses the divisors to test primality. The original took 146 seconds, this version took 35, 5 times faster, so definately a worthwhile optimization.

Code:
#define DWORD unsigned __int32
#define OFFSET 2
extern void laserlight(__int64*);

void abachler(__int64* PrimeList){
    __int64 Prime = 4294967297;
    __int64 Root = 1;
    __int64 PrimeCount = 0;
    __int64 Divisors[100000];
 
    laserlight(Divisors);
 
prime_start:
    while(PrimeCount < 100000){
        Prime += OFFSET;
        while((Prime / Root) > Root) Root++;
        for(DWORD Test = 1;Divisors[Test] <= Root;Test++){ // skips the 2
            if(Prime % Divisors[Test] == 0) goto prime_start;
            }
        PrimeList[PrimeCount] = Prime;
        PrimeCount++;
        }
    return;
    }
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet

Last edited by abachler; 12-01-2008 at 07:51 PM.
abachler is offline   Reply With Quote
Old 12-01-2008, 07:55 PM   #41
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by abachler View Post
process terminated after 15 minutes, no results returned...
Stupid signed integer math... Try this:
Attached Files
File Type: cpp PrimeFunc.cpp (1.4 KB, 90 views)
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-01-2008, 08:12 PM   #42
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
Quote:
Originally Posted by brewbuck View Post
Stupid signed integer math... Try this:
ahem, PrimeFunc(__int64*) not PrimeFunc(Bignum*), but I went ahead and added this fix

Code:
void PrimeFunc(__int64* data){
    PrimeFunc((Bignum*) data);
    return;
    }
4.625 seconds, thats impressive, would you explain your method in detail?
Attached Images
 
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 12-01-2008, 08:26 PM   #43
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by abachler View Post
ahem, PrimeFunc(__int64*) not PrimeFunc(Bignum*), but I went ahead and added this fix

Code:
void PrimeFunc(__int64* data){
    PrimeFunc((Bignum*) data);
    return;
    }
Bleh. Thanks.

Quote:
4.625 seconds, thats impressive, would you explain your method in detail?
It's fast because I'm cheating
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-02-2008, 01:00 AM   #44
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by abachler
4.625 seconds, thats impressive, would you explain your method in detail?
Impressive? I thought that "a non-optimized test program was run and completed within the alloted time", which sounds even more impressive
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 12-02-2008, 01:25 AM   #45
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
Can we use CUDA (Compute Unified Device Architecture)?

Otherwise what's the point of saying you have a 8600GT? ... there's a horse sitting in the stable and I want to play with him! Basically my plan is to adapt brewbuck's code and win :-) the only problem is I need to understand how it works first...
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 12-02-2008 at 01:28 AM.
zacs7 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
xor linked list adramalech C Programming 23 10-14-2008 10:13 AM
prime number program with function mackieinva C Programming 17 09-20-2007 08:36 AM
Prime Number stops after 29, but why? Daveo C Programming 22 09-17-2004 10:55 AM
Somewhat new to c++, adding Prime number and Sqrt featre to program... thynksheraze C++ Programming 3 01-14-2003 10:34 PM
Random number generator Caze C++ Programming 6 12-03-2002 08:10 AM


All times are GMT -6. The time now is 07:49 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22