| View previous topic :: View next topic |
| Author |
Message |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Fri Jun 25, 2010 8:52 am Post subject: Re: Small Programming Challanges :) |
|
|
| Montycarlo wrote: | I'm not a java coder so I don't know how the inverse-bit operator works in syntax.
Are either right? |
I think '~' will invert all bits and '!' will take the negative (which is inverting all bits and add one).
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Jun 25, 2010 11:53 am Post subject: |
|
|
Factorial. Mine is the fastest.
| Code: | #include <iostream>
template <unsigned int N>
struct Factorial {
enum { Result = N * Factorial<N-1>::Result };
};
template <>
struct Factorial<0> {
enum { Result = 1 };
};
int main()
{
std::cout << Factorial<30>::Result << std::endl;
return 0;
} |
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Fri Jun 25, 2010 2:48 pm Post subject: |
|
|
Nice solution, I wanted to give this challange later (factorial at compile time) but I guess I'll just think of another challange with classes and/or templates.
Anyway, Flyte: I did not request you to write a factorial function/template structure, look at the whole challange next time and not just the title.
In fact, I'll give you guys a hint and say calculating the factorial in this challange isn't even needed.
| Montycarlo wrote: | | Deltron Z wrote: |
7. (taken from a java book)
Without running, what will be the output of:
| Code: | public class JoyOfHex {
public static void main(String[] args) {
System.out.println(Long.toHexString(0x100000000L + 0xcafebabe));
}
} |
Explain. (3 points)
|
It will define a public class with a single member; the static function main which will print out 1cafebabe to the handler at System.out.
The function can be accessed and called using the dot notation: | Code: | //Reference: JoyOfHex.main
//Call: JoyOfHex.main() |
|
No, look at my solution on top of the 5th page.
| Montycarlo wrote: |
| Code: | public uint abs(int input){
//Assuming value is stored in 'Two's compliment' format
return input & (!input)
} |
I'm not a java coder so I don't know how the inverse-bit operator works in syntax.
Are either right? |
No. remember that x & NOT x = 0. (1 & 0 = 0, 0 & 1 = 0)
Use the sign bit, this is the way to do this.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Jun 25, 2010 4:11 pm Post subject: |
|
|
| Deltron Z wrote: | Anyway, Flyte: I did not request you to write a factorial function/template structure, look at the whole challange next time and not just the title.
|
| Code: | 11. Factorial. (2~5 points depending on solution)
Factorial (n!) means n × (n − 1) × ... × 3 × 2 × 1.
n! can also be described as the multiplication of prime numbers powers, for example:
5! can be described as 3 1 1 because 5! = 2^3 * 3^1 * 5^1. |
I do not see a question there. In fact, I do not even see you requesting anything. Perhaps you should finish writing the question before you make a fool of yourself.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Jun 25, 2010 7:06 pm Post subject: |
|
|
| Code: | cdq
xor eax, edx
sub eax, edx |
so
| Code: | inline int abs(int x)
{
__asm
{
mov eax, dword ptr [x]
cdq
xor eax, edx
sub eax, edx
}
} |
even the branching code will compile to branchless, i kinda cheated since i looked at the disassembly first.
otherwise, you can find this all over the place, it's well known
| Code: | inline int abs(int x)
{
int mask = x >> 31;
return (x ^ mask) - mask;
} |
but it hardly matters, the compiler does this optimization for you.
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Sat Jun 26, 2010 7:09 pm Post subject: |
|
|
| slovach wrote: | | Code: | cdq
xor eax, edx
sub eax, edx |
so
| Code: | inline int abs(int x)
{
__asm
{
mov eax, dword ptr [x]
cdq
xor eax, edx
sub eax, edx
}
} |
even the branching code will compile to branchless, i kinda cheated since i looked at the disassembly first.
otherwise, you can find this all over the place, it's well known
| Code: | inline int abs(int x)
{
int mask = x >> 31;
return (x ^ mask) - mask;
} |
but it hardly matters, the compiler does this optimization for you. |
Aren't you violating the "No conditions" requirement by using XOR?
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sat Jun 26, 2010 7:25 pm Post subject: |
|
|
| Odecey wrote: | | Aren't you violating the "No conditions" requirement by using XOR? |
Every action on a CPU is done using boolean logic. If you went that deep into it the question would be impossible as every instruction would violate the requirements.
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Sat Jun 26, 2010 10:51 pm Post subject: |
|
|
| Flyte wrote: | | I do not see a question there. In fact, I do not even see you requesting anything. Perhaps you should finish writing the question before you make a fool of yourself. |
My bad.
Write a program whose input is N and the output is the representation of N! as primes powers.
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Sun Jun 27, 2010 7:41 am Post subject: |
|
|
| Flyte wrote: | | Odecey wrote: | | Aren't you violating the "No conditions" requirement by using XOR? |
Every action on a CPU is done using boolean logic. If you went that deep into it the question would be impossible as every instruction would violate the requirements. |
Then I guess my next question is, how far is too far, and how is that made clear? Personally, I think Deltron Z should have rephrased it to something more like "Absolute value- Bitwise operators: Write a function which returns the absolute value of a number by only using bitwise and arithmetic operators."
Deltron Z: Why haven't you awarded Slovach points for the solution?
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Sun Jun 27, 2010 11:30 am Post subject: |
|
|
| Odecey wrote: | | Flyte wrote: | | Odecey wrote: | | Aren't you violating the "No conditions" requirement by using XOR? |
Every action on a CPU is done using boolean logic. If you went that deep into it the question would be impossible as every instruction would violate the requirements. |
Then I guess my next question is, how far is too far, and how is that made clear? Personally, I think Deltron Z should have rephrased it to something more like "Absolute value- Bitwise operators: Write a function which returns the absolute value of a number by only using bitwise and arithmetic operators."
Deltron Z: Why haven't you awarded Slovach points for the solution? |
I don't really notice points anymore, I begin to think it's a bad idea and just write the solver's name next to the challange.
Let's define a condition an operation that would change and use the flags. C: if, for, while, etc... ASM: CMP, TEST, Conditional Jumps, etc...
This also includes functions that has conditions in them (power, square root, etc...)
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Jun 28, 2010 2:55 pm Post subject: |
|
|
| how does power and square root have conditions in them ? it probably just gets translated to a 'fmul st0, st0' or something
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Mon Jun 28, 2010 10:31 pm Post subject: |
|
|
That's only a square.
A power requires loops, and if you look at the function's disassembly you will see conditions.
Anyway, stop trying to find loopholes and try to solve.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Jun 28, 2010 11:03 pm Post subject: |
|
|
| Deltron Z wrote: | Let's define a condition an operation that would change and use the flags. C: if, for, while, etc... ASM: CMP, TEST, Conditional Jumps, etc...
This also includes functions that has conditions in them (power, square root, etc...) |
You're aware that quite a few assembly instructions modify and use flags that aren't conditional expressions, right? Arithmetic instructions and the carry flag, for example.
| Slugsnack wrote: | | how does power and square root have conditions in them ? it probably just gets translated to a 'fmul st0, st0' or something |
Square root probably has a '>= 0' check.
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Jun 29, 2010 4:48 am Post subject: |
|
|
| Flyte wrote: | | You're aware that quite a few assembly instructions modify and use flags that aren't conditional expressions, right? Arithmetic instructions and the carry flag, for example. |
Just solve the damn challanges.
|
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Tue Jun 29, 2010 11:34 am Post subject: |
|
|
| max(a,b):(a+b+abs(a-b))/2
|
|
| Back to top |
|
 |
|