/* PHP & MySQL Journal */
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.
|
|
This site is a digital habitat of Sameer, a freelance web developer working from Pune.More
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);})));