I’ll give you a code structure the get you going on this problem. You may want to refer to your file from Tuesday, or to the manual.
powerdigitsum[x_,k_]:=(*Putyourfunctionhere.Itshouldreproducetheresultsbelow.*)
In[]:=
powerdigitsum[9,2]
Out[]=
9
In[]:=
powerdigitsum[123,12]
Out[]=
117
In[]:=
powerdigitsum[12345,108]
Out[]=
1980
Now I’ll define a function that helps you determine what numbers you need to consider. This function returns a number d, and no number larger than need to be considered.
d
10
In[]:=
findmax[k_]:=(d=1;While[10^(d-1)<9(kd+1),d++];Return[d-1])
In[]:=
findmax[2]
Out[]=
2
In[]:=
findmax[5]
Out[]=
3
In[]:=
findmax[108]
Out[]=
4
This code keeps the solutions in a list called solutions. You need to put in code that will check to see if a number is equal to its digitsum. If it is, put that number into the list of solutions. The empty brackets start us out with an empty list.
solutions={};k=2;power=findmax[k];Do[(*Putcodeinhere*),{j,1,10^power}];Print[solutions]
Here are the solutions for k=108.
{1,1322,1403,1405,1441}