1

First of all I want to know what it is even called; lets take an example a number

153

now let's cube all its digits:

(1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3) == 153

if the output is 153, what exactly is this thing called. I hope you understand what I'm trying to say now. I also want to implement the same thing in code without using any predefined methods in C#

How can I do that?

Please note that the input number can be dynamic and not hard coded like 153 as example

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • All that has been posted is a program description, but that doesn't tell us what _problem_ you're having. What have you tried, and what troubles did you encounter? Please [edit] your post to include a [valid question](/help/how-to-ask) that we can answer. Reminder: make sure you know what is [on-topic](/help/on-topic); asking us to write the program for you, opinions, and external links are off-topic. – gunr2171 Aug 25 '22 at 20:46
  • If you haven't started yet, the first thing I can think of is to split your input into digits. https://stackoverflow.com/questions/4808612/how-to-split-a-number-into-individual-digits-in-c can help (dupe target) – gunr2171 Aug 25 '22 at 20:47
  • yes i dont even know what that number is it even called thats why im asking it like this –  Aug 25 '22 at 20:48
  • 1
    If this scenario has a name then it is a recreational maths question not a programming one. – Martin Smith Aug 25 '22 at 20:50
  • 1
    https://math.stackexchange.com/a/643456/914 – Martin Smith Aug 25 '22 at 20:53
  • 4
    `1, 153, 370, 371, 407` oeis A046197 https://oeis.org/A046197 – Dmitry Bychenko Aug 25 '22 at 20:54
  • okay im storing each digits from the input into a List , now what should i do now ? –  Aug 25 '22 at 21:00
  • ??? What do you think you do., based on the question? You cube the digits, add them, and compare to the original number. – Tim Roberts Aug 25 '22 at 21:09
  • cube the digits and then add them and then compare it to the orignl –  Aug 25 '22 at 21:10
  • By the way, your title is wrong -- you want the "sum of the cubes of its digits", not the "sum of the digits of its cube", which would be another sequence entirely. – Tim Roberts Aug 25 '22 at 21:13
  • 2
    And for those who are curious, this is called "an Armstrong number". https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html – Tim Roberts Aug 25 '22 at 21:14

1 Answers1

3

First of all, please note that the sequence is finite: if we have 5 digits number, the maximum sum of digits cubed can be

99999 -> 5 * 9**3 == 3645 (max possible sum) < 10000 (min possible number)

Using calculus you can prove that sum of digits cubed grows slower than number itself (let me omit the proof); so if number has 5 or more digits it can't be the number we are looking for.

So far so good, we should check numbers from 1 to 10000 only.

Code: (please, fiddle yourself)

private static IEnumerable<int> A046197() {
  for (int number = 1; number < 10000; ++number) {
    int s = 0;
        
    for (int n = number; n > 0; n /= 10) {
      int d = n % 10;

      s += d * d * d;
    }

    if (number == s)
      yield return number;
  }
}

Let's have a look:

Console.Write(string.Join(", ", A046197()));

Output:

1, 153, 370, 371, 407

Note, that these numbers are A046197 sequence in oeis where you can find details.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • To be honest, I'm a beginner in C#, so if you can add another section after the main solution ,where you explain it with just static input and long but simple code, it would be very helpful to me. –  Aug 25 '22 at 21:08
  • i didnt get this thing IEnumerable A046197() –  Aug 25 '22 at 21:09
  • 1
    That just lets it return a sequence of numbers, one at a time using yield, instead of (for example) building up an array and returning the array. And please don't ask him to turn this into a complete program for you. You are required to do SOME of the work here. – Tim Roberts Aug 25 '22 at 21:11