Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Prime numberz help!

Reply
Thread Tools

Re: Prime numberz help!

 
 
Don
Guest
Posts: n/a
 
      08-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Just 4 fun I want to write a program to tell if numberz are prime.
> I will enter a number, and for all numbers 1 to that number it will say "N is prime" or "N is not prime".
> And it should be recursuve. What duz that mean?
> Can u show me how to do it?


/* Sure! I am always happy to help.
This program implements a blindingly fast O(n^n) algorithm
to find prime numbers, using an elegant recursive method. */

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

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

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      08-01-2007
Don wrote:

> (E-Mail Removed) wrote:
>> Just 4 fun I want to write a program to tell if numberz are prime.
>> I will enter a number, and for all numbers 1 to that number it will say
>> "N is prime" or "N is not prime".
>> And it should be recursuve. What duz that mean?
>> Can u show me how to do it?

>
> /* Sure! I am always happy to help.
> This program implements a blindingly fast O(n^n) algorithm
> to find prime numbers, using an elegant recursive method. */
>
> int _(int n, int m, int d, int p)


Why such an obfuscated name?

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


After compiling your program, by fixing missing includes, here's a sample
session:

$ ./0014
78
2 is prime
3 is prime
4 is not prime
5 is prime

Infinite loop? Killed after 5 minutes.

$ ./0014
10
2 is prime
3 is prime
4 is not prime
5 is prime

Maximum CPU usage, no response, killed after 10 minutes.


 
Reply With Quote
 
 
 
 
Justin Spahr-Summers
Guest
Posts: n/a
 
      08-01-2007
On Aug 1, 1:58 pm, santosh <(E-Mail Removed)> wrote:
> After compiling your program, by fixing missing includes, here's a sample
> session:
>
> $ ./0014
> 78
> 2 is prime
> 3 is prime
> 4 is not prime
> 5 is prime
>
> Infinite loop? Killed after 5 minutes.
>
> $ ./0014
> 10
> 2 is prime
> 3 is prime
> 4 is not prime
> 5 is prime
>
> Maximum CPU usage, no response, killed after 10 minutes.


I think his post was meant to be ironic, in response to the original
request for someone else to do the OP's (on whatever medium it was)
homework.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-01-2007
Justin Spahr-Summers wrote:

> On Aug 1, 1:58 pm, santosh <(E-Mail Removed)> wrote:
>> After compiling your program, by fixing missing includes, here's a sample
>> session:
>>
>> $ ./0014
>> 78
>> 2 is prime
>> 3 is prime
>> 4 is not prime
>> 5 is prime
>>
>> Infinite loop? Killed after 5 minutes.
>>
>> $ ./0014
>> 10
>> 2 is prime
>> 3 is prime
>> 4 is not prime
>> 5 is prime
>>
>> Maximum CPU usage, no response, killed after 10 minutes.

>
> I think his post was meant to be ironic, in response to the original
> request for someone else to do the OP's (on whatever medium it was)
> homework.


Should've known
Never mind, and, if it *is* meant to be ironic, apologies to Don.

 
Reply With Quote
 
Don
Guest
Posts: n/a
 
      08-01-2007
On Aug 1, 1:58 pm, santosh <(E-Mail Removed)> wrote:
> Don wrote:
> > (E-Mail Removed) wrote:
> >> Just 4 fun I want to write a program to tell if numberz are prime.
> >> I will enter a number, and for all numbers 1 to that number it will say
> >> "N is prime" or "N is not prime".
> >> And it should be recursuve. What duz that mean?
> >> Can u show me how to do it?

>
> > /* Sure! I am always happy to help.
> > This program implements a blindingly fast O(n^n) algorithm
> > to find prime numbers, using an elegant recursive method. */

>
> > int _(int n, int m, int d, int p)

>
> Why such an obfuscated name?
>
>
>
>
>
> > {
> > int i, r = m != n;
> > switch(p)
> > {
> > case 0:
> > for(i=0; d && (i<n); i=_(i,1,d,1))
> > r = _(r,_(n,(m<=n)?_(i,m,d,2):0,d-1,0)|!_(i,1,i,0),
> > 0,2);
> > break;
> > case 1:
> > r = n ? 1 + _(n-1,m,d,1) : m ? 1 + _(n,m-1,d,1) : 0;
> > break;
> > case 2:
> > r = m ? _(n,_(n,m-1,d,2),0,1) : m;
> > }
> > return r;
> > }

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

>
> After compiling your program, by fixing missing includes, here's a sample
> session:
>
> $ ./0014
> 78
> 2 is prime
> 3 is prime
> 4 is not prime
> 5 is prime
>
> Infinite loop? Killed after 5 minutes.
>
> $ ./0014
> 10
> 2 is prime
> 3 is prime
> 4 is not prime
> 5 is prime
>
> Maximum CPU usage, no response, killed after 10 minutes.- Hide quoted text -
>
> - Show quoted text -


Not infinite. It takes about an hour to figure out that 6 is not
prime. But it is recursive, as required!

 
Reply With Quote
 
stremler@rohan.sdsu.edu
Guest
Posts: n/a
 
      08-01-2007
begin quoting santosh <(E-Mail Removed)> :
> Justin Spahr-Summers wrote:

[snip]
>> I think his post was meant to be ironic, in response to the original
>> request for someone else to do the OP's (on whatever medium it was)
>> homework.


> Should've known
> Never mind, and, if it *is* meant to be ironic, apologies to Don.


Whenever I see a function named "_", I conclude that *someone* is
playing with someone else...

--
-----------------------------------------------------------------------------
"I've got a new entry for our list of words that | (E-Mail Removed)
get a reaction." -Calvin (_Calvin & Hobbes_) | Stewart Stremler
 
Reply With Quote
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      08-05-2007
[snips]

On Wed, 01 Aug 2007 11:27:24 -0700, Don wrote:

> (E-Mail Removed) wrote:
>> Just 4 fun I want to write a program to tell if numberz are prime.
>> I will enter a number, and for all numbers 1 to that number it will say "N is prime" or "N is not prime".
>> And it should be recursuve. What duz that mean?
>> Can u show me how to do it?

>
> /* Sure! I am always happy to help.
> This program implements a blindingly fast O(n^n) algorithm
> to find prime numbers, using an elegant recursive method. */



Blindingly fast O(n^n)? Hmm... think I'll see if I can't try this out
after I buy another 50 BlueGene systems.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Prime Time To Find All The Paths Of A Seq. Cir., Not Only the Critical Ones ilterisderici@hotmail.com VHDL 0 03-16-2006 09:46 PM
VS2005 Not ready for prime time CMM ASP .Net 11 02-09-2006 04:18 PM
Prime Cooler Fanless VGA Cooler Review @ PC Modding Malaysia Silverstrand Front Page News 4 07-04-2005 01:16 AM
Firefox...not ready for prime time. Jim L Firefox 21 02-17-2005 02:05 PM
Prime numbers: addative property modulo p, where p is prime Jeremy Fischer Perl Misc 0 01-16-2005 05:53 PM



Advertisments