Given n, find the sum of the digits that make up 2^n.
Example:
Input n = 7 Output b = 11
since 2^7 = 128, and 1 + 2 + 8 = 11..
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
'128' - '0' >>>> 1 2 8
Helped
% Split the elements up and convert from string to number.
our program didn't get approved until we forced b to be an integer with the int8 function
good job
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
This solution does not cover whole range of n.
This codes gives expected result verified in matlab, dont know why it is considered as incorrect answer
Clunky, but I'm happy ^_^
Interesting problem. I guess it´s rarely applied.
in qs the passing variable is 'n' and in solution it is 'a'
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.
This solution takes advantage of the fact that the ASCII character set is nicely ordered.
Extremely clever! Why didn't I think of that!?
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:]
Nice but this solution doesn't work with a sum>47
so switching between mod and sum should be fine right ?
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.
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
Buggy
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.
this should't work because you're supposed to sum the digits of 2^n
Clearly a bug in the test. This solution didn't pass the last two tests.
382 Solvers
199 Solvers
Celsius to Fahrenheit converter
262 Solvers
2146 Solvers
185 Solvers