 |
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: Tue Jun 15, 2010 4:25 am Post subject: Small Programming Challanges :) |
|
|
Let's start with an interesting one, than a few easy ones with, hopefuly, increasing difficaulty.
__________________
Points:
tombana - 16 points.
Noz3001 - 1 point.
Uzeil - 3½ points.
slovach - 2 points.
__________________
*blue - solved.
*dark red - unsolved.
1. Declare 'i' in a way which will produce an infinite loop, any language: (1 point)
Solved - tombana was closest - ½ a point.
1.1. - Java/C# specific (and probably more) Declare 'i' in a way which will produce an infinite loop: (1 point)
Code: | while (i != i+0) { } |
Solved - solved by Noz3001.
Good luck. there are many more where these came from.
_____________________________________
2. Without executing the program, what will be the return value? explain. (2 points)
Note: results may vary - depending on compiler! (see explanation next page)
Code: | #include<stdio.h>
int func(int a, int b)
{
return a+b;
}
int funca(int a, int b)
{
if(a>1)
func(a,b);
else
return 2;
}
void main()
{
int a;
a=funca(3,2);
printf("%d",a);
} |
Look at the code carfully.
Solved - solved by tombana, 1½ point for Uzeil's good thinking
_____________________________________
3. Write a program that it's output is it's own code, exacly. (5 points)
Limitations: not allowed to read from files or anything tricky like that, just a smart algorithm.
Solved - solved by tombana.
_____________________________________
4. Write a program which outputs "Hello, World!" N times, N being a constant or input, whichever you like. however, the catch is that it must be done without any conditions! some languages has more ways solving this than another, however, there are also solutions available for any language. points may vary, depending on soltuion. (3~7 points, depends on solution)
Solved - Two interesting solutions by tombana.
_____________________________________
5. Coins Problems
5.1. The near by convenience store decided to buy an automatic cash register, but it doesn't seem to work properly. Devise an algorithm that receives an array/list of cash in the register (each coin is worth 1 unit, 2 units, 5 units, 10 units, 20 units, 50 units, 100 units and 200 units) and a variable, amount. returns true if amount can be produced by using the cash in the register or false if you can't give back amount of change. (2 points)
Example: for the amount 253 and cash in the register { 1, 1, 0, 0, 0, 1, 2, 1 }, aka 1 coin worth 1 unit, 1 coin worth 2 units, one coin worth 50 units, 2 coins worth 100 units and 1 coin worth 200 units, the return value will be true, since we can return 1x1, 1x2, 1x50 and 2x100 or 1x200. for the amount 254 the return value is false.
_____________________________________
5.2. Devise an algorithm that it's output is the best way to return change to a customer. (2 points)
We will define a best way to return change to be using most small, less-worthy coins and least big, worthy coins.
Example: if there are 50 coins of 1 unit and 50 coins of 2 units, 2 coin of 100 units and 1 coin of 200 units, it is possible to return { 50x1, 50x2 }, { 2x100 }, { 1x200 }, but the best way would be to return 50x1 and 50x2 coins.
_____________________________________
5.3. (The following problem had been taken from Project Euler - Problem 31)
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
(3 points)
_____________________________________
6. Some math, thinking and knowledge acquired during your life.
This clock is a special mathematicians clock. every hour, 1~12, is encoded in some way.
Explain every encoding to earn ¼ of a point.
Solved.
01:00 - Legendre's Constant.
02:00 - Sum of: 1/1 + 1/2 + 1/4 + 1/8 + 1/16 . When going on infinitely it reaches 2.
05:00 - The Golden Ratio, φ (Phi), is a mathematical constant and it is calculated by the formula (sqrt(5) + 1) / 2. so this is just the opposite formula, doubled, minus 1, squared.
06:00 - 3! means 3 * 2 * 1 which is 6
07:00 - 6.99999 (with 9 repeated infinitely) is equal to 7. (see below)
08:00 - The binary representation of 8. (10002)
09:00 - 214 is 9 in base 4. (4^1*2 + 4^0*1 = 4*2 + 1*1 = 8 + 1 = 9)
10:00 - Amount of combinations possible when choosing 2 out of 5 => 10 combinations
11:00 - hexadecimal representation of 11
12:00 - 12^3 = 1728
Proof of 6.999... = 7
x = 6.999...
10x = 69.999...
10x - x = 69.999... - 6.999...
9x = 63
x = 7
6.999... = 7
_____________________________________
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)
_____________________________________
8. Ugly Numbers. (2 points)
We shall define an ugly number to be a number whose prime factors are only 2, 3 or 5.
By defintion, the first ugly number is 1. the first few ugly numbers would be:
Code: | 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... |
Write a program that finds all ugly numbers below 1,000 and their sum. - solved by Uzeil.
_____________________________________
9. Java Unsigned Shift Operator. (5 points)
In Java, there are no unsigned numbers. however, there is an unsigned shift operator, >>> or <<<, which works like signed shift, >> or <<, except that instead of preserving the sign - it fills the sign bit with 0.
Thus, the following code should produce an infinite loop:
Code: | i = -1;
while (i != 0)
i >>= 1; |
Since this operator is preserving the sign bit.
Can you declare i and assign a value to it so the following code will produce an infinite loop? if so, explain.
Code: | while (i != 0)
i >>>= 1; |
_____________________________________
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)
Let's define a condition an operation that would change and use the flags in ASM. C: if, for, while, etc... ASM: CMP, TEST, Conditional Jumps, etc...
This also includes functions that has conditions in them (power, square root, etc...)
Solved - solved by slovach.
_____________________________________
10.2. Maximum Value - No Conditions. (2 points)
Write a function that returns the maximum value of 2 values without using any conditions.
Hint: you may use the absolute value function written before.
_____________________________________
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.
Write a program whose input is N and the output is the representation of N! as primes powers.
_____________________________________
12. Special Numbers. (2~5 points depending on solution)
Let S(n) be the sum of the digits of n.
We shall define a special number, 10 < An < 100, to be a number with the following properties:
i. The last 2 digits are n. (An % 100 = n. example: 1056 % 100 = 56)
ii. An is divisible by n. (An % n = 0. example: 5600 % 56 = 0)
iii. The sum of the digits of An is n. (S(An) = n. example: S(19) = 10)
Example for such a number is 910 - ends with 10, divisible by 10 and sums up to 10.
Write a function, A, to find the first special number for n.
Note: There is no such number A11. (where n = 11)
Last edited by Deltron Z on Sun Jun 27, 2010 11:34 am; edited 41 times in total |
|
Back to top |
|
 |
Evil_Intentions Expert Cheater
Reputation: 65
Joined: 07 Jan 2010 Posts: 214
|
Posted: Tue Jun 15, 2010 4:39 am Post subject: |
|
|
i = &otherVariable;
just a guess i guess
|
|
Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Jun 15, 2010 4:48 am Post subject: |
|
|
It doesn't matter. it still compares the two values using the operator != if such a definition exists.
It's kinda easy when you think about it, just requires a bit knowledge.
Even if you use pointers, it'll compare pointers and return false since they're equal.
And when writing this post, I just thought of another solution.
|
|
Back to top |
|
 |
Evil_Intentions Expert Cheater
Reputation: 65
Joined: 07 Jan 2010 Posts: 214
|
Posted: Tue Jun 15, 2010 5:13 am Post subject: |
|
|
I really have no clue, y brains pretty fried after Project euler today, and its 5 am here.
|
|
Back to top |
|
 |
Uzeil Moderator
Reputation: 6
Joined: 21 Oct 2006 Posts: 2411
|
Posted: Tue Jun 15, 2010 5:20 am Post subject: |
|
|
I can't tell what you mean. Declare it in a way? Why is it then used in while loops?
Well, if you're saying 'declare' it in a way that leads to an infinite 'loop', then...
Code: |
for (int i = 0; i == i; i++){ }
|
or
Code: |
public int getInt(){
int i = getInt();
return i;
}
|
_________________
|
|
Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Jun 15, 2010 5:24 am Post subject: |
|
|
No, I mean declare it and assign an initial value to it.
int i = 0, for example. anything else, isn't allowed.
What you have to do is assign i an initial value to produce an infinite loop, but as you can see, i = 0 will produce 0 != 0, false, which isn't an infinite loop.
|
|
Back to top |
|
 |
Uzeil Moderator
Reputation: 6
Joined: 21 Oct 2006 Posts: 2411
|
Posted: Tue Jun 15, 2010 5:32 am Post subject: |
|
|
Ah well then fuck it lol, I don't know why any compiler would ever let that give you an infinite loop. Any data should be equal with a cmp + jz/jnz, including negative numbers, pointers to arrays(including strings), and I can't think of a #DEFINE that would give this result either. Unless you were to rewrite the loop to (i-- != --i) or (--i != --I) or (i-- != i) for example.
You sure you're right on this one? lol
_________________
|
|
Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Tue Jun 15, 2010 5:34 am Post subject: |
|
|
Is it allowed to declare i as a class with an overloaded != operator that always returns true?
|
|
Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Jun 15, 2010 5:35 am Post subject: |
|
|
I'll give you a hint: it has nothing to do with the compiler. thing about the processing units... already said too much.
#define is not allowed.
I'll give it another shot, if nobody will solve I'll post the answer.
Edit:
tombana wrote: | Is it allowed to declare i as a class with an overloaded != operator that always returns true? |
Very nice solution, exacly what I thought of in my second post - but then when I thoght about it, I decided it's not allowed since overloading this operator is just like using a function, and then it'll look like:
Code: | bool Compare(int a, int b)
{
return true;
}
while (Compare(i, i)) { } |
So, no. it's also OOP only, the solution should work in any language, ASM too! (I guess there are exceptions, languages that perform their own compares on some data types, but ignore these - possible in ASM, C, C++, C#, Java, etc...)
Last edited by Deltron Z on Tue Jun 15, 2010 5:37 am; edited 1 time in total |
|
Back to top |
|
 |
Uzeil Moderator
Reputation: 6
Joined: 21 Oct 2006 Posts: 2411
|
Posted: Tue Jun 15, 2010 5:36 am Post subject: |
|
|
You can overload the != operator? Haha
Then again, I guess going back to the #DEFINE idea that would work(though it wouldn't be 'overloaded' since you wouldn't be able to access the old one)
_________________
|
|
Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Jun 15, 2010 5:38 am Post subject: |
|
|
I've edited my post.
|
|
Back to top |
|
 |
Uzeil Moderator
Reputation: 6
Joined: 21 Oct 2006 Posts: 2411
|
Posted: Tue Jun 15, 2010 5:41 am Post subject: |
|
|
I'm awaiting the answer because I find it hard to believe that this could be possible without #DEFINE or changing the while loop parameters otherwise. :s
_________________
|
|
Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Tue Jun 15, 2010 5:42 am Post subject: |
|
|
Maybe a float/double with some special value? Like a 'signed zero' value. (EDIT:) Or infinity.
|
|
Back to top |
|
 |
Evil_Intentions Expert Cheater
Reputation: 65
Joined: 07 Jan 2010 Posts: 214
|
Posted: Tue Jun 15, 2010 5:44 am Post subject: |
|
|
I gotta get this answer...the c++ maggots are crawling out of my eyes in despiration.
sry..im a litte f'd in the head atm.
|
|
Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Jun 15, 2010 5:44 am Post subject: |
|
|
Code: | double i = double.NaN;
while (i != i) { } |
You guys might want to have a look here...
Quote: | It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN. |
And here's another quote:
Quote: | The IEEE 754 floating point standard states that comparing NaN with NaN will always return false. If you must do this, use Double.isNaN(). |
Posting a new challange in a minute at the first post.
Edit:
tombana wrote: | Maybe a float/double with some special value? Like a 'signed zero' value. (EDIT:) Or infinity. |
Aw, so close. you were at the right direction, however Infinity can be compared... NaN's the answer.
Damn, I'm too slow, everytime I post something, 3 people post new ideas.
Good job.
P.S. nobody has solved 1.1 - C# or Java. also posted another challange.
Last edited by Deltron Z on Tue Jun 15, 2010 5:49 am; edited 1 time in total |
|
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
|
|