Problem 20. Summing digits
Solution Stats
Problem Comments

6 Comments
The test suite was broken, as many of you suspected. I fixed it and rescored the problems.
I like this problem:)
very interesting problem
Good
The test case 3 is totally wrong. It needs immediate correction. The code maker didn't notice that b=25 is a number not a single digit. If you're doing the sum of digits it always ends up with a single digit, it will never end up with number cause number itself is carrying multiple digits.
Just for the badge ; )
Solution Comments

2 Comments
function b = sumDigits(n)
pow=2^n;
b = 0;
while pow~=0
b=b+mod(pow,10);
pow=floor(pow/10);
end
end
The code is wrong. Cause the while doest have a stopping condition.

1 Comment
This is cheating, you didn't even solve the problem, you just passed parameters to the test suite

2 Comments

1 Comment
our program didn't get approved until we forced b to be an integer with the int8 function

1 Comment
good job

1 Comment
function b = sumDigits(n)
%SUMDIGITS Given n, find the sum of the digits that make up 2^n
in=num2str(2^n);
b=sum(str2num(in(:)));
end

1 Comment
This solution does not cover whole range of n.

1 Comment
This codes gives expected result verified in matlab, dont know why it is considered as incorrect answer

1 Comment
Clunky, but I'm happy ^_^

2 Comments
Interesting problem. I guess it´s rarely applied.
great! learning the existence of arrayfun

1 Comment
in qs the passing variable is 'n' and in solution it is 'a'

3 Comments
I can't for the life of me figure out how to get this to work for any number n. Any tips?
One way would be to use mod(x,10) to get the ones digit, add that to the sum, then use x=floor(x/10) to lop it off, and iterate until x becomes zero.
Brilliant, thank you! That is most likely the method that I would have ended up using if had I thought of one as well.

1 Comment
This solution takes advantage of the fact that the ASCII character set is nicely ordered.

2 Comments
Extremely clever! Why didn't I think of that!?
Great: to transpose a char row into a char column makes the work...

2 Comments
How does this work?
This is precomputation, lookup table. Quite common method, when input variable is from small set(and computation long and hard). Now I think it is cheating in this competition, sorry:]

2 Comments
Nice but this solution doesn't work with a sum>47
so switching between mod and sum should be fine right ?

2 Comments
please explain why the digits get separated when (num2str(2^n)'0') is performed
num2str converts the integer number into a string representation of that number: 1234 becomes '1234', which is the same as ['1','2','3','4']. Now you've got one array entry per digit. Subtract '0' from there, then the char array will become a double array with value 0 where the char was '0', etc.

1 Comment
I'm not getting why my solution fails for a =16
b=sum(str2num(num2str(2^a)')) is 25 for a =16 and yet it fails

1 Comment
Buggy

1 Comment
this only works (obviously) if the digits of n add up to less than 9, which is great for the current test case but doesn't hold up later.

1 Comment
this should't work because you're supposed to sum the digits of 2^n

1 Comment
Clearly a bug in the test. This solution didn't pass the last two tests.
Problem Recent Solvers6655
Suggested Problems

3082 Solvers

2930 Solvers

Number of 1s in the Binary Representation of a Number
391 Solvers

512 Solvers

Sum of first n positive integers
494 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!