 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Jun 16, 2010 5:39 am Post subject: |
|
|
| Uzeil wrote: | For #7:
Either you're going to get a compilation error for putting a 32-bit together with a 64-bit, or it's going to let you add them and give you 0x1cafebabe. Though I have no idea how Java outputs it's hex function. An issue I've been having with this thread is I haven't bothered to install any compilers apart from delphi since my recent reformat D: Then again, another possibility is that, since you said in another problem there isn't such a thing as an unsigned integer, and the first bit in 0xC is set, that would be treated as 0x100000000L - 0x4afebabe (since 0xC AND 7 would give you 4.. binary(1100 AND 0111)), which would give you what... oh god hexadecimal subtraction I'm pulling out the calculator. 0xB5014542
|
You've got the right approach, but not exact. As I said, there are no unsigned numbers in Java, that is why this number is in fact signed, meaning it's representation is -889275714, so int 32-bit integer it's hex representation is 0xCAFEBABE, but you should also notice that converting one integer type to a larger integer type will preserve the sign, therefore, if the sign bit is off (0) the remaining digits will be filled with 0x0 (0000), otherwise, if the sign bit is on (1), the remaining digits will be filled with 0xF (1111), and thus, the long version of 0xCAFEBABE is 0xFFFFFFFFCAFEBABE, that is because the sign bit cancels the other bits out, we can easily see that in -1 in 8 bits which would be represented as 1111, so 2^0 + 2^1 + 2^2 - 2^3 (sign bit) turns to 1+2+3-8 = 7 - 8 = -1.
To get the answer, we must follow this process:
a. Convert 0xCAFEBABE to long.
b. Add 0x100000000L to it.
c. The result number is then passed to the function, aka, the answer.
As mentioned befoe, 0xFFFFFFFFCAFEBABE is the long version, and by adding 0x100000000L we will get this:
0xFFFFFFFFCAFEBABE
0x0000000100000000
__________________
Adding 0's to CAFEBABE results in CAFEBABE.
No we shall add 1 to F, we get 10, and thus, the result is 0 and remembering 1.
Then we shall add 0+F + the 1 remembered before, and once again, we shall get 10 - take 0, remember 1.
We repeat the process until the sign bit. once the remembered 1 is out of range we simply throw it away to get 0x00000000CAFEBABE.
I liked this question, it shows quite well how confusing and dangerous data-loss can be when converting types, so be carful.
| Uzeil wrote: | For #9:
You say "can you declare" one, so I'm going to say no. Simply because for each time you shift right, since the first bit is turned to 0, it would eventually(within either 32 or 64 shifts) become all unset bits. |
When I wrote this challange, I didn't know the answer myself. I was confused by it, so I thought maybe there's no trick, it's simply misleading and so... anyhow, there is a declaration for 'i' that will produce an infinite loop. I'm not quite sure myself about the explanation, but the answer would be short i = -1. that is becuse, as in before, there's data loss in here. however, in here we use the data-loss to, I don't wanna say prevent data-loss, but to prevent the value from changing.
From what I've understood, the operator works on 32-bit integers (or maybe just not working very good with short) and needs to convert the value to 32-bit integer, perform the shift operation and then convert to 16-bit integer once again.
The process looks like this:
i = 0xFFFF; the conversion will preserve the sign bit, thus, filling with 1's (0xF's) and we get 0xFFFFFFFF. the operation shifts the bits right, setting 0 at the sign bit ignoring what it is, and thus, we have 31 bits on and the sign bit off, 0x7FFFFFFF. when converting back to 16-bit integer, there is data-loss, and (short)0xXXXXYYYY = 0xYYYY, we take the lower 16-bits, in our case, 0xFFFF. the value is the name, making the process repeat itself infinitely.
| Uzeil wrote: | EDIT:
For #8:
| Code: | program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
I: Integer;
tempint: Integer;
sum: Integer;
buffer: string;
begin
sum := 0;
for I := 1 to 1000 do
begin
tempint := I;
while (((tempint mod 2) = 0) or ((tempint mod 3) = 0) or ((tempint mod 5) = 0)) do
begin
if (tempint mod 2) = 0 then
tempint := tempint div 2;
if (tempint mod 3) = 0 then
tempint := tempint div 3;
if (tempint mod 5) = 0 then
tempint := tempint div 5;
end;
if tempint = 1 then
sum := sum + I;
end;
writeln(IntToStr(sum));
ReadLn(buffer);
end. |
Result: 24355 |
Your code is working, but it doesn't mean that it's any good. First, I asked for the first 1,000 ugly numbers, not all ugly numbers below 1,000. your code might take up to triple the time than what the very same algorithm can take when written properly.
First, you perform 2, 4 or 6 mod operations every time! depending on the factors of the number. if the remaining factors contain 5, you will perform 6 such operations!
The only case where your algorithm would perform the same amount of operations is for numbers whose factors are 2, 3 and 5 and appear the amount as the rest, for example 2*3*5 or 2*2*3*3*5*5.
You should've used a while-loop for each factor seperatly, like so:
| Code: | #include<stdio.h>
int isUgly(int n)
{
static int Divisors[3] = { 2, 3, 5 };
int i = 0;
while (n > 1 && i < 3)
{
if (n % Divisors[i] == 0)
n /= Divisors[i];
else
i++;
}
return (n == 1);
}
int main()
{
int Count = 0;
unsigned long long Sum = 0;
for (int i = 1; Count < 1000; i++)
{
if (isUgly(i))
{
printf("%d ", i);
Count++;
Sum += i;
}
}
printf("\n\n%d ugly numbers found and their sum is %llu.\n", Count, Sum);
getchar();
return 0;
} |
And by that save a lot of mod operations. imagine the number 5^13 - you will perform 13 mod operations for checking for 2 in the while loop's condition and another 13 checking for 3 and then another 13 for each inside the loop, making it a total of 13*4 = 52 operations + you still perform the same expensive operation to divide the number by 5, you could use just 1 mod and 1 division to do this instead of 2 mods.
| Uzeil wrote: | EDIT:
For #5.2 (Best Choice): The following algorithm:
| Code: | //First I start out by changing the array to enumerate the coins instead of list how many of each type of coin there are. (for instance, the array 2,1,0,3,0,0,0,0 would become 1,1,2,10,10,10), then I pass that array to this algorithm.
function TForm1.generateChange(coins: array of Integer;
Amount: Cardinal): Boolean;
var
sum: Cardinal;
I: Cardinal;
BestChoice: array of Integer;
tempstr: String;
Current: Int64;
Max: Int64;
begin
if Amount = 0 then
begin
Result := True;
Exit;
end;
Result := False;
if length(coins) = 0 then
Exit;
setlength(BestChoice, 0);
Max := Trunc(power(2, length(coins)));
Current := 0;
while Current < Max do
begin
sum := 0;
for I := 0 to length(coins) - 1 do
if ((Current shr I) and 1) <> 0 then
sum := sum + coins[I];
if sum = Amount then
begin
setlength(BestChoice, length(coins));
for I := 0 to length(coins) - 1 do
if ((Current shr I) and 1) <> 0 then
begin
BestChoice[I] := coins[I];
end
else
BestChoice[I] := 0;
Break;
end;
Current := Current + 1;
end;
if length(BestChoice) <> 0 then
begin
Result := True;
tempstr := '';
for I := 0 to length(BestChoice) - 1 do
if BestChoice[I] <> 0 then
tempstr := tempstr + IntToStr(BestChoice[I]) + ' ';
WriteLn('Best choice is: ' + tempstr);
end
else
WriteLn('Change could not be generated.');
end; |
This can end up taking a very, very long time. This same algorithm can be used for 5.1 and 5.3 |
Oh oh! very very long time, eh? yeah, I could see that as soon as I read the comment about the algorithm... no offence, but this algorithm is pretty base and no it cannot be used in 5.3 as there are over 70,000 possiblities, and if finding one way takes too long for you then you won't be able to solve 5.3.
Anyway, the algorithm is very simple and should not take more than 5 lines, I solved 5.3 with 3 lines only and for the best way in 5 lines.
| Uzeil wrote: | I just really don't feel like spending the time siting there waiting for the answer to pop out. So, since I want to steal the points anyway, here's how to do it:
...
...
Sooo that's 5.1, 5.2, and 5.3 . I "devised" the algorithms, (though they're pretty slow LOL, as, for example, I didn't bother to make them realize things like how if you had 90 pieces of 1, and you wanted the number 56, it wouldn't go "well there's only one possibility if you ignore order" since it's all pieces of 1 in this case). |
You got that right...
| Uzeil wrote: | Hell, all of the #5 parts are only worth 2 points anyway Did you really want something with great power? |
I thought the challanges are quite simple and I find them very easy... I guess that's just me and in this case maybe I should give more points on each, however, they are all the same, and that's a total of 2+2+3 = 7 points for the same question where one takes 4 lines, the other 5 and the 3rd takes 3 lines.
| Uzeil wrote: | EDIT:
I just read in the other thread you saying you solved 5.3 in 3 lines of recursion. I'm curious as to how, considering the following:
Summing up to make sure you get 200
Only using the possible coin options
How did you form your parameters? I can't stop thinking of how you'd had us do 5.1 and 5.2, and I can't stop thinking of similarly using an array to be able to recursively do this. The only thing I can think is that you used external function calls within the recursion(like summing up the array?) |
I'll let people a few more tries at it, if nobody solves - I'll show my solution.
But since you're curious and I can't help it, the signature looked like this:
| Code: | | int Problem31(int Amount = 200, int Index = 0) |
(default values )
And just before you make wrong assumptions about my code, there aren't 239758 calls to Problem31 checking all of the coins, 1, 2, 5, 10, 20, 50, 100 and 200 - just 2 calls, and I might tell too much by this but 1 is for checking and 1 is for skipping.
|
|
| Back to top |
|
 |
Uzeil Moderator
Reputation: 6
Joined: 21 Oct 2006 Posts: 2411
|
Posted: Wed Jun 16, 2010 6:02 am Post subject: |
|
|
Aye I knew when I submitted that my code was a bare as it gets haha, I'm doing all of this because I'm so extremely mindfucked on something else that I keep needing breaks. I don't want to mindfuck myself as a break for being mindfucked Anywho, for the 'ugly numbers' one, I recognized the idea of doing three different algorithms before I even started writing, I just didn't want to write that much Second, delphi's compiler wouldn't do all three operations every time as, unless you uncheck a certain compiler option, whenever anything in an 'or' returns true(or similarly, anything in an 'and' returns false), it doesn't bother to check the rest.
For example, you could do this:
| Code: | | if (length(myarray) > 0) and (myarray[0] = 5) then | and you wouldn't get an exception when myarray is length 0, because it wouldn't go on to check the second condition.
and yeah I misread your question for the 'first 1,000' instead of 'first below 1,000'
Why can't it be used for 5.3? It would just take all day I see nothing wrong with this :whistles:
Anywho, at what point did this become about 'good, optimised code' instead of just 'whatever gets an answer to the question... eventually'
Anywho, I have nooooo clue how you came up with that recursion. I'll try to think it up later if no one else does Though I doubt I will.
Out of curiosity: How long have you been programming? What's your background o_O
EDIT:
So for fun(really just laziness) I kept it how it was, just threw in a Count variable.
| Code: | var
I: Cardinal;
tempint: Integer;
sum: Int64;
buffer: string;
Count: Cardinal;
begin
sum := 0;
Count := 0;
I := 1;
while Count < 1000 do
begin
tempint := I;
while (((tempint mod 2) = 0) or ((tempint mod 3) = 0) or
((tempint mod 5) = 0)) do
begin
if (tempint mod 2) = 0 then
tempint := tempint div 2;
if (tempint mod 3) = 0 then
tempint := tempint div 3;
if (tempint mod 5) = 0 then
tempint := tempint div 5;
end;
if tempint = 1 then
begin
sum := sum + I;
Inc(Count);
end;
Inc(I);
end;
writeln(IntToStr(sum));
ReadLn(buffer);
end. |
Took about 5 seconds to finish(32-bit).
Result: 7225005911
And then because I know you want to ask,
I rewrote it to
| Code: | while (tempint mod 2) = 0 do
tempint := tempint div 2;
while (tempint mod 3) = 0 do
tempint := tempint div 3;
while (tempint mod 5) = 0 do
tempint := tempint div 5; | and it took about the same amount of time. I'm sure if I increase the count goal to a larger number there will start to be a notable difference, but for now: Nope :s
_________________
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Jun 16, 2010 6:21 am Post subject: |
|
|
| Uzeil wrote: | Aye I knew when I submitted that my code was a bare as it gets haha, I'm doing all of this because I'm so extremely mindfucked on something else that I keep needing breaks. I don't want to mindfuck myself as a break for being mindfucked Anywho, for the 'ugly numbers' one, I recognized the idea of doing three different algorithms before I even started writing, I just didn't want to write that much Second, delphi's compiler wouldn't do all three operations every time as, unless you uncheck a certain compiler option, whenever anything in an 'or' returns true(or similarly, anything in an 'and' returns false), it doesn't bother to check the rest.
For example, you could do this:
| Code: | | if (length(myarray) > 0) and (myarray[0] = 5) then | and you wouldn't get an exception when myarray is length 0, because it wouldn't go on to check the second condition. |
I know this - it had a name, I've just forgot... but you didn't read what I've written - I gave you certain cases where this is the case, where mod 2 and mod 3 are never true - extra 52 checks, 68 if you count the division after a secondary condition.
Anyway, it is known that the compiler would optimize conditions, so when 1 || x then x won't be checked, so if x is a function it won't be executed.
Same with 0 && x - obviously false.
Anyway, everything I've said still applies. I choose my words carfuly.
| Uzeil wrote: | and yeah I misread your question for the 'first 1,000' instead of 'first below 1,000'
Why can't it be used for 5.3? It would just take all day I see nothing wrong with this :whistles: |
Becuase Project Euler's got the "1 minute rule" - a solution is not valid if it takes up to a minute. most solutions should take less than 3~5 seconds, depending on algorithm, but in this question, it runs too fast for the computer to count so I get 0.000 seconds.
Ofcourse they can't check how long your algorithm takes, but nobody really cares, people solve these problems for themeselves, not to prove anything to anyone but themeselves.
| Uzeil wrote: | Anywho, at what point did this become about 'good, optimised code' instead of just 'whatever gets an answer to the question... eventually'  |
I thought "good optimised code" was the default.
Anyway, most algorithmic challanges aren't that difficault to take too long, but as mentioned before, if Project Euler wants you to solve problem 31 (5.3 here) under a minute I think you should do the same here, as it is the very same questio...
| Uzeil wrote: | Anywho, I have nooooo clue how you came up with that recursion. I'll try to think it up later if no one else does Though I doubt I will.
Out of curiosity: How long have you been programming? What's your background o_O |
No worries, you're doing a good job so far. just a little more optimizations...
Anyway, I've known programming since I was about 15 but only started learning seriously when I was 15 and a half at least, at about 10th grade. Now I'm 17, although I was solving Project Euler problems for over a year now (actually, I haven't visited there in a while - untill recently ), and that's pretty much it... learned from a book, some people met on the internet, forums... that's all it takes. there was a time I kinda stopped with computers, because of lack of interest and I was kinda stuck, but then I realized computers are my thing and I'd love to do something with it sometime... as in, working in some big projects or working for people, I could really use the cash.
Edit: oh, I see you solved it... good job, the answer seems to be correct, I haven't checked but I remember it started with 77...
|
|
| Back to top |
|
 |
Uzeil Moderator
Reputation: 6
Joined: 21 Oct 2006 Posts: 2411
|
Posted: Wed Jun 16, 2010 6:36 am Post subject: |
|
|
o_O Project Euler has no such rule. It has that rule for the people who make the questions - that it must be solvable with an algorithm that shouldn't take longer than 1 minute on a reasonably-equipped computer. The solver has no such limitation, just that they should keep in mind that it is in fact possible if they're interested.
Hoooly shit you took a different approach than I did haha. I spent all my hobbyist programming time hacking and seeing what I could do to manipulate code and create GUIs that everybody loves. I spent time learning optimization in terms of how long which operations take and such so that, if I so choose, I can dissect algorithms to the point of knowing how to write something so that a slight modification can heavily increase speed.
You on the other hand spent it like learning an intense way of solving puzzles. I like the mindset, and I plan on picking it up within the next year, but for now I stay impressed with some of the creative ideas you've come up with / displayed here. Just the idea of using that key 'else' in the while loop was a pretty damn good idea that I almost certainly wouldn't've come up with.
Anyway, to compare histories: Almost identical time-frame-wise, but instead I started that 15 -> 15 1/2 learning assembler and CE's auto assembler / memory editing. The rest I spent improving on more 'bare' assembler(though I honestly never got around to float operations, still don't plan to for a good while >_>), and picked up what languages I could before I started making my own hacking programs and adding onto CE.
Anywho, all of it is generally easier than this, but a compleeeeetely different style of thought. Big projects, keeping everyone in mind, analyzing and manipulating what's already been done instead of starting it yourself, etc. Though I have a feeling if you can come up with all of this you'd be good to go on most of what I've worked on
I feel jealous that for all the time I spent bypassing anti-hacking programs/methods, you got to just keep improving with that goal in mind
Anywho, I'm now just about to turn 20, just picked programming back up about 2 months ago after a 2+ year break. Hopefully you won't get tired of learning programming, programming, and then more programming after 2 years or so like I did.
EDIT:
and I don't quite understand: Why have you spent so much time learning multiple languages? I've noticed you mentioning Java, C#, PHP, C, and ASM already. How does learning a lot of languages and their technicalities(learning syntax is one thing, bu you seem to be learning the in-depth stuff) mix with the project euler mentality? (I never heard of project euler until.... yesterday.)
_________________
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Jun 16, 2010 6:59 am Post subject: |
|
|
| Uzeil wrote: | | o_O Project Euler has no such rule. It has that rule for the people who make the questions - that it must be solvable with an algorithm that shouldn't take longer than 1 minute on a reasonably-equipped computer. The solver has no such limitation, just that they should keep in mind that it is in fact possible if they're interested. |
| Project Euler wrote: | I've written my program but should it take days to get to the answer?
Absolutely not! Each problem has been designed according to a "one-minute rule", which means that although it may take several hours to design a successful algorithm with more difficult problems, an efficient implementation will allow a solution to be obtained on a modestly powered computer in less than one minute. |
As mentioned before, they can't check it, but it is highly "advised" to solve it under 1 minute. and I expect you to devise algorithms that are not only working but efficient. efficiency had many aspects, as in time and space - time and memory it takes. you could also reffer "good" to an algorithm that is short in code, and yet readable, even self-explanotary, but usually the implementation is ignored when the algorithm is obvious and it is trivial to write it efficiently.
| Uziel wrote: | | Anyway, to compare histories: Almost identical time-frame-wise, but instead I started that 15 -> 15 1/2 learning assembler and CE's auto assembler / memory editing. The rest I spent improving on more 'bare' assembler(though I honestly never got around to float operations, still don't plan to for a good while >_>), and picked up what languages I could before I started making my own hacking programs and adding onto CE. |
I must agree - I also started at this age learning some ASM, looking at the forum, tired of "leeching hacks" for lame games. I guess I knew there won't always be someone to give me hacks and teach me how, so I've decided to learn - and then I've got a whole different perception of computers, hacking and programming. what first was intended to be learned to hack a game then used to learn programming generally, why wasting time on a specific topic in computers for one specific game? computers is a very wide topic.
Anyway, as I said, learned only CE's auto-assembler syntax and until today I probably won't be able to write a 32-bit ASM application, but I certainly did make some nice hooks and trainers at the beginning. at some point, I realized hacking is just a skill which is an "extra" for learning computers - how things work... when you know how things work, even if you never learned about hacking, you can probably hack it better than if you'd just learnd hacking. I guess you could say I don't hack anymore, but that's not quite true. I always like messing with the compiler and make new hacks, then post it as a challange on forums, but that's pretty much it... I guess I could hack a few simple CrackMe's whose trivial to me, but I'm probably lacking some hacking techniques.
| Uzeil wrote: | I feel jealous that for all the time I spent bypassing anti-hacking programs/methods, you got to just keep improving with that goal in mind  |
And I'm jealous that you kept improving your hacking skills.
I sure did loved hacking, as a white hat (never really liked black hat...), but as I said, I guessed it'd be better to just learn how things work by learning programming and then analyzing it on a lower level of the application before I jump to simply hacking it.
|
|
| Back to top |
|
 |
bhpianist Cheater
Reputation: 1
Joined: 17 Apr 2010 Posts: 38
|
Posted: Wed Jun 16, 2010 5:41 pm Post subject: |
|
|
new #8?
| Code: | #include <stdio.h>
int main()
{
int nTotal = 0, nSum = 0;
const int ca_iNumbers[3] = {2, 3, 5};
for(int i = 0; nTotal < 1000; i++)
{
for(int j = 0; j < sizeof(ca_iNumbers)/sizeof(int); j++)
{
if(i % ca_iNumbers[j] == 0)
{
nTotal++;
nSum += i;
break;
}
}
}
printf("the sum is %d", nSum);
} |
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Jun 16, 2010 5:49 pm Post subject: |
|
|
There are two major mistakes: (I didn't run this code)
1. An ugly number contains only factors less than 5, not a factor less than 5. you should've used "while" instead of "if" and divide instead of break. only then add to the sum.
2. The numbers and sum are too big to fit in int. it will overflow.
|
|
| Back to top |
|
 |
Uzeil Moderator
Reputation: 6
Joined: 21 Oct 2006 Posts: 2411
|
Posted: Thu Jun 17, 2010 12:16 am Post subject: |
|
|
Baw I haven't been given my points
_________________
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Thu Jun 24, 2010 5:59 am Post subject: |
|
|
And we're back with new challanges.
Questions 10~12 are available at the first post.
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Jun 24, 2010 6:52 am Post subject: |
|
|
assuming we're allowed to use the c standard library, abs() is a function. if not, then the rip of abs :
| Code: | int __cdecl abs (
int number
)
{
return( number>=0 ? number : -number );
}
__int64 __cdecl _abs64(
__int64 num
)
{
return (num >=0 ? num : -num);
} |
max() is also a c standard library function.
my factorial solution :
| Code: | int Fac( int num ) {
if( num == 1 )
return 1;
return num * Fac( num - 1 );
} |
i have no idea on your explanation of factorial. you seem to be using a different definition to any i've ever seen before..
your last question is confusing. is 'An' an atomic term or is it the product of 'A' and 'n' ?
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Thu Jun 24, 2010 7:06 am Post subject: |
|
|
| Slugsnack wrote: | assuming we're allowed to use the c standard library, abs() is a function. if not, then the rip of abs :
| Code: | int __cdecl abs (
int number
)
{
return( number>=0 ? number : -number );
}
__int64 __cdecl _abs64(
__int64 num
)
{
return (num >=0 ? num : -num);
} |
|
That function uses conditions, which is exactly what he doesn't want.
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Thu Jun 24, 2010 11:33 am Post subject: |
|
|
| Slugsnack wrote: | assuming we're allowed to use the c standard library, abs() is a function. if not, then the rip of abs :
| Code: | int __cdecl abs (
int number
)
{
return( number>=0 ? number : -number );
}
__int64 __cdecl _abs64(
__int64 num
)
{
return (num >=0 ? num : -num);
} |
max() is also a c standard library function.
my factorial solution :
| Code: | int Fac( int num ) {
if( num == 1 )
return 1;
return num * Fac( num - 1 );
} |
i have no idea on your explanation of factorial. you seem to be using a different definition to any i've ever seen before..
your last question is confusing. is 'An' an atomic term or is it the product of 'A' and 'n' ? |
As tombana said, you missed the whole point. you're not supposed to use conditions.
And "An" is supposed to be the nth term of the group A.
If you want, just ignore "A" and treat An as n - same meaning. I just used A because later I planned to bring another challange to test the efficiency of this one. In case you wondered, A is the group of the smallest numbers with the mentioned proprties.
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Fri Jun 25, 2010 6:18 am Post subject: |
|
|
I had a go at task 10.1, and thought I'd found the solution when I came with up with the idea to do a leftshift, and then a logical rightshift, removing the sign bit. This does not work, and I'm wondering why (This is in Java):
| Code: |
int Abs(int val)
{
val <<= 1;
val >>>=1;
return val;
}
|
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Fri Jun 25, 2010 6:26 am Post subject: |
|
|
| Odecey wrote: | I had a go at task 10.1, and thought I'd found the solution when I came with up with the idea to do a leftshift, and then a logical rightshift, removing the sign bit. This does not work, and I'm wondering why (This is in Java):
| Code: |
int Abs(int val)
{
val <<= 1;
val >>>=1;
return val;
}
|
|
Look up how 2s complement works and you'll find the difference between x and -x is not just the first bit
|
|
| Back to top |
|
 |
Montycarlo Expert Cheater
Reputation: 0
Joined: 10 Jan 2007 Posts: 124
|
Posted: Fri Jun 25, 2010 7:28 am Post subject: Re: Small Programming Challanges :) |
|
|
| 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() |
| Deltron Z wrote: |
10.1. Absolute Value - No Conditions. (2 points)
Write a function that returns the absolute value of a number without using any conditions (if, for, while, etc...) or external functions (like pow and sqrt) |
| 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?
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|