Show 80 post(s) from this thread on one page
Page 4 of 8 First 12345678 Last
• 03-27-2009
PING
Riddle #4: if-statement considered harmful
Code:

`(a>b)-(a<b)+(a==b)-(a==b)`
• 03-27-2009
Snafuist
Riddle #3: three dwarfs, two paths, one life
Quote:

Originally Posted by laserlight
I wanted to propose the solution of just capturing the dwarves and dragging them in a direction to see their response, but then I realised that I could not be sure that the dwarves were not so bored with their existence that they intended suicide, hence I would have to ask them questions anyway :o

I love this solution. My little brother suggested killing the dwarves until one of them decides to tell the truth anyway, but this solution suffers in a similar way.

And after all, a 50% chance is not too bad, so simply asking for food and a cold beer is probably ok.

Greets,
Philip

PS: ok, I looked it up and will use "dwarves" from now on. Funny language...
• 03-27-2009
Snafuist
Riddle #4: if-statement considered harmful
Quote:

I presume that includes ternary operator, and that this is either C or C++?
Yes.

Code:

`return (a >b)?1:(a<b)?-1:0;`
Nice try.

Quote:

(a>b)-(a<b)+(a==b)-(a==b)
Right! But you may safely skip the "+(a==b)-(a==b)" part.

Greets,
Philip
• 03-27-2009
laserlight
Riddle #3: three dwarfs, two paths, one life
I was inspired by this: Security.
• 03-27-2009
Snafuist
Riddle #3: three dwarfs, two paths, one life
And I was inspired by this: Labyrinth Puzzle.
• 03-27-2009
laserlight
Riddle #4: if-statement considered harmful
Oh, this is good. Every since iMalc pointed out that a simple return a - b; is vulnerable to arithmetic overflow, I have been rather depressed over having to use the more verbose ternary operator version, or resort to an if-else chain. So, return (a > b) - (a < b); is good enough :)
• 03-27-2009
Snafuist
Riddle #5: more dwarfs
Right!

Can someone else explain it?

Greets,
Philip
• 03-27-2009
Perspective
Riddle #4: if-statement considered harmful
They are not equivalent if I remember correctly. (a < b) will return 0 if false and something non-zero otherwise (but not necessarily 1). The original program returned 1.
• 03-27-2009
laserlight
Riddle #4: if-statement considered harmful
Quote:

Originally Posted by Perspective
(a < b) will return 0 if false and something non-zero otherwise (but not necessarily 1).

No, it will return 1 for non-zero (or true, in the case of C++, which will be converted to 1).
• 03-27-2009
Snafuist
Riddle #6: natural languages
I introduce two new adjectives fooish and barish.

A word is fooish if it denotes a property that it has itself.
A word is barish if it doesn't.

Examples for fooish words:
- "short" is a short word.
- "English" is an English word.
- "unfrequent" is an unfrequent word.

Examples for barish words:
- "long" is not a long word.
- "German" is not a German word.
- "rife" is not a rife word.

I claim that every adjective is either fooish or barish.

Is "barish" fooish or barish?

Greets,
Philip
• 03-27-2009
abachler
Number theory in 21 minutes
Quote:

Originally Posted by Sebastiani
>> Its faster to just check if a number, modulo 6469693230 has a prime remainder, since all prime numbers do.

Are you sure about that? Can you elaborate?

Yes I'm sure, no I won't elaborate. Well, maybe a little. Specifically, any prime number P modulo a primorial X such that P=> primorial X will have a prime remainder. Obviously it also works for P< X btu then you just check it agaisnt he known primes. This is not a guarantee that the number tested is prime, but it will guarantee it is composite if it fails. It will nto however give you any of the factors of the composite number. The greater the value of X the stronger the evidence for being prime. This method of checking very large primes is an embarrisingly parallelizable method. Since as X grows, it increases teh number of independant checks very quickly. It does however require a very large and comprehensive database of known primes for values of X larger than ~31. The size of the database approaches (X primorial / ln(X primorial)) * sizeof(N) where N is the type of teh storage unit for a single prime. For X = 23 and N is a DWORD its about 46MB, while for 29 its over 1GB, and it grows more rapidly after that.
• 03-27-2009
brewbuck
Riddle #4: if-statement considered harmful
This would be more fun if you also banned '<' and '>'
• 03-27-2009
whiteflags
Riddle #4: if-statement considered harmful
This reminds me when I listened to Steve Gibson talk about a version of assembly that had no OR. You had to implement your own.
• 03-27-2009
Snafuist
Riddle #4: if-statement considered harmful
Quote:

This would be more fun if you also banned '<' and '>'
I can see some ugly bit twiddling here. Any volunteers? :))

Quote:

This reminds me when I listened to Steve Gibson talk about a version of assembly that had no OR. You had to implement your own.
Nothing easier than that if you know that double negation doesn't change the value:
a OR b == !(!a AND !b)

I once had to show that AND and NOT form a logical basis, i.e. that one can express every boolean formula by only using AND and NOT (and variables, of course). An even smaller logical basis is NOR. Sounds like a good riddle...

A far more intriguing example of simplicity is the "One Instruction Set Computer", which only has a single instruction. One possible solution is the SBN instruction, subtract-and-branch-if-negative. It has three operands a, b and c. It works by subtracting the contents of memory location a from those at address b, storing the result at address b and jumping to c if *a > *b. Of course, this machine can be proven to be Turing-complete.

This takes the meaning of RISC to a higher level.

Greets,
Philip
• 03-27-2009
laserlight
Riddle #4: if-statement considered harmful
Quote:

Originally Posted by Snafuist
I once had to show that AND and NOT form a logical basis, i.e. that one can express every boolean formula by only using AND and NOT (and variables, of course). An even smaller logical basis is NOR. Sounds like a good riddle...

I had the impression that proving that NAND and NOR are universal gates is a pretty common assignment for first or second year computing and engineering students.
Show 80 post(s) from this thread on one page
Page 4 of 8 First 12345678 Last