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.
  • 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
        • P PIEBALDconsult

          (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 Offline
          S Offline
          Sylvester george
          wrote on last edited by
          #23

          That is again Horror Coding :laugh:

          Regards, Sylvester G sylvester_g_m@yahoo.com

          L 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

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

            Ah, but in .net (C#) we can use a (generic) dictionary and an enum to further extend the required functionality...

            public static partial class NumberKeeper
            {
                \[System.FlagsAttribute\]
                public enum NumberProperty
                {
                    None       =   0
                ,             
                    Odd        =   1
                ,
                    Even       =   2
                ,
                    Positive   =   4
                ,
                    Negative   =   8
                ,
                    Prime      =  16
                ,
                    PowerOfTwo =  32
                ,
                    Square     =  64
                ,
                    Cube       = 128
                ,
                    Fibonacci  = 256
                ,
                    Factorial  = 512
            
                    /\* et cetera \*/
                } ;
            
                public static readonly System.Collections.Generic.Dictionary<int,NumberProperty> Numbers ;
            
                static NumberKeeper
                (
                )
                {
                    Numbers = new System.Collections.Generic.Dictionary<int,NumberProperty>() ;
            
                    Numbers.Add (   0 , NumberProperty.None ) ;
                    Numbers.Add (   1 , NumberProperty.Odd  | NumberProperty.Positive                         | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci | NumberProperty.Factorial ) ;
                    Numbers.Add (   2 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Prime  | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci | NumberProperty.Factorial ) ;
                    Numbers.Add (   3 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Prime                              | NumberProperty.Fibonacci ) ;
                    Numbers.Add (   4 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Square | NumberProperty.PowerOfTwo ) ;
                    Numbers.Add (   5 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Prime                              | NumberProperty.Fibonacci ) ;
                    Numbers.Add (   6 , NumberProperty.Even | NumberProperty.Positive                                                                                | NumberProperty.Factorial ) ;
                    Numbers.Add (   7 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Prime  ) ;
                    Numbers.Add (   8 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Cube   | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci ) ;
                    Numbers.Add (   9 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Square ) ;
                    Numbers.Add (  10 , NumberProperty.Even | NumberProperty.
            
            A 1 Reply Last reply
            0
            • A Andre Ladeira

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

              S Offline
              S Offline
              S Douglas
              wrote on last edited by
              #25

              andre-ladeira wrote:

              He calculated the prime numbers up to 113 without assistance!

              :cough: Unlikely ->- Pascal - Response[^] http://en.wikipedia.org/wiki/Prime_number[^]


              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.

                T Offline
                T Offline
                ToddHileHoffer
                wrote on last edited by
                #26

                I do kinda feel sorry for him. I hope you crushed his dreams of being a programmer.

                GameFly free trial

                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
                  Patrick Etc
                  wrote on last edited by
                  #27

                  Wow

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Ah, but in .net (C#) we can use a (generic) dictionary and an enum to further extend the required functionality...

                    public static partial class NumberKeeper
                    {
                        \[System.FlagsAttribute\]
                        public enum NumberProperty
                        {
                            None       =   0
                        ,             
                            Odd        =   1
                        ,
                            Even       =   2
                        ,
                            Positive   =   4
                        ,
                            Negative   =   8
                        ,
                            Prime      =  16
                        ,
                            PowerOfTwo =  32
                        ,
                            Square     =  64
                        ,
                            Cube       = 128
                        ,
                            Fibonacci  = 256
                        ,
                            Factorial  = 512
                    
                            /\* et cetera \*/
                        } ;
                    
                        public static readonly System.Collections.Generic.Dictionary<int,NumberProperty> Numbers ;
                    
                        static NumberKeeper
                        (
                        )
                        {
                            Numbers = new System.Collections.Generic.Dictionary<int,NumberProperty>() ;
                    
                            Numbers.Add (   0 , NumberProperty.None ) ;
                            Numbers.Add (   1 , NumberProperty.Odd  | NumberProperty.Positive                         | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci | NumberProperty.Factorial ) ;
                            Numbers.Add (   2 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Prime  | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci | NumberProperty.Factorial ) ;
                            Numbers.Add (   3 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Prime                              | NumberProperty.Fibonacci ) ;
                            Numbers.Add (   4 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Square | NumberProperty.PowerOfTwo ) ;
                            Numbers.Add (   5 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Prime                              | NumberProperty.Fibonacci ) ;
                            Numbers.Add (   6 , NumberProperty.Even | NumberProperty.Positive                                                                                | NumberProperty.Factorial ) ;
                            Numbers.Add (   7 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Prime  ) ;
                            Numbers.Add (   8 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Cube   | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci ) ;
                            Numbers.Add (   9 , NumberProperty.Odd  | NumberProperty.Positive | NumberProperty.Square ) ;
                            Numbers.Add (  10 , NumberProperty.Even | NumberProperty.
                    
                    A Offline
                    A Offline
                    Andre Ladeira
                    wrote on last edited by
                    #28

                    Is this real c# code or just a joke? I don't know the language but it looks pretty ugly to me...

                    We're in the pipe, five by five - Terran dropship.

                    P 1 Reply Last reply
                    0
                    • P Pascal 0

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

                      V Offline
                      V Offline
                      Vasudevan Deepak Kumar
                      wrote on last edited by
                      #29

                      - Pascal - wrote:

                      wikipedia stops at 113 too

                      13 is considered bad unlucky number. Is 113 fall under this category also? :-D

                      Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                      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...

                        V Offline
                        V Offline
                        Vasudevan Deepak Kumar
                        wrote on last edited by
                        #30

                        mav.northwind wrote:

                        1 is not a prime number, by definition.

                        It is qualified as Unique Number right?

                        Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                        1 Reply Last reply
                        0
                        • D dbrenth

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

                          Brent

                          V Offline
                          V Offline
                          Vasudevan Deepak Kumar
                          wrote on last edited by
                          #31

                          dbrenth wrote:

                          The first 10000 prime numbers[^]

                          And any of the following things would have happened for sure: 1) Typing the if-else clause for 10000 numbers, he would broken the keyboard. 2) Typing the if-else clause for 10000 numbers, he would have broken down and ambulance should have been requested. It is an unnecessary headache, at least in this case right?

                          Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                          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

                            V Offline
                            V Offline
                            Vasudevan Deepak Kumar
                            wrote on last edited by
                            #32

                            Giorgi Dalakishvili wrote:

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

                            And for sure, the developer would written the code which would have thrown System.StackOverFlowException :-D

                            Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                            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
                              Armin Fryder
                              wrote on last edited by
                              #33

                              He is a genius - always staying in business with a new update of primes!!

                              1 Reply Last reply
                              0
                              • P Pascal 0

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

                                K Offline
                                K Offline
                                Kevin McFarlane
                                wrote on last edited by
                                #34

                                Interesting how many unproved conjectures there are.

                                Kevin

                                1 Reply Last reply
                                0
                                • A Andre Ladeira

                                  Is this real c# code or just a joke? I don't know the language but it looks pretty ugly to me...

                                  We're in the pipe, five by five - Terran dropship.

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

                                  Yes, just formatted "my way".

                                  D 1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    Yes, just formatted "my way".

                                    D Offline
                                    D Offline
                                    Dan Neely
                                    wrote on last edited by
                                    #36

                                    yowch! Glad I don't have to maintain any of your code.

                                    -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                                    P 1 Reply Last reply
                                    0
                                    • S Sylvester george

                                      That is again Horror Coding :laugh:

                                      Regards, Sylvester G sylvester_g_m@yahoo.com

                                      L Offline
                                      L Offline
                                      leppie
                                      wrote on last edited by
                                      #37

                                      Your nick is a coding horror by itself! Make it small like all the other please :)

                                      **

                                      xacc.ide-0.2.0.75 - now with C# 3.5 support and Navigation Bar!

                                      **

                                      S 1 Reply Last reply
                                      0
                                      • D Dan Neely

                                        yowch! Glad I don't have to maintain any of your code.

                                        -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

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

                                        Back atcha!

                                        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
                                          DynV
                                          wrote on last edited by
                                          #39

                                          ROFL ! :laugh: I REALLY hope that he doesn't get hired to program an elevator controller or people will will wait a long time before getting to their level.

                                          Je vous salue du Québec !

                                          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