How many positive perfect squares less than 1 0 1 0 have their digits appearing in non-decreasing order when represented in decimal notation?
Eg : 4 , 1 6 , 1 6 9 , 1 2 2 5 are valid numbers. But, 6 4 , 1 0 0 , 1 9 6 are not valid.
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.
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 |
|
Trying to make one-liners is sometimes fun.
1 2 |
|
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
to
9
9
9
9
9
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.
\( // 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 << "/";
} }\)
Problem Loading...
Note Loading...
Set Loading...
Trivial Solution
Takes about 0.13 secs
49! Another square!