Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. at least he tried

at least he tried

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
42 Posts 25 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Eric Georgiades

    as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

    if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
    {
    return true;
    }
    else
    {
    return false;
    }

    when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

    me, myself and my blog - loadx.org ericos g.

    M Offline
    M Offline
    mav northwind
    wrote on last edited by
    #3

    And, in addition to being very limited in the range of input values, the result isn't correct, either. <SmartassMode> 1 is not a prime number, by definition. </SmartassMode> ;P

    Regards, mav -- Black holes are the places where God divided by 0...

    E V 2 Replies Last reply
    0
    • P Pascal 0

      wikipedia stops at 113 too :) http://en.wikipedia.org/wiki/Prime_number[^]

      E Offline
      E Offline
      Eric Georgiades
      wrote on last edited by
      #4

      well then. and i felt sorry for him having to find them on his own. well observed!

      me, myself and my blog - loadx.org ericos g.

      1 Reply Last reply
      0
      • M mav northwind

        And, in addition to being very limited in the range of input values, the result isn't correct, either. <SmartassMode> 1 is not a prime number, by definition. </SmartassMode> ;P

        Regards, mav -- Black holes are the places where God divided by 0...

        E Offline
        E Offline
        Eric Georgiades
        wrote on last edited by
        #5

        True, but that did not worry me. ;P

        me, myself and my blog - loadx.org ericos g.

        1 Reply Last reply
        0
        • E Eric Georgiades

          as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

          if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
          {
          return true;
          }
          else
          {
          return false;
          }

          when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

          me, myself and my blog - loadx.org ericos g.

          D Offline
          D Offline
          dbrenth
          wrote on last edited by
          #6

          Should have told him he has to use this list: The first 10000 prime numbers[^]

          Brent

          V 1 Reply Last reply
          0
          • E Eric Georgiades

            as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

            if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
            {
            return true;
            }
            else
            {
            return false;
            }

            when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

            me, myself and my blog - loadx.org ericos g.

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #7

            Ask him to write a function that returns weather or not a given number is even :D

            my articles

            E V 2 Replies Last reply
            0
            • E Eric Georgiades

              as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

              if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
              {
              return true;
              }
              else
              {
              return false;
              }

              when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

              me, myself and my blog - loadx.org ericos g.

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #8

              How about a function that returns whether a number is an integer?

              Chris Meech I am Canadian. [heard in a local bar]

              E 1 Reply Last reply
              0
              • G Giorgi Dalakishvili

                Ask him to write a function that returns weather or not a given number is even :D

                my articles

                E Offline
                E Offline
                Eric Georgiades
                wrote on last edited by
                #9

                Well funny you should mention that, since it was asked previously. Amazingly enough, most knew the *hidden* patterns of even numbers. Or they heard me say modulus, between all the words that sent them to sleep. :-)

                me, myself and my blog - loadx.org ericos g.

                1 Reply Last reply
                0
                • C Chris Meech

                  How about a function that returns whether a number is an integer?

                  Chris Meech I am Canadian. [heard in a local bar]

                  E Offline
                  E Offline
                  Eric Georgiades
                  wrote on last edited by
                  #10

                  I guess if we follow the "Law of Wikipedia"[^], that would simply be -3,-2,-1,0,1,2,3.

                  me, myself and my blog - loadx.org ericos g.

                  1 Reply Last reply
                  0
                  • E Eric Georgiades

                    as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

                    if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
                    {
                    return true;
                    }
                    else
                    {
                    return false;
                    }

                    when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

                    me, myself and my blog - loadx.org ericos g.

                    _ Offline
                    _ Offline
                    _anil_
                    wrote on last edited by
                    #11

                    I really feel sorry for him... I hope u might not have scold that poor guy ;P

                    Regards Anil

                    E 1 Reply Last reply
                    0
                    • E Eric Georgiades

                      as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

                      if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
                      {
                      return true;
                      }
                      else
                      {
                      return false;
                      }

                      when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

                      me, myself and my blog - loadx.org ericos g.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #12

                      Waaaay back in college, in my first Pascal class, we were assigned the task of writing a program to perform the "Sieve of Eratosthenes"... but the assignment quoted a formula to use... which was faulty (of course). I don't know what the other students did, but I headed to the library. If I ever teach (and I'd like to) I would assign the problem with extra credit to whomever builds the largest sieve. Wouldn't this be a good Friday programming quiz?

                      V 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Waaaay back in college, in my first Pascal class, we were assigned the task of writing a program to perform the "Sieve of Eratosthenes"... but the assignment quoted a formula to use... which was faulty (of course). I don't know what the other students did, but I headed to the library. If I ever teach (and I'd like to) I would assign the problem with extra credit to whomever builds the largest sieve. Wouldn't this be a good Friday programming quiz?

                        V Offline
                        V Offline
                        Vikram A Punathambekar
                        wrote on last edited by
                        #13

                        PIEBALDconsult wrote:

                        Wouldn't this be a good Friday programming quiz?

                        I miss those. :sigh: I leave my office a bit early on Friday evenings and don't get to see the Friday quizzes because they're posted later in the day. (I don't have a computer at home.)

                        Cheers, Vıkram.


                        After all is said and done, much is said and little is done.

                        1 Reply Last reply
                        0
                        • _ _anil_

                          I really feel sorry for him... I hope u might not have scold that poor guy ;P

                          Regards Anil

                          E Offline
                          E Offline
                          Eric Georgiades
                          wrote on last edited by
                          #14

                          Nah, since for most of their test data it returned good results.

                          me, myself and my blog - loadx.org ericos g.

                          1 Reply Last reply
                          0
                          • E Eric Georgiades

                            as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

                            if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
                            {
                            return true;
                            }
                            else
                            {
                            return false;
                            }

                            when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

                            me, myself and my blog - loadx.org ericos g.

                            S Offline
                            S Offline
                            Sylvester george
                            wrote on last edited by
                            #15

                            He does not need any computer to find prime numbers :-D

                            Regards, Sylvester G sylvester_g_m@yahoo.com

                            1 Reply Last reply
                            0
                            • E Eric Georgiades

                              as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

                              if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
                              {
                              return true;
                              }
                              else
                              {
                              return false;
                              }

                              when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

                              me, myself and my blog - loadx.org ericos g.

                              R Offline
                              R Offline
                              Rage
                              wrote on last edited by
                              #16

                              You are right, this is awful. Better is:

                              int Primes[]={2,3,5,7,...,113};
                              int size=sizeof(Primes[])/sizeof(Primes[0]);
                              int i=0;
                              while (i<size)
                              {
                              if (num==Primes[i]) return true;
                              }
                              return false;

                              Last modified: 21hrs 3mins after originally posted --

                              Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

                              P H B 3 Replies Last reply
                              0
                              • E Eric Georgiades

                                as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

                                if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
                                {
                                return true;
                                }
                                else
                                {
                                return false;
                                }

                                when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

                                me, myself and my blog - loadx.org ericos g.

                                N Offline
                                N Offline
                                Nemanja Trifunovic
                                wrote on last edited by
                                #17

                                Beh, any real programmer would write it like this:

                                int primes[] = {2,3,5,...133};
                                const int* end = primes + sizeof(primes)/sizeof(int);
                                return (std::find(primes, end, num) != end);


                                Programming Blog utf8-cpp

                                M P 2 Replies Last reply
                                0
                                • R Rage

                                  You are right, this is awful. Better is:

                                  int Primes[]={2,3,5,7,...,113};
                                  int size=sizeof(Primes[])/sizeof(Primes[0]);
                                  int i=0;
                                  while (i<size)
                                  {
                                  if (num==Primes[i]) return true;
                                  }
                                  return false;

                                  Last modified: 21hrs 3mins after originally posted --

                                  Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #18

                                  I dunno, maybe a switch statement.

                                  1 Reply Last reply
                                  0
                                  • R Rage

                                    You are right, this is awful. Better is:

                                    int Primes[]={2,3,5,7,...,113};
                                    int size=sizeof(Primes[])/sizeof(Primes[0]);
                                    int i=0;
                                    while (i<size)
                                    {
                                    if (num==Primes[i]) return true;
                                    }
                                    return false;

                                    Last modified: 21hrs 3mins after originally posted --

                                    Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

                                    H Offline
                                    H Offline
                                    hlmechanic
                                    wrote on last edited by
                                    #19

                                    Don't you just love infinate loops?:)

                                    P 1 Reply Last reply
                                    0
                                    • N Nemanja Trifunovic

                                      Beh, any real programmer would write it like this:

                                      int primes[] = {2,3,5,...133};
                                      const int* end = primes + sizeof(primes)/sizeof(int);
                                      return (std::find(primes, end, num) != end);


                                      Programming Blog utf8-cpp

                                      M Offline
                                      M Offline
                                      mav northwind
                                      wrote on last edited by
                                      #20

                                      And any real engineer would build something like this: :D

                                      bool IsPrime(int i)
                                      {
                                      if (i<2)
                                      return false;
                                      if (i == 2)
                                      return true;
                                      if (i%2 == 1)
                                      // All odd numbers are prime:
                                      // 3 ... prime
                                      // 5 ... prime
                                      // 7 ... prime
                                      // 9 ... measuring fault
                                      // 11... prime
                                      // 13... prime
                                      // and so on...
                                      return true;
                                      else
                                      return false;
                                      }

                                      Regards, mav -- Black holes are the places where God divided by 0...

                                      1 Reply Last reply
                                      0
                                      • E Eric Georgiades

                                        as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :

                                        if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
                                        {
                                        return true;
                                        }
                                        else
                                        {
                                        return false;
                                        }

                                        when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?

                                        me, myself and my blog - loadx.org ericos g.

                                        A Offline
                                        A Offline
                                        Andre Ladeira
                                        wrote on last edited by
                                        #21

                                        The guy is hidden a genious! He calculated the prime numbers up to 113 without assistance! I would have stopped at 19... :-)

                                        S 1 Reply Last reply
                                        0
                                        • H hlmechanic

                                          Don't you just love infinate loops?:)

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #22

                                          (Pssst, I think he used a <, but didn't remember to use < when he posted.) -- modified at 20:41 Thursday 14th June, 2007

                                          int Primes[]={2,3,5,7,...,113};
                                          int size=sizeof(Primes[])/sizeof(Primes[0]);
                                          int i=0;
                                          while (i<size)
                                          {
                                          if (num==Primes[i]) return true;
                                          }
                                          return false;

                                          But you're right, it lacks i++.

                                          S 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups