Project Euler #119 : Digit power sum











up vote
3
down vote

favorite












From problem #119 of the Project Euler :




The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.



We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.



You are given that a2 = 512 and a10 = 614656.



Find a30.




The number 512 fits because :




5+1+2 = 8
and 83 = 512.




The number 17576 fits because :




1+7+5+7+6 = 26
and 263 = 17576.




There's my solution but it's extremely inefficient. I need the variable c to reach 25 but I haven't seen past 16.



    public static int c = 0;
public static void TryAll(long x, long y)
{
for (int i = 2; i < 10; i++)
{
double powered = Math.Pow(y, i);
if (x % y == 0 && powered == x && x % 10 != 0)
{
c++;
Console.WriteLine("----- {0}", c);
Console.WriteLine(powered);
Console.WriteLine(y);
Console.WriteLine(i);
}
}
}
public static void Main(string args)
{
int baseNum = 0;
for (c = c; c < 26; baseNum++)
{

if (baseNum > 9)
{
int sum = 0;
int baseNumD = baseNum;
while (baseNumD != 0)
{
sum += baseNumD % 10;
baseNumD /= 10;
}
TryAll(baseNum, sum);
}
}
}









share|improve this question









New contributor




AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    3
    down vote

    favorite












    From problem #119 of the Project Euler :




    The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.



    We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.



    You are given that a2 = 512 and a10 = 614656.



    Find a30.




    The number 512 fits because :




    5+1+2 = 8
    and 83 = 512.




    The number 17576 fits because :




    1+7+5+7+6 = 26
    and 263 = 17576.




    There's my solution but it's extremely inefficient. I need the variable c to reach 25 but I haven't seen past 16.



        public static int c = 0;
    public static void TryAll(long x, long y)
    {
    for (int i = 2; i < 10; i++)
    {
    double powered = Math.Pow(y, i);
    if (x % y == 0 && powered == x && x % 10 != 0)
    {
    c++;
    Console.WriteLine("----- {0}", c);
    Console.WriteLine(powered);
    Console.WriteLine(y);
    Console.WriteLine(i);
    }
    }
    }
    public static void Main(string args)
    {
    int baseNum = 0;
    for (c = c; c < 26; baseNum++)
    {

    if (baseNum > 9)
    {
    int sum = 0;
    int baseNumD = baseNum;
    while (baseNumD != 0)
    {
    sum += baseNumD % 10;
    baseNumD /= 10;
    }
    TryAll(baseNum, sum);
    }
    }
    }









    share|improve this question









    New contributor




    AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      From problem #119 of the Project Euler :




      The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.



      We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.



      You are given that a2 = 512 and a10 = 614656.



      Find a30.




      The number 512 fits because :




      5+1+2 = 8
      and 83 = 512.




      The number 17576 fits because :




      1+7+5+7+6 = 26
      and 263 = 17576.




      There's my solution but it's extremely inefficient. I need the variable c to reach 25 but I haven't seen past 16.



          public static int c = 0;
      public static void TryAll(long x, long y)
      {
      for (int i = 2; i < 10; i++)
      {
      double powered = Math.Pow(y, i);
      if (x % y == 0 && powered == x && x % 10 != 0)
      {
      c++;
      Console.WriteLine("----- {0}", c);
      Console.WriteLine(powered);
      Console.WriteLine(y);
      Console.WriteLine(i);
      }
      }
      }
      public static void Main(string args)
      {
      int baseNum = 0;
      for (c = c; c < 26; baseNum++)
      {

      if (baseNum > 9)
      {
      int sum = 0;
      int baseNumD = baseNum;
      while (baseNumD != 0)
      {
      sum += baseNumD % 10;
      baseNumD /= 10;
      }
      TryAll(baseNum, sum);
      }
      }
      }









      share|improve this question









      New contributor




      AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      From problem #119 of the Project Euler :




      The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.



      We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.



      You are given that a2 = 512 and a10 = 614656.



      Find a30.




      The number 512 fits because :




      5+1+2 = 8
      and 83 = 512.




      The number 17576 fits because :




      1+7+5+7+6 = 26
      and 263 = 17576.




      There's my solution but it's extremely inefficient. I need the variable c to reach 25 but I haven't seen past 16.



          public static int c = 0;
      public static void TryAll(long x, long y)
      {
      for (int i = 2; i < 10; i++)
      {
      double powered = Math.Pow(y, i);
      if (x % y == 0 && powered == x && x % 10 != 0)
      {
      c++;
      Console.WriteLine("----- {0}", c);
      Console.WriteLine(powered);
      Console.WriteLine(y);
      Console.WriteLine(i);
      }
      }
      }
      public static void Main(string args)
      {
      int baseNum = 0;
      for (c = c; c < 26; baseNum++)
      {

      if (baseNum > 9)
      {
      int sum = 0;
      int baseNumD = baseNum;
      while (baseNumD != 0)
      {
      sum += baseNumD % 10;
      baseNumD /= 10;
      }
      TryAll(baseNum, sum);
      }
      }
      }






      c# programming-challenge time-limit-exceeded






      share|improve this question









      New contributor




      AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 52 mins ago









      Calak

      2,082318




      2,082318






      New contributor




      AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      AcedPI

      191




      191




      New contributor




      AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      AcedPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          AcedPI is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209311%2fproject-euler-119-digit-power-sum%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          AcedPI is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          AcedPI is a new contributor. Be nice, and check out our Code of Conduct.













          AcedPI is a new contributor. Be nice, and check out our Code of Conduct.












          AcedPI is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209311%2fproject-euler-119-digit-power-sum%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List directoties down one level, excluding some named directories and files

          list processes belonging to a network namespace

          list systemd RuntimeDirectory mounts