- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I'm quite surprised noone's posted this obviously more efficient one yet:
or you could even do:
Oooh, even better:
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
Admin
Ok, in all seriousness, why are you all doing this the hard way when a perfectly simple method exists?
Admin
of course there's a subtle error in my algorithm... erm... to prevent people from copying it... yes, that's the ticket.
Admin
That's awesome, Richard, pretty creative. =P
Admin
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:
Its enough to give me nightmares :-)
Admin
Yup - a better name would be SEE_THE_DAILY_WTF_POST_34239_FOR_CORRECT_CURRENT_VALUE_OF_THIS_VARIABLE
Admin
evna, we're just following after this awesome piece of code.
Admin
We have a winner folks.
Admin
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>
Admin
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); }
}
Admin
How about something like this: (in ML)
(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.
Admin
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)
Admin
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?
Admin
haven't you guys learned anything from TDWTF??? You have to use a for-switch pattern!
Admin
(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.
Admin
I can never, ever write just "i += 11" again.
Admin
Oh ya had to spoil it didn't ya! :)
Admin
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 ?
Admin
I don't think i've laughed this hard in a very very long time.
Admin
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>
Admin
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;
Admin
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
Admin
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.
Admin
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";
}
}
Admin
i = -~-~-~-~-~-~-~-~-~-~-~i;
Admin
Now we're arguing over the most efficient way to implement an infinite loop? Whose will finish first?
Admin
This version does not suffer from alignment constaints imposed by every modern compiler:
Incidentally, why did the forum software say I was quoting Anonymous when the message I posted wasn't by an anonymous person?
Admin
Good lord... what on EARTH is wrong with this forum software?
Admin
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)
Admin
((1++)+9)
Admin
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.
Admin
LOL You guys are a mess, this was a train wreck of a thread!! Great laughs though!
Admin
"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!"
Admin
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?
Admin
Of course we would have to kill anyone who actually did this.
Admin
Jeff S wrote ".... I'm incrementing by 11!"
LOL!
Admin
I could have done:
Because this stacks fewer registers, and doesn't depend on reusing a register, it'll run faster.
Admin
"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.)
Admin
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:
This code, like several examples above, also allows incrementing by values other than 11, simply by changing the value of the ELEVEN variable!
Admin
j+=(12-1);
:P
Admin
Admin
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)
Admin
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
Admin
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
}
Admin
OK, I just gotta know. Is one a he/she?
Admin
Oh my god you all are such fucking idiots, lol.
Admin
<FONT face=Verdana>aint no solutions using parallel processing?</FONT>
Admin
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#.
Admin
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!
Admin
Bugger I didn't notice the character limit. Here's a link anyway:
http://www.worth.id.au/~luke/inc11.c