HELP!

D

Don

Mack said:
Here is my problem I am trying to solve just for fun.
The input is a number in the range 1-30000.
The output is one line of text for each number in the range 2 to the number input.
For each number it must print either "n is prime" or "n is not prime".
Speed is not important because this is running on a very fast computer.
I am not sure how to do this. Any ideas?

/*
Sure, I'd be happy to help. Here you go. Enjoy!

I know you said that speed is not important, but this program
implements a blindingly fast O(n^n) algorithm to find prime
numbers, using an elegant recursive method.
*/

#include <stdio.h>

int _(int n, int m, int d)
{
int r = m != n;
for(int i=0; d && (i<n); i++)
r *= _(n,(m<=n)?i*m:0,d-1)|!_(i,1,i);
return r;
}

/*------------------------------------------
Print primes up to the requested value
--------------------------------------------*/
int main(int argc, char* argv[])
{
int m;
scanf("%d", &m);
for(int n = 2; n<=m; n++)
printf("%d is%s prime\n",n, _(n,1,n)?"" : " not");
return 0;
}
 
M

mlimber

Don said:
Mack said:
Here is my problem I am trying to solve just for fun.
The input is a number in the range 1-30000.
The output is one line of text for each number in the range 2 to the number input.
For each number it must print either "n is prime" or "n is not prime".
Speed is not important because this is running on a very fast computer.
I am not sure how to do this. Any ideas?

/*
Sure, I'd be happy to help. Here you go. Enjoy!

I know you said that speed is not important, but this program
implements a blindingly fast O(n^n) algorithm to find prime
numbers, using an elegant recursive method.
*/

#include <stdio.h>

int _(int n, int m, int d)
{
int r = m != n;
for(int i=0; d && (i<n); i++)
r *= _(n,(m<=n)?i*m:0,d-1)|!_(i,1,i);
return r;
}

/*------------------------------------------
Print primes up to the requested value
--------------------------------------------*/
int main(int argc, char* argv[])
{
int m;
scanf("%d", &m);
for(int n = 2; n<=m; n++)
printf("%d is%s prime\n",n, _(n,1,n)?"" : " not");
return 0;
}

What is your question exactly?

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top