2
|
3666 Views
|
2 Replies
|
5 Total Likes
View groups...
Share
GROUPS:

# Expand a number into the sum of degrees of a prime number

Posted 11 years ago
 Sorry I have to bother Wolfram Community (maybe, better name'll be Wolfram Research Community - WRC, imho, cuter then WC :-)) on such an issue. I'm novice in Mathematica, in my project need to decompose the number to the sum of degrees of a given prime number.Example (need common construction): 103^3==2^20+2^15+2^13+2^11+2^10+2^6+2^5+2^4+2^2+2^1+2^0Or: 1092727==1048576+32768+8192+2048+1024+64+32+16+4+2+1Upper degree i need may be found by this functionf2[y1_, y2_] :=  Catch [Do [If [(y1/y2^i > 1) && (y1/y2^(i + 1) < 1), Throw[i]], {i, 1000}]]but here is a problem - construction Throw-Catch returns only one value - degree i (for solving problem need, for example, difference: y1-y2^i or smth like it). Imho, if Throw-Catch can return 2 or more values - this construction becomes more strong and interesting. As a variant - divide variables of function in 2 groups - one group of variables - send to the function for calculation, another group - return as a result of calculation. But this idea is too common, need to solve this problem anyway now.
2 Replies
Sort By:
Posted 11 years ago
 This could be done efficiently with IntegerDigits[n, p] or here is an alternative idea:  In[1]:= pow[n_, p_] := p^Reap[NestWhile[(# - p^Sow[Floor[Log[p, #]]]) &, n, Positive];][[2, 1]]  In[2]:= pow[103^3, 2]  Out[2]= {1048576, 32768, 8192, 2048, 1024, 64, 32, 16, 4, 2, 1}  In[3]:= Total[%]  Out[3]= 1092727In[4]:= pow[103^3, 5]Out[4]= {390625, 390625, 78125, 78125, 78125, 15625, 15625, 15625, 15625, 3125, 3125, 3125, 3125, 625, 625, 625, 125, 25, 25, 25, 25, 1, 1}In[5]:= Total[%]Out[5]= 1092727 A faster equivalent would be pow1[n_, p_] := Module[{d = IntegerDigits[n, p] /. m_Integer :> ConstantArray[1, m]},                          Flatten[d p^Range[Length[d] - 1, 0, -1]]]
Posted 11 years ago
 Thanx.IntegerDigits - solve the problem.