Sum of digits ( n ) > Sum of digits ( n 3 )
Find the smallest natural number n such that the above statement is true.
Inspired by this
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.
You just wait Brock , I have started leaning Python , in just a few months time I'll be a threat for you !!
Just kidding , there's no way I could be a threat for you :)
Nice solution .
I had Bookmarked this site a long time ago , and now it's coming in handy .
For those who don't read the comments below and just want to look at the solution , let me present a solution that my friend Kishlaya has come up with .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
returns 587
I'll try to come up with a Java solution tomorrow .
Log in to reply
Here's just a short brute-forcey method (in JAVA)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
returns 587
Log in to reply
I had a similar code.
Nice solution :) +1
How did you give the numbering to the lines of your code ?
Log in to reply
@A Former Brilliant Member – That's a new feature on B'ant.
Log in to reply
@Pranjal Jain – Ok . But how to use it ?
Log in to reply
@A Former Brilliant Member – Use three tick marks alongwith the programming language name besides it.
image
My python 2.7 solution:
for i in range(5000):
a=str(i)
s1=0
for j in a:
s1+=int(j)
s2=0
b=str(i**3)
for k in b:
s2+=int(k)
if s1>s2:
print a
Mine is a bit convoluted but here is what I did:
My C# code (Uses System.Numeric.dll library for BigInteger)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Output = 587
Using javascript. With google chrome, you can easily run this code.
for (x = 1; x <= 1000; x++) { var f1 = String(x), f2 = String(x x x); var n1 = 0, n2 = 0; for (var i in f1) n1 += Number(f1[i]); for (var j in f2) n2 += Number(f2[j]); if (n1 > n2) {alert(x); break;} }
C language :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Java solution -
public static void main(String[] args){
for(int i = 1;;i++)
if(sum(i) > sum(i * i * i))
System.out.println(i);
}
private static int sum(int n){
int sum = 0;
while (n != 0){
sum += n % 10;
n /= 10;
}
return sum;
}
Execution time - 0.000456635 seconds
Problem Loading...
Note Loading...
Set Loading...
Python 2.7: