Velocity Reviews > C++ > HELP!

HELP!

Don
Guest
Posts: n/a

 12-09-2005
Mack <(E-Mail Removed)> wrote:
> 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;
}

mlimber
Guest
Posts: n/a

 12-09-2005
Don wrote:
> Mack <(E-Mail Removed)> wrote:
> > 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;
> }

Cheers! --M

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules