r/CS_Questions • u/kpthunder The Riddler • Dec 27 '11
Sum of Digits
Consider the following example:
210 = 1024 -> 1 + 0 + 2 + 4 = 7
Write a program which calculates the sum of the individual digits of 250 .
3
u/ubermole Dec 27 '11 edited Dec 27 '11
Hah, not using any bignums etc - which would have horrible performance, there is a really classic solution to this. [double dabble!]/spoiler
2
u/ubermole Dec 28 '11
This question has a lot of follow up:
Is the input always a power of two? If yes, I suspect there is a shortcut.
What is the big O(n), where n is the number of bits in the input number. I think it's n2 for arbitrary n bit input numbers.
I would be really curious about timings in various languages for really big n (n around 100000 up) using the builtin bignums.
I am kind of curious if many of the high level language one line solutions actually do bignum as written. i can post some test results for largeish n > 1000 to check.
1
Jan 01 '12
Is the input always a power of two? If yes, I suspect there is a shortcut.
Possibly, but I don't see it! If you have any ideas, I'd love to hear them.
I think it's n2 for arbitrary n bit input numbers.
I'm pretty sure it's O(N). Conversion from binary to decimal takes linear time. Adding digits together takes linear time too (the obvious approach requires O(1) amortized time per digit). So the resulting algorithm requires only linear time.
If it were really O(N2) the problem would not be solvable for N around 1,000,000.
I am kind of curious if many of the high level language one line solutions actually do bignum as written.
What do you mean by this? Do you doubt that it runs correctly or that it runs in time? I'm pretty sure most modern
I am kind of curious if many of the high level language one line solutions actually do bignum as written.
I can vouch that at least the Python code works correctly. E.g. sum(map(int,str(2**1234567))) yields 1674299 after about five seconds. Why wouldn't this work?
1
u/ubermole Jan 02 '12
How do you do conversion from binary to decimal in linear time?
for n input bits: for ( n ) { shift and add n+n+n*4/3 bits }the shift and add is linear over n, and you do this for n bits. n2. and yes, a 1000000 bits binary to decimal can take a very long time.
2
Jan 02 '12
Hmm, it appears I was wrong about that! But I'm sure it's better than O(N2 ) as witnessed by the fact that Python or GMP can very quickly convert large numbers (with millions of bits) to decimals.
GMP seems to use a divide-and-conquer approach, though I'm not exactly sure what the complexity of their algorithm is.
1
u/ubermole Jan 05 '12
Looked up the GMP algo... they switch to it after large n only, and then it is n log n i guess. Interesting algorithm actually.
1
Dec 27 '11
long long n = 1 << 50;
int sum = 0;
while (n) {
sum += n % 10;
n /= 10;
}
printf("%d", sum);1
Dec 27 '11
I can one up you. Though this may have problems with large m.
uint64_t sumDigits (uint64_t m){ uint64_t n = 1 << m; uint64_t sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; } uint64_t printSumDigits50() { static uint64_t n = sumDigits(50); printf("%.1u", n); }Edit: you could improve this further with a static hash map instead.
1
1
u/are595 Dec 28 '11
So I may be going crazy, but here is what I have so far pattern wise. I see the beginnings of a pattern. Hopefully after some time I will be able to figure it out more clearly.
2^0 = 1 = 1 = 1
2^1 = 2 = 2 = 1x2
2^2 = 4 = 4 = 2x2
2^3 = 8 = 8 = 4x2
2^4 = 16 = 7 = 3x2 + 1
2^5 = 32 = 5 = 1x2 + (1x2 + 1)
2^6 = 64 = 10 = 3x2 + 2x2
2^7 = 128= 11 = (1x2 + 1) + 4x2
2^8 = 256= 13 = 1x2 + 2x2 + (3x2 + 1)
2^9 = 512= 8 = 2x2 + (0x2 + 1) + (1x2 + 1)
2^10= 1024= 7 = (0x2 + 1) + 1x2 + 2x2
2^11= 2048= 14 = 1x2 + 0x2 + 2x2 + 4x2It seems that the last multiplication is related to the one 4 or 8 (it seems it will be any multiple of 4) but by the looks of it the whole pattern will wind up being vastly more complicated that just using the bignum approach. Also, I don't have the time to figure it out :/. I do hope someone can though, I would like to see the pattern if there is one.
1
u/orangeyness Dec 27 '11 edited Dec 27 '11
The problem is pretty trivial when you have access to bignums, no need for clever tricks. In ruby it can be done in a couple lines of code,
sum = 0
(1 << 50).to_s.each_char {|c| sum += c.to_i }
puts sum(If you have a newer version of ruby you can do it in just one line of code something like below)
puts (1 << 50).to_s.each_char.reduce(0) {|sum, c| sum += c.to_i}3
u/nightless_night Dec 27 '11
I wonder how small can you make it. My best so far, in Python, is 24 characters:
sum(map(int,str(2**50)))1
u/orrEmbar Feb 22 '12
oooh. I'm learning python and gonna checkout the map function there. So here's a general-purpose newbie program for this.
base = int(raw_input("Enter the base-number\n>>> ")) exp = int(raw_input("Enter the exponent-number\n>>> ")) theNum = str(base**exp) theSum = 0 for i in theNum: theSum += int(theNum) print theSum
1
u/amitburst Feb 11 '12
Hey, I'm fairly new to programming. What would be the big O(N) for this recursive solution? I'm guessing it's slow, but just curious.