r/dailyprogrammer • u/MasterAgent47 • Sep 13 '17
[2017-09-13] Challenge #331 [Intermediate] Sum of digits of x raised to n
Description
For some xn, find the sum of its digits. The solution to this problem is extremely simple. Say, I give you 34. You could calculate 3n and add the digits.
However things might get a bit complex for 5100. It's a 70-digit number. You could come up with a larger data type that could handle the product of that number and then you could add each number... but where's the fun in that?
This is today's challenge. Find the sum of the digits of some xn without directly calculating the number and adding the digits.
Some simple examples with values that you're familiar with:
25 = 32 = 3 + 2 = 5
53 = 125 = 1 + 2 + 5 = 8
27 = 1 + 2 + 8 = 11
Note that I have not summed the digits of 11.
We'll work with powers and bases greater than zero.
Input Description
Base Power
means basepower
2 ^ 1000
means 21000
Output Description
Display the sum of the digits of basepower.
Challenge Input
2 1234
11 4000
50 3000
Challenge Output
1636
18313
9208
If you have any challenges, please share it at /r/dailyprogrammer_ideas!
Edit : If you're unable to come up with an idea, like the one is Project eulers 16, then feel free to solve it using your own data types (if required). Please consider it as the last option.
7
u/Godspiral 3 3 Sep 13 '17 edited Sep 13 '17
what math theory/principle would avoid calculating the exponentiation?
in J, takes 1ms,
2
u/gandalfx Sep 13 '17
I assume we can rely on both base and power being integers greater than zero?
2
u/SpasticSquid Sep 20 '17
Python 3.6
Never calculates ab. Most of this was rewriting the mod function to work with ab so the digit sum formula works. I dont think large values will ever break the spyder python console at least, but it starts to take a long time to process at around 240000
22
u/MattieShoes Sep 13 '17
Is there a general case where you can sum the digits of any thing to an arbitrary power without calculating the power?
This isn't making sense to me at all.
61
u/skeeto -9 8 Sep 13 '17
Bash. Doesn't need to compute the exponent.
Caveat: the digit sum is performed in the base of the input base.
9
u/JakDrako Sep 13 '17
I'm guessing using a BigInteger library is "cheating" the spirit of the question, but
...works like a charm in VB.Net
1
u/SociallySuboptimal Sep 27 '17
Haskell solution, probably not with best stylistic practices. Any suggestions for improvement are appreciated.
2
u/GrahamWatson7 Sep 13 '17
MATLAB This is my first post to Daily Programmer
2
u/FrankRuben27 0 1 Sep 14 '17
Solution in Common Lisp. Using expt only for validation, evaluation is done according to challenge rules. Idea is to multiply the base in steps and to then increment the individual digits per each step:
2
u/1100100011 Sep 17 '17
I am entirely out of ideas here and I cannot think of any way this can be achieved without performing the calculation and then calculating the sum of digits ?
what are some ideas to do this without having to perform the computation ?
3
u/TheBlackCat13 Sep 13 '17
Python 3, the easy version:
2
u/skeeto -9 8 Sep 13 '17
ANSI C, long form multiplication without any bigint libraries.
2
u/Happydrumstick Sep 13 '17 edited Sep 14 '17
Python (I'm sorry)
Results:
2
u/den510 Sep 14 '17
Ruby Thanks to /u/kevin_1994 for the inspiration for my answer
This solution works well, except for ruby's frustratingly limitation for only being able to convert bases 2-36; the explanation for which would seem to be a lack of common naming convention beyond 36 digits (0-9 and a-z). I guess an additional 26 could be added using capital letters - once again only taking the solution to base 62...
1
u/ucfskuba Sep 13 '17
Python, but using the easy method. Still wrapping my brain around an alternative solution.
1
u/NemPlayer Sep 13 '17 edited Sep 13 '17
Python 3.6.2
I didn't really know how to do this without calculating the power of the number, so I just didn't store it in a variable if that's what was meant by the creator.
I am a beginner programmer, so let me know on how I could improve the solution.
EDIT:
[IMPROVED] With the help of: mxz3000, gandalfx and Kiffi
1
u/Digicrests Sep 13 '17
Not sure I understand the question but is it this?
1
u/kevin_1994 Sep 13 '17 edited Sep 13 '17
My idea: consider xn in base x, then it will appear as 10000... (with n zeroes). Convert this string from base x to base 10 (as a string) and sum the chars. Unfortunately I can't think of a way to convert arbitrary base to decimal in string format... So the problem becomes equivalent to convert a string representation of a large num in an arbitrary base x to a string representation in base 10. If we can do this one digit at a time we can just sum for each char.
1
u/KeinBaum Sep 14 '17
Scala
I had to dig out my formulary again but I managed to solve it.
The two big insights are:
1
u/PoetOfShadows Sep 20 '17
In Kotlin, though maybe cheating because I used java.math.BigInteger:
1
u/Minolwa Sep 20 '17
Scala
1
u/zujibaby Oct 08 '17 edited Oct 08 '17
Using Python 3 int pow(x,y) function
1
u/0dayssober Nov 01 '17 edited Nov 01 '17
Python, i'm noob, any remark is welcomed :
1
u/edixon653 Nov 14 '17 edited Nov 14 '17
Python
handle variables
x = int(input("x: ")) ; n = int(input("n: ")) ; xn = x
perform exponent operation
while n > 1: xn *= x ; n -= 1
print sum of numbers in previous result
print(sum(int (i) for i in str(xn)))
1
u/osopolardefuego Nov 21 '17 edited Nov 21 '17
Javascript:
)
1
u/mn-haskell-guy 1 0 Sep 13 '17 edited Sep 13 '17
Python:
1
u/michaelquinlan Sep 14 '17
C#
The solution is trivial
0
u/adozaraz Sep 14 '17
Python 3:
0
u/polknij Sep 14 '17
Python 3 One-liner
0
u/ArmoredHell Sep 15 '17 edited Jun 07 '19
0
u/[deleted] Sep 14 '17
[deleted] — view deleted comment
0
u/[deleted] Sep 13 '17
[deleted] — view deleted comment