Magnificent Digits!

Find the number of 3 digit numbers which are the sum of the cubes of their digits.


The answer is 4.

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.

5 solutions

Arulx Z
Jul 4, 2015
1
2
3
4
5
>>> num = 0
>>> for x in xrange(100, 1000):
        num += 1 if sum(int(n) ** 3 for n in str(x)) == x else 0
>>> print num
4

Moderator note:

Simple standard approach.

In this solution, does it check for x = 1000 x = 1000 ?

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 = 1000 x = 1000 .

Arulx Z - 5 years, 11 months ago

Log in to reply

The more you know :)

Calvin Lin Staff - 5 years, 11 months ago
Brock Brown
Jul 1, 2015

1 (long) line of Python:

Python 3.3:

1
print("Answer:", [n == sum((int(digit)**3 for digit in str(n))) for n in range(100, 1000)].count(True))

I don't think that removing natural line breaks counts as "1 line of Python".

Calvin Lin Staff - 5 years, 11 months ago

Log in to reply

I didn't remove natural line breaks, I used list comprehensions . :p

This would be removing natural line breaks:

1
for thing in range(10): for thing2 in range(10): do_thing()

And I did say it was looooong.

Brock Brown - 5 years, 11 months ago
Bhumika Sharma
Jun 30, 2015

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

Harshi Singh - 5 years, 11 months ago

Log in to reply

You should post that directly as a problem !

Calvin Lin Staff - 5 years, 11 months ago

145 1!+4!+5!=1+24+120=145

Ishan Tewari - 5 years, 11 months ago
Josiah Bruner
Aug 24, 2015

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;
}
Dalia Palace
Aug 16, 2015

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

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...