|
1 registered members (1 invisible),
2,890
guests, and 4
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
coding contest
#74729
05/19/06 16:07
05/19/06 16:07
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
OP
Expert
|
OP
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
hi there! recently i've been a lot into code optimization to speed up algorithms that are - mostly - basis of real time applications. so i'd like to introduce a competition, giving you a task and at about two weeks to solve it. i'll participate, too. the winner is determined by the competition rules (following). after that, the whole thing begins anew, either in a separate thread or in this one (depends on the percentage of spam, which i'd like you to omit  ). here we go...- you are allowed to publish several entries, but please edit your post (as long as possible) to enter in your new idea. enumerate them.
- you are allowed to use a programming language of your desire, but keep in mind that i (or someone else) has to inspect your code and bring it to work properly, so don't use something totally unknown (i'd prefer c, c++, assembler, basic, java, even wdl if you want to try *g*)
- whole program or only the algorithm? if possible, the whole program.
- the winner is determined by the speed the algorithm has calculated the expected result, which has to be represented in memory in any form, that can - without great effort - be translated into a readable output.
- my computer is the reference machine, i've a pentium 4 with 1816 MHz, 512 mb DDR RAM and a GeForce 6 6600 GT GS - the rest is rather nonrelevant.
- measure or estimate execution time by yourself! and add your system specs to make it possible for us to sort out the entries. if your time is highly unlikely, it'll get controlled anyway, so don't cheat.
prices? fame and glory. programmers don't get much for their work - sadly.  but you can use all that stuff here for your own projects and applications (if the author allows it). so here is the first task: a classical one. "calculate all prime numbers from 2 to 1 048 576." good luck. joey.
|
|
|
Re: coding contest
[Re: Joey]
#74730
05/19/06 19:03
05/19/06 19:03
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
OP
Expert
|
OP
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
okay, motivation (don't think i'm a looser 'cause i'm sitting here on friday night, having nothing to do but imposing such a contest upon you, i'll go out later on  ). my "brute-force"-entry (1), short and dirty, just to make you eager to show me you can do better: Code:
#include <iostream> #include <cmath> #include <bitset>
#include <windows.h>
typedef unsigned int uint;
bool check(uint n) { uint lim = (uint)sqrt((double)n);
for (uint i = 2; i <= lim; i ++) if (!(n%i)) return false;
return true; }
int _tmain(int argc, _TCHAR* argv[]) { std::bitset<4194304> bits; uint start = GetTickCount();
std::cout << "starting..." << std::endl;
for (uint i = 2; i <= 4194304; i ++) bits[i-1] = check(i);
std::cout << "finished." << std::endl << (float)(GetTickCount() - start)/1000 << " seconds needed." << std::endl;
return 0; }
execution time is somewhat less than one second, so i took fourth the size. since the execution time t(n) ~ sqrt(n), and the output shows 10.5 seconds, the time needed for 1 048 576 primes is t = 10.5 seconds / (4^2) = 0.66 seconds (approx.) the code shows you how to measure the time easily, if you didn't know already. joey.
|
|
|
Re: coding contest
[Re: Joey]
#74731
05/20/06 12:50
05/20/06 12:50
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Grr I can't get it to work in wdl... Code:
var max_loops = 11000; var a; //prime number var b=2; //counter (starting) var c; //division number var d=1; //array position var prime_numbers[99999]; //prime number storage var primehandle; //filehandle /* font prime_font = "arial", 0, 16;
panel primetext { digits = 5, 5, 15, prime_font, 1, a; digits = 5, 25, 15, prime_font, 1, b; digits = 5, 45, 15, prime_font, 1, c; digits = 5, 65, 15, prime_font, 1, d; flags = visible; } */ function main() { screen_color.blue = 200; prime_numbers[d]=b; //store prime number b+=1; //next number while(b<=pow(10,6)) //prime numbers to be checked { c=int(b/2)+1; //startvariable for divide the prime number a=b/c; //divide the prime number while (frc(a)!=0&&c>2) //check the divided number, no prime when a = integer { c-=1; //next division number a=b/c; //divide the prime number } if(c<=2){ //no valid division numbers? it's a prime number d+=1; //next place in the array prime_numbers[d]=b; //store the prime in the array } b+=2; //next prime number } // while (!key_any) {wait(1);} //wait for user input //write the numbers in a text file primehandle = file_open_write ("primenumbers.txt"); while (d>=0) { file_var_write (primehandle,prime_numbers[d]); file_str_write (primehandle, "\n"); d-=1; } file_close (primehandle); exit; }
It works until 2039, that's the last prime number it searches for  I just don't understand why it doesn't search further, must have something to do with the -99999 +99999 variable limit I guess  Time: 1 frame... (both calculating and writing the prime numbers in a text file) Dunno what time it takes for one frame.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: coding contest
[Re: Joozey]
#74732
05/21/06 10:37
05/21/06 10:37
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
OP
Expert
|
OP
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
no, i think it's the max_loops variable. try increasing it to 600 000... and if you want to calculate the full range, you should put a wait(1) anywhere in your loop. Code:
var max_loops = 600000; var a; //prime number var b=2; //counter (starting) var c; //division number var d=0; //array position var prime_numbers[83000]; //prime number storage var primehandle; //filehandle var time_needed = 0; /* font prime_font = "arial", 0, 16;
panel primetext { digits = 5, 5, 15, prime_font, 1, a; digits = 5, 25, 15, prime_font, 1, b; digits = 5, 45, 15, prime_font, 1, c; digits = 5, 65, 15, prime_font, 1, d; digits = 5, 85, 15, prime_font, 1, time_needed; flags = visible; } */ function main() { screen_color.blue = 200;
prime_numbers[d]=b; //store prime number b+=1; //next number while(b<=1048576) //prime numbers to be checked { timer(); c=int(b/2)+1; //startvariable for divide the prime number a=b/c; //divide the prime number while (frc(a)!=0&&c>2) //check the divided number, no prime when a = integer { c-=1; //next division number a=b/c; //divide the prime number } if(c<=2){ //no valid division numbers? it's a prime number d+=1; //next place in the array prime_numbers[d]=b; //store the prime in the array } b+=2; //next prime number time_needed += timer(); wait(1); } // while (!key_any) {wait(1);} //wait for user input //write the numbers in a text file primehandle = file_open_write ("primenumbers.txt"); while (d>=0) { file_var_write (primehandle,prime_numbers[d]); file_str_write (primehandle, "\n"); d-=1; } file_close (primehandle); exit; }
try it... i haven't... joey.
|
|
|
Re: coding contest
[Re: Joey]
#74733
06/04/06 21:13
06/04/06 21:13
|
Joined: Sep 2002
Posts: 758 Sunny Scotland
xoNoid
Developer
|
Developer
Joined: Sep 2002
Posts: 758
Sunny Scotland
|
I've decided to use the code of the human brain  ... umm... 1,3,5,7,11,13...989898974974*10(99999999999999)
|
|
|
Re: coding contest
[Re: Joey]
#74736
06/08/06 13:41
06/08/06 13:41
|
Joined: Sep 2002
Posts: 8,177 Netherlands
PHeMoX
Senior Expert
|
Senior Expert
Joined: Sep 2002
Posts: 8,177
Netherlands
|
Quote:
lol... you won 
maybe i shouldn't hold contests, seems as if my ideas don't get across...
On the contrary, I think your contest's idea is good, a programming challenge, eventhough it might have one flaw and that is this contest is supposed to be about which algorithm works fastest. My code would probably have been very similar to that of Jostie. So I wonder if there would be a remarkable or even noticable difference in speed. Anyways, I think we are up for the next task. 
Cheers
|
|
|
|