Love ko to
Thank God for McDonalds. With my present job as a programmer, I find myself waking in odd hours because my body clock is already used to it. Because I don't know how to cook my own meal, I have to rely on restaurants and cafeterias to supply my body with much-needed energy. The restaurants are already closed at this hour, so I had to sacrifice my hard-earned money just to eat. McDo is kindda expensive. I spend more than a hundred bucks, and I'm still not satisfied. The McDo crowd is different in the evening. I find myself surrounded by Med and Law students who are burning the midnight candle. Anyway, I'll just eat to my heart's content tommorrow.
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.
Posted by: simoncpu in: With Logic and Chaos
Modified on July 15, 2005 at 9:17 PM