HELP!

Discussion in 'C++' started by Don, Dec 9, 2005.

  1. Don

    Don Guest

    Mack <> 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;
    }
     
    Don, Dec 9, 2005
    #1
    1. Advertising

  2. Don

    mlimber Guest

    Don wrote:
    > Mack <> 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;
    > }


    What is your question exactly?

    Cheers! --M
     
    mlimber, Dec 9, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    774
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    718
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    853
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=

    HELP - HELP - HELP

    =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    687
    Eliyahu Goldin
    Mar 9, 2006
  5. Buster

    Help, Help, Help

    Buster, Oct 4, 2003, in forum: Java
    Replies:
    3
    Views:
    478
    Saager
    Oct 30, 2003
Loading...

Share This Page