simoncpu Random thoughts of simoncpu. You may view my old blog here.

Ten-digit Perfect Square Numbers

Comments: 0     Stars : 0

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.





You need to log in to DigitalFilipino.com Blogsite in order to comment on this entry.
Back to entries



Back to entries