473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Faster algorithm for prim numbers in C...???

Dear,

I weated a simple program, he is compiled
with gcc, and generates prim numbers,
with fastes method that I find.He is based
on divide with generated prim numbers...
At end he gives time report on format:
dd:hh:mi:sec...
Precompile as gcc prim.c -o prim, and use
him as "prim upper_bound",as example:
"prim 1000".If anybody have some idea
how to make algorithm fastes write to me...!!

//----------------------------------------------------------
#include<stdio.h>
#include<malloc.h>
#include<time.h>

void transform(char*, int);
struct lst{
long int value;
struct lst* next;
};

int main(int argc, char* argv[]){
long int i,j,k, time_begin, time_end, razlika_vremena;
char trans_value[20];
struct lst *prvi, *tekuci_prvi, *tekuci_drugi, *zadnji;
prvi=(struct lst*)malloc(sizeof(struct lst));
prvi->next=(struct lst*)malloc(sizeof(struct lst));
prvi->value=1;
prvi->next->value=2;
tekuci_prvi=prvi->next;
zadnji=prvi->next;
zadnji->next=(struct lst*)NULL;
k=atoi(argv[1]);
tekuci_drugi=prvi;
time(&time_begin);
for(i=1;i<k;i++){
exit:
tekuci_drugi=prvi;
while(tekuci_drugi->next){
if((i%(tekuci_drugi->value)==0) && tekuci_drugi->value!=1){
if(i==1){
goto exit;
}
i++;
goto exit;
}
tekuci_drugi=tekuci_drugi->next;
}
tekuci_prvi->next=(struct lst*)malloc(sizeof(struct lst));
tekuci_prvi=tekuci_prvi->next;
tekuci_prvi->value=i;
tekuci_prvi->next=(struct lst*)NULL;
/*printf("%ld\n",i);*/
}
tekuci_prvi=prvi;
while(tekuci_prvi->next){
printf("%d\n", tekuci_prvi->value);
tekuci_prvi=tekuci_prvi->next;
}
time(&time_end);
razlika_vremena=time_end-time_begin;
transform(&trans_value, razlika_vremena);
printf("\nGeneriranje i stampanje do %d broja je trajalo:%s", k,
trans_value);
exit(0);
}


void transform(char* t, int time){
int days, hours, minuts, seconds, i, timi;
char dd[5],hh[3],mi[3],sec[3];
timi=time;
days=time/(24*3600);
i=time%(24*3600);
time=time/(24*3600)+i;
hours=(time/3600);
i=time%3600;
time=time/3600+i;
minuts=time/60;
time=time/60;
time=time%60;
seconds=timi-days*3600*24-hours*3600-minuts*60;
if(days<10){
sprintf(dd,"0%d",days);
}else{
sprintf(dd,"%d",days);
}
if(hours<10){
sprintf(hh,"0%d",hours);
}else{
sprintf(hh,"%d",hours);
}
if(minuts<10){
sprintf(mi,"0%d",minuts);
}else{
sprintf(mi,"%d",minuts);
}
if(seconds<10){
sprintf(sec,"0%d",seconds);
}else{
sprintf(sec,"%d",seconds);
}
sprintf(t,"%s:%s:%s:%s",dd, hh, mi, sec);
}

//-------------------------------------------------

Thanks in advance, Robert ..!!
ro***********@si.t-com.hr
Nov 15 '05 #1
5 2177
"Robert Bralic" <ro*********@yahoo.co.uk> wrote in message
news:dg**********@ss405.t-com.hr...
I weated a simple program, he is compiled
with gcc, and generates prim numbers,
with fastes method that I find.He is based
on divide with generated prim numbers...
At end he gives time report on format:
dd:hh:mi:sec...
Precompile as gcc prim.c -o prim, and use
him as "prim upper_bound",as example:
"prim 1000".If anybody have some idea
how to make algorithm fastes write to me...!!


Prime number generation is off-topic in this group. Try mathematical groups.

Alex
Nov 15 '05 #2
In article <dg**********@ss405.t-com.hr>,
Robert Bralic <ro*********@yahoo.co.uk> wrote:
I weated a simple program, he is compiled
with gcc, and generates prim numbers,
with fastes method that I find.He is based
on divide with generated prim numbers...


Well, once you've generated "2", there's not much point in incrementing
the candidate by 1 each time...
--
These .signatures are sold by volume, and not by weight.
Nov 15 '05 #3

Robert Bralic wrote:
Dear,

I weated a simple program, he is compiled
with gcc, and generates prim numbers,
with fastes method that I find.He is based
on divide with generated prim numbers...
At end he gives time report on format:
dd:hh:mi:sec...
Precompile as gcc prim.c -o prim, and use
him as "prim upper_bound",as example:
"prim 1000".If anybody have some idea
how to make algorithm fastes write to me...!!

[snip]

/*
* All the primes less than 2^32
*/
#include <stdio.h>

#define MAX_PRIMES 6542
#define K_MAX 715827882

unsigned long primes[MAX_PRIMES], pcount, tpcount ;

unsigned long isqrt(unsigned long x)
{
unsigned long i = 0, b=1<<15 ;

do
{
i^=b;
if( i*i > x ) i^=b;
} while(b>>=1);
return i ;
}

int isp(unsigned long x)
{
unsigned long i, r ;

r = isqrt(x) ;
for ( i = 1 ; i < MAX_PRIMES ; i++ )
{
if ( primes[i] > r ) break ;
if ( x % primes[i] == 0 ) return 0 ;
}
if ( pcount < MAX_PRIMES ) { primes[pcount++] = x ; }
tpcount++ ;
if ( tpcount % 100000 == 0 ) printf("%ld %ld\n",x,tpcount) ;
return 1 ;
}

int main(int argc, char *argv[])
{
unsigned long k ;

primes[0] = 2 ;
primes[1] = 3 ;
for( k = 1, pcount = tpcount = 2 ; k < K_MAX ; k++ )
{
isp(6*k-1) ;
isp(6*k+1) ;
}
printf("%ld\n", tpcount) ;
return 0 ;
}

Nov 15 '05 #4
Don

Robert Bralic wrote:
I weated a simple program, he is compiled
with gcc, and generates prim numbers,
with fastes method that I find.He is based
on divide with generated prim numbers...
At end he gives time report on format:
dd:hh:mi:sec...
Precompile as gcc prim.c -o prim, and use
him as "prim upper_bound",as example:
"prim 1000".If anybody have some idea
how to make algorithm fastes write to me...!!


The faster algorithm for this would be a sieve. Use a bit-field to
remember which numbers have been eliminted. For each prime, eliminate
all of the multiples of that prime. Then search to find the next
number which has not been eliminated. That is the next prime. Repeat
the process until you are done.

It will be orders of magnitude faster than trial division for large
values of n.

Don

Nov 15 '05 #5
In article <11**********************@f14g2000cwb.googlegroups .com>,
Don <do*******@gmail.com> wrote:
Robert Bralic wrote:
I weated a simple program, he is compiled
with gcc, and generates prim numbers,
with fastes method that I find.
The faster algorithm for this would be a sieve. Use a bit-field to
remember which numbers have been eliminted. For each prime, eliminate
all of the multiples of that prime.


That's not the *fastest* algorithm, as Robert requested.

See for example, "Efficient Generation of Prime Numbers"
(Joye / Paillier / Vaudenay)
"Fast generation of prime numbers and secure public-key cryptographic
parameters" (Maurer)

or, like one the other posters said, look in one of the mathematics
newsgroups.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Armin Rigo | last post by:
Hi! This is a rant against the optimization trend of the Python interpreter. Sorting a list of 100000 integers in random order takes: * 0.75 seconds in Python 2.1 * 0.51 seconds in Python...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
11
by: bill | last post by:
I am trying to figure out if I can use sse to help execute arithmetic operations faster. I have 900 values that must each be scaled with a divide and multiply. This happens repeatedly. Any examples...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
52
by: Nomad.C | last post by:
Hi I've been thinking of learning Fortran as number crunching kinda language for my Physics degree......but then looking around the internet, people are saying that the libraries/ Algorithms once...
4
by: knuxus | last post by:
Hey everyone.... I would appreciate any type of help if anyone could explain me how to translate this algorithm to Visual Basic, im still learning and i would appreciate this algorithm to my prime...
5
by: Brosert | last post by:
I am writing (or trying to) a small program to draw a maze, that can then be traversed by a user. I have set up a grid of squares that can be either present (blocking the path) or not (allowing the...
3
by: Van Dugall | last post by:
I writing a economic statistics program.. I need to allocate billions of dollars and I have been shown projects to consider and ith project estimates its cost to be dollars and claims to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.