Find the number of 3 digit numbers which are the sum of the cubes of their digits.
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.
In this solution, does it check for x = 1 0 0 0 ?
In response to Challenge Master
range
and
xrange
include the first number while excludes the last. So in my code, 100 is inclusive while 1000 is exclusive and so it doesn't check when
x
=
1
0
0
0
.
1 (long) line of Python:
Python 3.3:
1 |
|
I don't think that removing natural line breaks counts as "1 line of Python".
Log in to reply
I didn't remove natural line breaks, I used list comprehensions . :p
This would be removing natural line breaks:
1 |
|
And I did say it was looooong.
The 4 numbers are 153 , 370 , 371 , 407 example- 153 -when the cubes of 1,5,3 are added that is 1+125+27=153.
thats good bhumika...anyways one simple question to you :which is the only three digit number who is the sum of its factorial???try its simple
Log in to reply
You should post that directly as a problem !
145 1!+4!+5!=1+24+120=145
C++ "normal" solution. Note that the code is perhaps a little more verbose than necessary, simply because I find it simpler to follow:
#include <iostream>
#include <cmath>
int main()
{
int counter = 0;
for (int i = 100; i < 1000; i++) {
int hundredDigit = floor(i / 100);
int tensDigit = floor((i % 100) / 10);
int onesDigit = i % 100 % 10;
if (i == (hundredDigit * hundredDigit * hundredDigit) + (tensDigit * tensDigit * tensDigit) + (onesDigit * onesDigit * onesDigit)) {
counter++;
}
}
std::cout << "The total is: " << counter;
}
Using python
>>> numbers = []
>>> for i in range(100, 1000):
numbers.append(i)
>>> len(numbers)
900
>>> numbers = []
>>> for i in range(100, 1000):
numbers.append(str(i))
>>> r = []
>>> for num in numbers:
if int(num) == int(num[0])**3+int(num[1])**3+int(num[2])**3:
r.append(num)
>>> len(r)
4
Problem Loading...
Note Loading...
Set Loading...