Define sod(x) as the sum of the digits of x.
For example, sod(1245984)= 1+2+4+5+9+8+4= 33.
Devise an algorithm to determine the value of:
sod(20232023).
yp=char(sym(2023)^2023);
disp(length(yp))
disp(sod(yp))
finds that the 6689 digits of the power add up to 30,112:
>> yearYearn5
6689
30112
>>
where the sod function is
function sd = sod(n0)
sd=0;
n1=char(string(n0));
for i=1:length(n1)
sd=sd+str2double(n1(i));
end
end
which unnecessarily converts the character string to a unitary string and back again as the sod function is written so that it would work even on a numeric, so in fact the char function in the outer function was not really necessary except to find the length of the number, which was not really asked for in the puzzle.
So
yp=sym(2023)^2023;
disp(sod(yp))
works just as well as the above in finding only
>> yearYearn5
30112
>>
|
Posted by Charlie
on 2023-06-28 08:42:31 |