comp.lang.c
[Top] [All Lists]

Re: Help! Need prime number program!

Subject: Re: Help! Need prime number program!
From: Don
Date: Tue, 1 Apr 2008 12:33:13 -0700 PDT
Newsgroups: comp.lang.c, comp.lang.c++

Mac Daddy <hwills@xxxxxxxx>
> I want to rite a program which will ask for a nummer and then print out the 
> prime
> nummers up to that nummer.  A prime nummer is only divizibal by one.
> If its prime it should say it is prime or if not it should say it is not 
> prime.
> It doesn't have to be that fast because the nummer will be small.
> It has to be recursive.  What does that mean?
> I am just doing this for fun.  It is not homework.
> Henry Wills aka The Mac Daddy

#include <stdio.h>

/* Sure, Mr. Daddy.  Here is a Prime Nummer Program just for you.
   This program implements a blindingly fast O(n^n) algorithm
   to find prime nummers, using an elegant recursive method. */
int _(int n, int m, int d, int t=0)
{
    int r;
    if (t) return d?1+_(n,m,d-1,d):n?_(n-1,m,m,n):0;
    for(r=m!=n; d*(t<n); ++t)
        r &= _(n,_(t,m,0,1),d-1)|!_(t,1,t);
    return r*n;
}


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

    return 0;
}

<Prev in Thread] Current Thread [Next in Thread>
  • Re: Help! Need prime number program!, Don <=
Privacy Policy