Sunday, June 28, 2009

Puzzle 37 – Fun with Strings (BirthDay Blues)

Language – Java | Type – Concept | Last date 1-Jul-2009 9:00 p.m. IST | Points 3

It’s my birthday today (June 28th) – so I got one of those Happy Birthday puzzles I have been saving up for so long!
Strings are one of the most used Java classes – so how well do we know Strings? Here another puzzle on Strings.

package com.twisters;
class Stingify{
public static void main(String args[]){
String firstOne
= new String("Happy Birthday");
String secondOne
= new String("Happy Birthday");
/* No change to the line below! */
System.out.println(
"First String is equal to second : " + (firstOne == secondOne));
}
}

The first string is pretty much equal to the second. I think it’s fair that the output should be First String is equal to second: true.

Just a few simple conditions:
1. Don’t make any changes to the line which has the print statement
2. Three semicolons are more than enough in this program. No additional semicolons.
3. The usual rule – add as many characters that you like but no deleting characters. Commenting any of the existing lines of code is equivalent to deleting it.

P.S. - I think there might be couple of hints and rambling coming across on Twitter.
P.S.2 - I still looking for folks who want to test drive the Java Treasure Hunt. Any takers?

Got an answer? Do leave it here.

Puzzle 36 – Solution

This puzzle has loads of possible optimization – here are my action packed 18 characters!
The comments for each optimization should be self explanatory.
package com.sam.twisters.euler;
/*
* A Pythagorean triplet is a set of three natural numbers,
* a < b < c, for which,
* a^(2) + b^(2) = c^(2)
* For example, 3^(2) + 4^(2) = 9 + 16 = 25 = 5^(2).

* There exists exactly one Pythagorean triplet for which a + b + c = 1000.
* Find the product abc.
*/
public class Prog9 {

public static void main(String[] args) {
long startTime = System.nanoTime();

for(int a=1;a<1000;a++){
/*
* Optimization -->for(int b=1;b<1000;b++){ (5 char deleted 1000 added 'a')
* In a Pythagorean triplet a > b & c
*/
for(int b=1;b<a;b++){
/*
* Optimization -->for(int c=1;c<1000;c++){(6 char deleted 1000 added '=b')
*/
for(int c=1;c<=b;c++){
if(a*a == b*b+c*c && a+b+c == 1000){
System.out.println(a
*b*c);
/*
* Optimization -->a=1000; (7 char)
* At this point we have found our solution and we need to break out
* of the Loop. Breaking out of the outer for loop makes most sense
*/
a
=1000;
}
}
}
}

long estimatedTime = System.nanoTime() - startTime;
System.out.println((
float)estimatedTime/1000000000);
}
}

http://www.blogtrog.com/code.aspx?id=d83d3910-5e47-4c86-ae5e-b36ec47f7b57

There were a couple of solutions like,
for(int b=1;b<1000-a;b++){
for(int c=1000-a-b;c<1000;c++){
while this solution works when the sum is 1000 it fails when you go for higher numbers say 10,000 - truth is I think the algorithm at worst is incorrect or at least arbitrary - whats the reasoning behind 1000-b? However since it works for 1,000 I considered these solutions to be correct!

I usually don't reply to all the comments (too time consuming) - but there were plenty of good suggestions/solutions so here my 2 cent.

@Arshia - Nice use of the cut-paste rule. Pretty neat of you to align the code with the comments.

@Sebastian - I think my code is fine. I just renamed the variable - the 'c' in the comments correspond to the 'a' in my code -:). Hence I find a triplet where a*a = b*b + c*c instead of c*c = a*a+b*b - Same thing isn't it?
Reminds me, Trust only the code not the comments!!

@George Pólya - Your solution gets the award for the best abuse of rules -:)! Pretty neat!!

@TheMalkolm - Interesting Idea about posting the fastest code - but it would require analyzing the code - not just running all codes - after all the algorithm might be good for 1000 but what about 10,000 or 100,000?
Plus even if I just decided to run each solution - it's a lot of work - to run each solution to check the amount of time it takes!

@Makkhdn - How did you count 18. I made the exact same changes and counted 12.

I would also like to thank you folks for pointing out the problem you faced while
pasting the solution - with the '<' and '>'. I do have a suggestion folks - instead of posting the long code directly in the comments you could use the excellent service provided by Dave at www.blogtrog.com
You could just paste the link provided by at Blogtrog as the solution - could save a lot of trouble with illegal characters while adding them in comments here.

Wednesday, June 24, 2009

Puzzle 36 – Triple the fun (Optimization Series puzzle - 2)

Language – Java | Type – Concept | Last date 28-Jun-2009 9:00 p.m. IST | Points 3

If you have not looked at code optimization puzzle before – I would suggest having a look at the
first one!

Unlike the previous puzzle where the un-optimized code took over 30 minutes to run, I have already done a pretty good job on this one. It executes in about 5 seconds. Well 5 seconds is still a lot of time. Can you get it to execute in less than half a second?


package com.sam.twisters.euler;
/*
* A Pythagorean triplet is a set of three natural numbers,
* a < b < c, for which,
* a^(2) + b^(2) = c^(2)
* For example, 3^(2) + 4^(2) = 9 + 16 = 25 = 5^(2).

* There exists exactly one Pythagorean triplet for which a + b + c = 1000.
* Find the product abc.
*/
public class Prog9 {

public static void main(String[] args) {
long startTime = System.nanoTime();
/*
* For each possible pair of numbers below 1000
* check if there is a Pythagorean triplet for which a + b + c = 1000.
*/
for(int a=1;a<1000;a++){
for(int b=1;b<1000;b++){
for(int c=1;c<1000;c++){
if(a*a == b*b+c*c && a+b+c == 1000){
System.out.println(a
*b*c);
}
}
}
}

long estimatedTime = System.nanoTime() - startTime;
System.out.println((
float)estimatedTime/1000000000);
}
}

http://www.blogtrog.com/code.aspx?id=bd1246e9-25f6-4f9a-b983-486745ae457c

Rules? Same as before –

1. This code takes about 5 seconds to run. Get it to run in under half a second.
2. You see while it’s really great that you have decided to help me – I really don’t want you to mess up my entire code. So here is the deal – you can change a maximum of 18 characters (yes that’s all).
3. What constitutes a character change?
a. Inserting a character – each character (including space) that you add to the existing code.

b. Deleting a character – each character (including space) that you delete from the existing code.

c. Over writing a character – well you cannot do that. You could delete and insert but you cannot over write.

d. Cut Paste - Special Rule for cut paste is you can cut code from the existing code and paste it somewhere (except inside a comment) else in the code without using up a single char.
Just remember its cut-paste and not copy-paste.

Hint – There are a few comments in the code, I sure there is something there that might help!

If optimizing a code that runs in 4 seconds does not sound like a challenge – try the same code expect that instead of the sum equaling 1000 (a+b+c=1000), try for 10,000 or 100,000.

Got an answer? Do leave it here.

Puzzle 35 – Solution

A MB and KB are special numbers simple because they can be expressed as powers of 2.
1 KB = 2(10) and 1 MB = 2(20). Java provides a simple operator to multiply by two better know as the left shift operator.

So the code snippet that does the trick is,

int oneMB = oneKB<<10;

The other answer that I got is,
int oneMB = oneKB+1047552; which look like a neat enough way to solve the puzzle!

Leaders board to be updated with the next post.