A Smith number is a nonprime integer such that the sum of its digits is the sum
ID: 3880823 • Letter: A
Question
A Smith number is a nonprime integer such that the sum of its digits is the sum of the digits in its prime
factorization. For example,
58 is a Smith number because 58 = 2 * 29 and 5 + 8 = 2 + 2 + 9
See https://en.wikipedia.org/wiki/Smith_number
See also https://oeis.org/A006753
Write a C program that inputs a sequence of integers terminated by 0. For each positive nonprime N
the program reports whether N is a Smith number.
Example program run (updated 1/26 to show treatment of negative and prime inputs)
N: 8
N: 17
N: 4
Yes
N: 10
No
N: 727
No
N: 728
Yes
N: 729
Yes
N: 777
No
N: 4937775
Yes
N: 121
Yes
N: 100
No
N: 1000
No
N: 0
Explanation / Answer
#include <stdio.h>
int check_Prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
int digitsSum(int n)
{
int s=0,d;
while (n!=0)
{
s=s+n%10;
n=(int)n/10;
}
return s;
}
int factorsSum(int n)
{
int i;
int s;
s=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
if(check_Prime(i))
{
s=s+digitsSum(i);
}
}
}
return s;
}
int main()
{
int n;
n=1;
printf("N: ");
scanf("%d",&n);
while(n!=0)
{
if((n>0)&&(check_Prime(n)==0))
{
if (factorsSum(n)==digitsSum(n))
{
printf("Yes ");
}
else
{
printf("No ");
}
}
printf("N: ");
scanf("%d",&n);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.