All in a Single File

How many positive perfect squares less than 1 0 10 10^{10} have their digits appearing in non-decreasing order when represented in decimal notation?

Eg : 4 , 16 , 169 , 1225 4, 16, 169, 1225 are valid numbers. But, 64 , 100 , 196 64, 100, 196 are not valid.


The answer is 49.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Arulx Z
Oct 27, 2015

Trivial Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import math

count = 0
n = 0

for x in xrange(100000):
    n += x + x + 1
    z = n
    for y in xrange(int(math.log10(n)) + 1):
        last = z / 10
        if z % 10 < last % 10:
            count -= 1
            break
        z = last
    count += 1

print count

Takes about 0.13 secs

49! Another square!

Moderator note:

Simple standard approach. Nice way to obtain the sequence of digits.

Here is my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
d=0
for x in range(1,(10**5+1)):
    n=0
    s=str(x**2)
    for i in range(0,(len(s)-1)):
        if s[i]<=s[i+1]:
            n=n+1
    if n==(len(s)-1):
        d=d+1

print(d)

Ivan Koswara
Nov 10, 2015

Trying to make one-liners is sometimes fun.

1
2
sum((int("".join(list(sorted(str(n**2))))) == n**2) for n in range(1, 100000))
# prints 49

Explanation:

n**2 is our perfect square. str(n**2) turns it into a string , which is also an iterable (with elements being individual characters), so sorted(str(n**2)) sorts the characters in the element. list(sorted(...)) forces the result to a list (instead of a sorted type). "".join(list(...)) glues the characters together, so that the result is a string with the characters sorted, and int("".join(...)) turns it back into an int .

In short, int("".join(...)) sorts the digits of our perfect square. We compare it with the original perfect square, checking if they are equal; if they are, then the digits of the perfect square are listed in non-decreasing order, which is what we want to find.

Now, int(...) for n in range(1, 100000) iterates it for all perfect squares from 1 2 1^2 to 9999 9 2 99999^2 . Every number that we seek will give a True , which counts as a 1 , and every number that we don't seek will give a False , which counts as a 0 . Thus sum(...) gives the count of the True values, or in other words the number of perfect squares with sorted digits. This is what we want.

Jafar Badour
Oct 26, 2015

\( // ConsoleApplication111.cpp : Defines the entry point for the console application. //

#include "stdafx.h"

#include<iostream>

using namespace std;

int jeff(int y){

if \(y == 0\)return 1;

if \(y % 10 >= \(y/10\) % 10\) return jeff\(y / 10\);

if \(y % 10 < \(y/10\) % 10\) return 0;

}

int main(){

int b = 0;

for \(int i = 0; i < 100000; i\+\+\)\{

    if \(jeff\(i×i\) == 1\)\{

        cout << i << "\\n";

        b\+\+;

    \}



\}

cout << b << "/";

} }\)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...