Programming challenges


Posted in: puzzles | Save to del.icio.us | Twit This! 28 Oct 2009

Programming is about solving problems. To be precise, solving problems using a computing device. I love solving puzzles as do most programmers. Solving a challenging puzzle or a programming problem gives one a wonderful sense of satisfaction. Starting with this post I’ll be presenting a programming problem every week that will help readers take some time out from their routine work and have some additional fun working on challenging problems. Below is a small problem to kickstart the series:

Write a program which finds a four digit number AABB which is a perfect square. A and B represent different digits.

If you are posting solutions, then please make them in php, as this is what this blog is related around, but if would like to submit in any other language you are welcome, as requested by Evans in the comments.

I invite readers to send me problems you find interesting which can be incorporated in future posts. Just ensure that problems are not too easy or too hard. An easy problem doesn’t challenge you, while a hard one destroys the motivation to solve it. You can mail problems to me at metapix[at]gmail.com with the subject ‘problems’. Don’t forget to include your name.




Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



5 Responses

1

Giorgio Sironi

October 28th, 2009 at 10:18 am

<?php
for ($a = 1; $a <= 9; $a++) {
for ($b = 0; $b <= 9; $b++) {
// dynamic typing is awesome
$number = (int) “$a$a$b$b”;
$squareRoot = (int) sqrt($number);
if ($squareRoot * $squareRoot == $number) {
echo $number, “\n”;
}
}
}

2

Evans

October 28th, 2009 at 10:31 am

As someone who does not use PHP, I was wondering if you won’t mind me sending in my solution in Python or Java (I know this blog is about PHP). I have little to no experience in PHP at all so I tend to solve problems with any language I find easier.

sameer

October 28th, 2009 at 10:40 am

Any language solution is welcome!

4

joey

October 28th, 2009 at 1:03 pm

<?php
for ($i=32, $square=32*32; $i<100; $i++, $square=$i*$i) {
$matches = preg_match(’/(00|11|22|33|44|55|66|77|88|99){2}/’, $square);
if ($matches) echo “i={$i} ({$square})\n”;
}

5

Bert Slagter

October 28th, 2009 at 1:27 pm

The most obvious solution is the one Giorgio Sironi gave. What would be the shortest?

What about this one (PHP53 only):

echo implode(’, ‘, (array_filter(range(0011, 9988), function($x) { return preg_match(’/(\d)\1(\d)\2/’, $x) && sqrt($x) == (int) sqrt($x);})));

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 13 Users Online
    • 11 Guests, 2 Bots