• Xepo (unregistered)

    I'm quite surprised noone's posted this obviously more efficient one yet:

    int UpBy11(int i)
         {
         srand(magicnumber);
         while (i != i+11)
              i = rand();
         return i;
         }
    
    

    or you could even do:

    int UpBy11(int i)
         {
         srand(magicnumber);
         if (rand() == i+11)
              return rand();
         else
              return UpBy11(i); //Some problem in the code, let's just try again.
         }
    
    

    Oooh, even better:

    int UpBy11(int i)
         {
         srand(magicnumber);
         if (UpBy11(i) == i+11)
               return UpBy11(i);
         else
              return rand();
    
    

    Then, all you have to do is input a magicnumber seed which will return your i+11 on the first call to rand() to have maximum efficiency

  • Richard Platel (unregistered)

    Ok, in all seriousness, why are you all doing this the hard way when a perfectly simple method exists?

    typedef struct cImportant {
     int m_nDecrement;
     int m_nReferant;
     short m_snCRC;
     char m_cNext;
     char m_cPrev;
    } Important;
    
     i = (int)(((Important*)i)++);
    
  • Richard Platel (unregistered) in reply to Richard Platel

    of course there's a subtle error in my algorithm... erm... to prevent people from copying it... yes, that's the ticket.

  • Xepo (unregistered) in reply to Richard Platel

    That's awesome, Richard, pretty creative.  =P

  • (cs) in reply to Richard Platel

    Of course all of those people who are declaring a constant called ELEVEN are opening themselves up for trouble further down the line. What happens when the value to increment by changes?

    I can see it now:

    static final int ELEVEN = 42;
    i += ELEVEN;

    Its enough to give me nightmares :-)

  • (cs) in reply to evnafets
    evnafets:

    Of course all of those people who are declaring a constant called ELEVEN are opening themselves up for trouble further down the line. What happens when the value to increment by changes?

    I can see it now:

    static final int ELEVEN = 42;
    i += ELEVEN;

    Its enough to give me nightmares :-)



    Yup - a better name would be SEE_THE_DAILY_WTF_POST_34239_FOR_CORRECT_CURRENT_VALUE_OF_THIS_VARIABLE
  • Xepo (unregistered) in reply to evnafets

    evna, we're just following after this awesome piece of code.

  • Rody (unregistered) in reply to Irrelevant
    Irrelevant:
    i -= (++i - ++i) + (++i + ++i) - (++i + ++i);


    We have a winner folks.
  • (cs)

    Recursion is ALWAYS the answer.

    void add(*int x, int count)
    {
       *x++;
       if (count--)
       {
          add(x,count);
       }
    }

    Since my C is a little rusty, lesse here..

    C#
    void add(ref int x, int count)
    {
       x++;
       if(count--)
       {
          add(ref x,count);
       }
    }

    Pascal :

    Procedure Add(Var X : Integer; Count : Integer);
    Begin
      Inc(X);
      Dec(Count);
      If Count<>0 Then Add(X,Count);
    End;

     

    Anyone wanna try a multi-core solution?  Spawn n processes, and then combine the results? <grin>

  • (cs)

    here we go, better code through obscurity and recursion:

    void function add(ref int x, int count)
    {
      switch(count % 10)
      {
      case  9 : x++; count--;
      case  8 : x++; count--;
      case  7 : x++; count--;
      case  6 : x++; count--;
      case  5 : x++; count--;
      case  4 : x++; count--;
      case  3 : x++; count--;
      case  2 : x++; count--;
      case  1 : x++; count--;
      default :  break;
      }
      if (count) { add(ref x,count); }
    }

  • Zatanix (unregistered) in reply to Xepol

    How about something like this: (in ML)

    1. make a recursive datatype to represent numbers in a romean kind of way
         (I(Z)=1, I(I(Z))=2, I(I(I(Z)))=3, I(I(I(I(Z))))=4, ...)

        datatype number = I of number
                        | Z

    2. make a recursive add function

        fun add (a) (Z)    = a
        |   add (a) (I(x)) = add (I(a)) x

    3. make a way to convert between our number-representation and normal integers:

        fun int2num(0) = Z
        |   int2num(i) = I(int2num(i-1))
        
        fun num2int(Z)    = 0
        |   num2int(I(x)) = 1 + num2int(x)

    this way you don't have to type weird stuff like i+11, you can just type:

    num2int(add (int2num(i)) (I(I(I(I(I(I(I(I(I(I(I(Z)))))))))))))

    much easier, i think.
  • Zatanix (unregistered) in reply to Zatanix

    Or for all C64 freaks out there:

          *=$1000     ; <- maybe $1000 isn't so good.. there should be room for the music here!! :)
          lda i
          clc
          adc #11
          sta i
          rts
    i     .byte whatever

    but since normal is boring, this may be even better:

          *=$4000
          ldx #11
          lda #0
          pha
    loop  lda #1
          pha
          dex
          bne loop
         
          ldx i
    loop2 pla
          beq jump
          inx
          jmp loop2
    jump  txa

          rts
          
    i     .byte whatever

    Using n+1 elements of the stack for incrementing a number by n is very good practice on any computer!
    (especially when the stack if only 256 bytes long)

  • (cs) in reply to Xepol

    Xepol:

    here we go, better code through obscurity and recursion:

    void function add(ref int x, int count) { switch(count % 10) { case 9 : x++; count--; (snip) case 1 : x++; count--; default : break; } if (count) { add(ref x,count); } }

    And if you feed this 10?

  • (cs) in reply to Bellinghman

    haven't you guys learned anything from TDWTF??? You have to use a for-switch pattern!

  • Captain Lambda (unregistered) in reply to foxyshadis

     (define (make-incrementor x)
         (lambda (y)
            (+ x y)))

    (define incBy11 (make-incrementor 11))

    (incBy11 2) evaluates to 13

    Don't see what's so hard about that.

  • x (unregistered) in reply to Captain Lambda

    I can never, ever write just "i += 11" again.

  • (cs) in reply to Maurits
    Maurits:
    He's probably looking for

    i += 11;


    Oh ya had to spoil it didn't ya!  :)
  • (cs) in reply to Effendi
    Anonymous:
    So, is lamecoder the only one here besides me who got the Spinal Tap reference?


    I got it too, I laughed out loud, (well, I laughed out loud in the way that I didn't actually laugh out loud, but was jolly close to it).

    Why doesn't he just increment by a larger value of 10 ?
  • (cs) in reply to Chris
    Anonymous:
    You are all great programmers... I bet most of you work on the Linux kernel.

    Why don't you just write:

    i += 11;

    Sometimes, the answer is that simple!



    I don't think i've laughed this hard in a very very long time.

  • Anonymous (unregistered) in reply to mattparkins

    Oh, come on. You all know better than that. Obviously, it's done like this:

    template<unsigned inc="" typename="" data_t=""> < unsigned inc, typename data_t > void increment(data_t &x) { ++x; increment<inc -="" 1=""> < inc - 1 > (x); }
    template<typename data_t=""> < typename data_t > void increment < 0U > (data_t &x) { }

    increment < 11U > (i);

    Damn, template code always gets messed up here.
    </typename></inc></unsigned>

  • (cs) in reply to mattparkins

    mattparkins:
    Maurits:
    He's probably looking for

    i += 11;


    Oh ya had to spoil it didn't ya!  :)

    Sorry - here's my entry (pseudocode)

    int j;

    for (

       j = 0;

       !is_prime(j) ||

       j.ToString().length % 2 ||

       !is_palindrome(j.ToString());

       j++

    ) {}

    i += j;

  • zolo (unregistered) in reply to mattparkins

    You are all wrong binary ops are the way:

    int Inc(int i,int inc)
    {
        if(inc == 0) return i;
        return Inc(i^inc,(i&inc)<<1);
    }

    i = Inc(i,11);

    Or if you dont like recursion:

        a=i&1
        i>>=1
        i+=1
        b=i&3
        i>>=2
        i+=1
        i<<=2
        i+=b
        i<<=1
        i+=a
        i+=1

    Hope the poor guy finds his way to TDWTF, all the help he could get to improve his programming skills here!!!!!!!


    LOL

  • Peter Ibbotson (unregistered) in reply to Richard Platel

    I like Richards version, meets most of the requirements for a WTF in it's own right, plus it's got enough C portability problems to be an all time classic of what not to do. Shame the count is out by one and it would probably need some #pragma on most architectures.

  • (cs) in reply to Peter Ibbotson

    this is a perl example that uses a remote cluster to come up with the answer (namely, google calculator). should be rather recource-efficient if a similar approach was to be used for more complex arithmetical functions.


    #!/usr/bin/perl


    use HTTP::Request;

    use LWP::UserAgent;


    printf increment_by_eleven(6) . "\n";


    sub increment_by_eleven

    {

            $i = $_[0];


            $request = HTTP::Request->new(GET => "http://www.google.com/search?q=$i%2B11");

            $ua = LWP::UserAgent->new;

            $ua->agent('Not-a-bot');

            $response = $ua->request($request);


            if (!$response->is_success) {

                    die $response->status_line;

            # important: close brace

            }


            $_ = $response->content;

            if(/$i + 11 *= (\d+)/) {

                    return $1;

            }

            else {

                    die "answer from google not properly formatted!\n";

            }

    }

  • Defenestrator (unregistered)

    i = -~-~-~-~-~-~-~-~-~-~-~i;

  • Victor (unregistered) in reply to rogthefrog
    rogthefrog:

    loneprogrammer:
    // increment by eleven
    while(i != i + 11) {
       i++;
    }

    Save trees! Use less space!

    while(i++ < i + 11) {}

    [6]



    Now we're arguing over the most efficient way to implement an infinite loop?  Whose will finish first?
  • Oliver Klozoff (unregistered) in reply to Richard Platel
    Richard Platel:
    Ok, in all seriousness, why are you all doing this the hard way when a perfectly simple method exists?
    typedef struct cImportant {
     int m_nDecrement;
     int m_nReferant;
     short m_snCRC;
     char m_cNext;
     char m_cPrev;
    } Important;
    

    i = (int)(((Important*)i)++);

    This version does not suffer from alignment constaints imposed by every modern compiler:

    typedef char eleven_t[11];
    
    i = (int) & ( (eleven_t*) i )[1];
    

    Incidentally, why did the forum software say I was quoting Anonymous when the message I posted wasn't by an anonymous person?

  • Oliver Klozoff (unregistered) in reply to Oliver Klozoff

    Good lord... what on EARTH is wrong with this forum software?

  • Binsky (unregistered) in reply to Oliver Klozoff

    My, my, my...

    This is an amazingly fun thread! I just love the way you guys have proved all over that some programmers are blessed with a great sense of humor! (A shame some haven't found a way to implement this, but even those are funny sometimes)

     

  • BS (unregistered) in reply to Pedant

    ((1++)+9)

  • Hank Miller (unregistered)

    You folks need to think ahead alittle.  Today it is incriment by 11, tommorow it is 26 and the next day 4.  I would have thought most of you were smart enough to know that you never solve a specific problem when there is a more generic solution that covers the same thing.

    Here is my solution in python:

    class myInt(object):
        def init(self,init = 0):
            self.i = init;

        def getattr(self,name):
            # do some magic here to make sure only p is passed...
            self.i = self.i + len(name) - 1
            return self.i


    Now you can do
    >>>j  = myInit()
    >>j.pp
    1
    >>> j.pppppppppppp
    12
    >>> j.ppppppppppppppppppppppppppp
    38
    >>> j.ppppp
    42

    You could play with metaclasses a little bit so that you can do:
    >>> j
    42
    >>> j = 4
    >>> j
    4

    But I'm not advanced enough to go there.

  • (cs)

    LOL You guys are a mess, this was a train wreck of a thread!! Great laughs though!

  • (cs) in reply to curtisk

    "See that? I'm incrementing i by 11.  Most people increment by 10 ... this is 11."

    "why 11 ?"

    "Most people increment by 10, but once you're there, where can you go? Nowhere, that's where. You're already at i+10 !  But in my code, I do one better than every other programmer out there -- I increment by 11"

    "But why not make i equal to i+1, then and then you don't need to increment by 11.  You can just increment by 10 but the value will be the same."

    " I'm incrementing by 11!"

  • Stephan Rose (unregistered) in reply to Jasmine Strong
    Anonymous:
    Man, you bring back memories of my time when I wrote an OS for the Cirrus Logic EP9315, which has an ARM core =)


    I am not men, I am hot chicks.  :-)

    ARM cores are everywhere.  So much so that optimization can save lives.  Can you see where I could have saved an ARM11 cycle by using an extra register?

    Ohh a girl that writes ARM code. Talk about a dream come true =)

    But as far as your question goes, nope don't know. Is it by using some other command somewhere that uses that additional register that I just can't seem to remember off the top of my head?

     

  • Jon Strayer (unregistered) in reply to fooji
    Anonymous:
    i beg to differ you should just overload the ++ operator

    public static Complex operator ++(Complex c1)
    {
          return new Complex(c1 += 11);
    }


    Of course we would have to kill anyone who actually did this.
  • Zatanix (unregistered) in reply to Jeff S

    Jeff S wrote ".... I'm incrementing by 11!"

    LOL!

  • Jasmine Strong (unregistered) in reply to Stephan Rose
    Anonymous:
    Anonymous:
    Man, you bring back memories of my time when I wrote an OS for the Cirrus Logic EP9315, which has an ARM core =)


    I am not men, I am hot chicks.  :-)

    ARM cores are everywhere.  So much so that optimization can save lives.  Can you see where I could have saved an ARM11 cycle by using an extra register?

    Ohh a girl that writes ARM code. Talk about a dream come true =)

    But as far as your question goes, nope don't know. Is it by using some other command somewhere that uses that additional register that I just can't seem to remember off the top of my head?

     

    Hey, you know the ARM instruction set was designed by a woman in the first place?

    I could have done:

    .incbyeleven STMFD r13!, {r1-r3}
             ADR r1, __variabletoken     ; get whichever variable
             LDR r2, [r1]              ; load the variable
             ADD r3, r2, #11           ;  add 11, avoiding the stall
             STR r3, [r1]              ; store it back
             LDMFD r13!, {r1-r3}
             BX r14                     ; we might have been called from Thumb
    

    Because this stacks fewer registers, and doesn't depend on reusing a register, it'll run faster.

  • (cs) in reply to Jasmine Strong

    "Hey, you know the ARM instruction set was designed by a woman in the first place?"

    Two guys designed the ARM instruction set.

    One of the two people who designed the ARM instruction set is a woman.

    Both statements are true.

    (I knew both of them by sight at the time.)

  • cswetenham (unregistered) in reply to Bellinghman

    The REAL WTF is that no-one has considered using threads to perform the increment in parallel! Here is my code in Java 1.5, which I believe to be the optimal solution:

    // IncrementBy11.java
    public class IncrementBy11 {
      public static int i;
      public static final int ELEVEN = 11;
    
      public class Incrementor implements Runnable {
        public void run() { i = i + 1; }
      }
      
      Thread[] = new Thread[ELEVEN];
      for(Thread t : threads) {
        t = new Thread(new Incrementor(), "Incrementor Thread");
        t.start();
      }
    }
    

    This code, like several examples above, also allows incrementing by values other than 11, simply by changing the value of the ELEVEN variable!

  • hawkan (unregistered)

    j+=(12-1);
    :P

  • Spudley (unregistered) in reply to hawkan
    function AddEleven($i) {
        $j=1;
        $s="$j$j";      //Create a string equal to eleven.
        $i=$i+$s;       //Isn't implicit typecasting wonderful?
        return $i;
    }
    
  • Jon (unregistered) in reply to cm

    Here's a Python example showing how 'lambda's make code easy to read:

        i = (lambda x, y, z: x+y+z)((lambda x, y: xy)((lambda x: (x,x))(3)), (lambda x: 2x)(1), i)

    Or how about this more generic example? As a bonus, it introduces a subtle bug:

        i = (i
    (i + 6) - 55) / (i-5)

  • Jon (unregistered) in reply to Jon

    Oh, and in response to the post earlier that used operator overloading: very clever, but overloading ++ will probably get confusing. Obviously, we want to overload something that no one ever uses. What about augmenting left shift?


    class our_int(int):

        def ilshift(self, increment):

            self += 11*increment

            return self


    >>> x = our_int(10)
    >>> x <<= 1
    >>> x

    21

  • preProcessor (unregistered) in reply to Darrin

    Here ya go - good ol' c :-)

    #include <stdio.h>

    void main(void) {
     int i = 100;
    #define i (i + 11)
     printf("i = %d\n", i);
    #undef i
    }

  • (cs) in reply to Bellinghman
    Bellinghman:
    "Hey, you know the ARM instruction set was designed by a woman in the first place?"

    Two guys designed the ARM instruction set.

    One of the two people who designed the ARM instruction set is a woman.

    Both statements are true.

    (I knew both of them by sight at the time.)



    OK, I just gotta know.   Is one a he/she?


  • dude (unregistered) in reply to Pedant

    Oh my god you all are such fucking  idiots, lol. 

  • onurbyk (unregistered)

    <FONT face=Verdana>aint no solutions using parallel processing?</FONT>

  • Rudy Velthuis (unregistered)

    While it may look stupid at first sight, if the new C# developer came from Delphi, I can understand it.

    In Delphi, 

      Inc(A);

    will do the same as

      A++;
     
    in C#, but

      Inc(A, 11);
     
    will do the same as

      A += 11;

    in C#. I guess he was just asking how to do the C# equivalent of Inc(A, 11) , while he already knew the C# equivalent for Inc(A). OK, someone already programming in C, C++, Java or C# would know this, but we don't know how often he uses or needs C#.

  • Povman (unregistered) in reply to Rudy Velthuis

    Ah. A challenge!
    It seems noone is making good use of arrays. What's the point in having them in the language if you're not going to use them?
    I've tried to make this as elegant as possible, if anyone can see a more efficient way of adding 11 to a number, please let me know!

    #define NUMBITS 32
    void incrementByEleven(int* num) {
      int elevenArray[NUMBITS];
      int inputArray[NUMBITS];
      int tempArray[NUMBITS];
      int resultArray[NUMBITS];
      int i, temp = *num;
      int result;
    
      // Zero the arrays
      for (i = 0; i < NUMBITS; i++) {
        elevenArray[i] = inputArray[i] = tempArray[i] = resultArray[i] = 0;
      }
    
      // Set up elevenArray
      elevenArray[NUMBITS-1] = elevenArray[NUMBITS-2] = elevenArray[NUMBITS-4] = 1;
    
      for (i = 0; i < NUMBITS; i++) {
        if (temp % 2) {
          tempArray[i] += 1;
          temp -= 1;      
        }
        temp /= 2;
      }
    
      // Reverse the array
      for (i = 0; i < NUMBITS / 2; i++) {
        temp = tempArray[i];
        tempArray[i] = tempArray[NUMBITS-1-i];
        tempArray[NUMBITS-1-i] = temp;
      }
    
      int j;
      // Add elevenArray to inputArray
      for (i = NUMBITS-1; i >= 0; i--) {
        resultArray[i] += tempArray[i] + elevenArray[i];
    
        // Use this instead of a carry int to save storage space
        if (resultArray[i] > 1) {
          if (i > 0) resultArray[i-1] += 1;
          resultArray[i] -= 2;
        } else { /* do nothing */ }
    
      }
    
      result = 0;
      // Decode resultArray
      for (i = 0; i < NUMBITS; i++) {
        if (resultArray[NUMBITS-1-i] == 1) {
          result += pow(2, i);
        }
      }
    
      *num = result;
    }
    
    void main() {
      int i = 14;
      printf("i was %d\n", i);
      incrementByEleven(&i);
      printf("and is now %d\n", i);
    }
    
  • Povman (unregistered) in reply to Povman

    Bugger I didn't notice the character limit. Here's a link anyway:
    http://www.worth.id.au/~luke/inc11.c

Leave a comment on “If ++ Increments ...”

Log In or post as a guest

Replying to comment #:

« Return to Article