Assume that for any two positive integers, a and b, the function, a mod b works
ID: 3628701 • Letter: A
Question
Assume that for any two positive integers, a and b, the function, a mod b works only if b = 10.a) Provide an algorithm to determine the sum of the digits of a positive integer, a (Note: The
sum of the digits must finally be an integer between 0 and 9. So, if you input a = 6372041485, your
algorithm should provide an output, 4 and not 40).
b) Does the ability to perform a mod 9 simplify your solution? If so, how?
c) ) Use the algorithms developed in part (3a) or (3b) to determine whether 6|y (i.e., 6 divides
y) for any positive integer, y.
Explanation / Answer
a)
Algorithm to compute sum of digits of a positive integer:
read integer n;
declare integer sum=0
while (n>0)
{
sum=sum+n%10;
n=n/10;
sum=sum%10+sum/10;
}
or
declare integer sum=0,temp
while (n>0)
{
sum=sum+n%10;
n=n/10;
if (sum>9)
{
temp = sum%10;
sum = sum /10;
sum = sum+temp;
}
}
b)
while (n>0)
{
sum=sum+n%9;
n=n/9;
sum=sum%10+sum/10;
}
The ability of mod 9 does not simplify the solution. It gives inaccurate solution.
c)
Algorithm to compute sum of digits of a positive integer:
read integer y;
declare integer sum=0
while (y>0)
{
sum=sum+y%6;
y=y/6;
sum=sum%10+sum/10;
}
if (sum=0)
output 6 divides y
else
output 6 does not divides y
Hope this would help you.