#include %26lt;stdio.h%26gt;
# define FALSE 0 /**Value Representing False for NOT PRIME**/
# define TRUE 1 /**Value Representing True for IS PRIME**/
int isprime(long fib); /**Function prototype for isprime**/
long fibonacci (long n); /**Function prototype for nextFib**/
int main (void)
{long num;
long prime;
long fib; /**next fibonacci value**/
printf("\n\tTable Showing Fibonacci Values And Whether Prime!\n\n");
printf("%s%20s%30s\n\n", "Number/Times","Fibonacci Value","If Prime (1=True) (0=False)");
for (num = 0; num %26lt;= 40; num++){ /**Begin For**/
fib = fibonacci (num); /**Calculates Fibonnaci Values**/
prime = isprime(fib); /**Calculates if Fib values are prime**/
printf(" %2ld -%26gt;%18ld -%26gt; %18d\n",num,fib,prime );
} /**End For**/
return 0;
} /**end main**/
long fibonacci (long n) /**Next Fibonacci Value Function**/
{
if
Can anyone help me run this C program faster, it takes long to execute completely..!?
First of all this looks a lot like a school assignment, but I am going to trust you.
The first thing I did was to turn on optimizations on the compiler. I assume that you have already done something like that, but I was able to make it run much faster by doing that.
For gcc it is -O3
Next I ran a profiler and 91% of the time was beign spent in fibonacci. So I rewrote it to not recalculate everything that you already know. If you do go with a much larger number of loops try moving the fib cache off of the stack and onto the heap. You may need to look at ways to optimize the prime number checker then too.
Before 7.533s
After 0.004s
#include %26lt;stdio.h%26gt;
# define FALSE 0 /**Value Representing False for NOT PRIME**/
# define TRUE 1 /**Value Representing True for IS PRIME**/
#define NUM_LOOPS 40
int isprime(long fib); /**Function prototype for isprime**/
long fibonacci (long n, long * fib); /**Function prototype for nextFib**/
int main (void)
{
long num;
long prime;
long fib;
long fib_cache[NUM_LOOPS+1]; /**next fibonacci value**/
printf("\n\tTable Showing Fibonacci Values And Whether Prime!\n\n");
printf("%s%20s%30s\n\n", "Number/Times","Fibonacci Value","If Prime (1=True) (0=False)");
for (num = 0; num %26lt;= NUM_LOOPS; num++){ /**Begin For**/
fib = fibonacci (num, fib_cache); /**Calculates Fibonnaci Values**/
prime = isprime(fib); /**Calculates if Fib values are prime**/
printf(" %2ld -%26gt;%18ld -%26gt; %18d\n",num,fib,prime );
} /**End For**/
return 0;
} /**end main**/
long fibonacci (long n, long * fib) /**Next Fibonacci Value Function**/
{
if(n == 0 || n == 1) {
fib[n] = n;
} else {
fib[n] = fib[n-1] + fib[n-2];
}
return fib[n];
} /**End Fibonacci Function**/
int isprime(long fib) /**Check if Fib is Prime function**/
{
long divisor;
if (fib %26lt; 2) return FALSE;
for (divisor = 2; divisor %26lt; fib; divisor++){/**Start for**/
if ((fib % divisor) == 0) return FALSE;
}/*End for**/
return TRUE;
} /**End isprime fucntion**/
ADDED:
I shaved off some more time got it down to 0.001 seconds in some cases by rewriting isprime too, but you may have to link against the math library on some systems. -lm on Linux The idea for this came from the wikipedia page on prime number testing.
#include %26lt;math.h%26gt;
//...
int isprime(long fib) /**Check if Fib is Prime function**/
{
long k;
long max;
if (fib %26lt; 2) return FALSE;
if (fib == 2 || fib == 3) return TRUE;
if ((fib % 2) == 0 || (fib % 3) == 0) return FALSE;
max = (long)sqrt((double)fib);
for (k = 6; k %26lt; max; k+=6){/**Start for**/
if ((fib % (k-1)) == 0) {
return FALSE;
}
if ((fib % (k+1)) == 0) {
return FALSE;
}
}/*End for**/
return TRUE;
}
Reply:It does not even run for me. I think you may be missing some codes. Try fixing the first part because the second part seems fine. (First part is the part before additional info)
Reply:its working fine dude...... i executed it its giving all correct answers....:-)
Reply:I think you cant make it any faster. Computer would take time to execute such complicated calculations. And also, it didnt take more than 4-5 sec to execute on mine. If it takes more time on yours, then your computer maybe slow or you need to change some settings.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment