Ten-digit Perfect Square Numbers
In one of the mailing lists that I've subscribed to, a question was
posed yesterday for finding a ten-digit number consisting of distinct
digits that is a perfect square. Wikipedia defines a perfect square as "a positive integer which is the square of some other integer, i.e. can be written in the form n^2 for some integer n."
I wanted to code my solution using C/C++, but not all of us have
compilers. Thus, I coded my solution using JavaScript. To
see the results, just copy and paste the code below and save it to a
file with .html as its filename extension. Then, run it in your
browser.
Some of the Answers
1026753849 (lowest value)
9814072356 (highest value)
There are 87 possible answers.
Solution
<script type="text/javascript">
function isUnique(n) {
var str = new String(n);
var tmp = new Array(10);
for(var i = 0; i < 10; i++) {
tmp[i] = null;
}
for(var i = 0; i < 10; i++) {
if(tmp[str.charAt(i)] == null) {
tmp[str.charAt(i)] = str.charAt(i);
} else {
return false;
}
}
return true;
}
function main() {
var lbound = 31992;
var ubound = 99380;
var count = 0;
for(var i = lbound; i <= ubound; i++) {
if(isUnique(i * i)) {
document.write((i * i) + "<br />");
count++;
}
}
document.write("There are " + count + " possible answers.");
}
main();
</script>
Discussion
The lowest possible solution is 1023456789, and the highest
possible solution is 9876543210. Although 0123456789 is the
lowest possible number that can be constructed using 10 distinct
digits, I leave you to find out why we can't use it as a lower bound
(duh). :)
The square root of 1023456789 is aprox. 31991.51. We round it
off to 31992, and start finding the solution here. The square
root of 9876543210 is aprox. 99380.80. We round it off to 99381,
and stop finding the solution here. The script then calculates
the square of every number from 31992 to 99381, and displays it if the
result consists of distinct digits.
Coldfusion's isNumeric(): A different approach
My friend, Aldwin, has a different approach in solving the isNumeric()
problem mentioned in my previous post. Here is his solution for
the module that he's working on (I've slightly modified it to remove
sensitive information):
<cfset acceptonly = "1,2,3,4,5,6,7,8,9,0,.,-">
<cfif listfind(acceptonly, str, ",") gt 0>
This is not a valid number.
<cfelse>
This is a valid number.
</cfif>
Please note that this is only applicable for certain (preferrably pre-validated) inputs. This will not flag an error for inputs such as "8.-.16-" or "...32.256"
Work-around for Coldfusion's isNumeric() function
Problem: You need to
determine whether a given input is a number or not.
Unfortunately, Coldfusion's isNumeric() function only checks whether a
number can be treated as a number internally. Therefore, the
following strings (without the quotation marks) would cause the
function to return a true value:
"16d"
"128f"
"32e256"
Although this is fine for many applications, this is often not the desired behaviour such as when you need to validate them before passing it to a MySQL query.
Solution: A solution I've found so far is to use Regular Expressions. We use REFind to search for the following regex pattern:
Example:
This is a vaild number.
<cfelse>
This is not a valid number.
</cfif>
Please note that this is also applicable to other programming languages that support Regular Expressions (i.e.: either built-in or through a library), although this may no longer be necessary for more elegant languages such as PHP (sorry, I'm biased).
Generating RTF
I'm making a web-based payroll system right now. Since we need to
generate print-outs of the data, I opted to format it using Microsoft's
proprietary (?!) Rich Text Format (RTF). I didn't choose PDF
because it's a bit complicated, and because we are on a tight deadline
(was it a correct decision?). I have no formal training on this
whatsoever, so I had to rely on samples to deduce some of the rules
that make up this document language.
The text that MSWord generates is so horrible. I had to examine lengthy sequences such as this:
This is fun! :)
DeCSS - Algorithm for unscrambling DVD content
Lesson No. 1: Filipinos love sachet marketing.
Lesson No. 2: Filipinos, in general, aren't loyal to a particular product or service provider.
I have just changed my webhost. The first file that I've uploaded
is a (supposedly) illegal algorithm for decrypting DVDs. It's
released under the GNU General Public License, so I've uploaded it
here. Most mirrors that host this kind of information has already
been taken down by US government authorities. You may access the
DeCSS source code at
http://freedom.simoncpu.com/