Given n, find the sum of the digits that make up 2^n.
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
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 ; )
function b = sumDigits(n)
b = 0;
The code is wrong. Cause the while doest have a stopping condition.
'128' - '0' >>>> 1 2 8
% Split the elements up and convert from string to number.
'*'-'0' is very smart, thank you!
our program didn't get approved until we forced b to be an integer with the int8 function
function b = sumDigits(n)
%SUMDIGITS Given n, find the sum of the digits that make up 2^n
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.
great! learning the existence of arrayfun
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!?
Great: to transpose a char row into a char column makes the work...
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
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.
Make a Palindrome Number
Find the two most distant points
It dseon't mettar waht oedrr the lrettes in a wrod are.
Matrix indexing with two vectors of indices
Create an index-powered vector
Determine whether a vector is monotonically increasing
Get the area codes from a list of phone numbers
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office