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);
}
}
}
c# programming-challenge time-limit-exceeded
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.
add a comment |
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);
}
}
}
c# programming-challenge time-limit-exceeded
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.
add a comment |
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);
}
}
}
c# programming-challenge time-limit-exceeded
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
c# programming-challenge time-limit-exceeded
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.
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.
add a comment |
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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