SOURCE CODE
/*program to find out all smith numbers upto 10000 though it'd work for further range as well*/
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
//function're declared here
int sum_of_digits(int,int);
int sumofPrimeFactors(int);
bool checkMorethanonedigit(int);
bool checkPrimeNumber(int);
//main function defined here
int main()
{
int i,range,n,k;
printf("Enter the range = ");
scanf("%d",&range);
for(i=4;i<=range;i++)
{
if(checkPrimeNumber(i)) continue;
if(sum_of_digits(i,0) == sumofPrimeFactors(i))
{
printf("%d ",i);
}
}
return 0;
}
//function definition starts here
//check the prime factor has more than 1 digit or not
bool checkMorethanonedigit(int n)
{
if(n > 9)
{
return true;
}
else
{
return false;
}
}
//get the summation of all prime factors of the number
int sumofPrimeFactors(int n)
{
int i,sum=0;
while (n%2 == 0)
{
sum += 2;
n = n/2;
}
for (i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
if(checkMorethanonedigit(i))
{
sum += sum_of_digits(i,0);
}
else sum += i;
n = n/i;
}
}
if (n > 2)
{
if(checkMorethanonedigit(n))
{
sum += sum_of_digits(n,0);
}
else sum += n;
}
return sum;
}
//get summation of digits of a number if needed
int sum_of_digits(int i,int sum)
{
int j;
while (i > 0)
{
j = i % 10;
i = i / 10;
sum=sum+j;
}
return sum;
}
//check a number prime or not
bool checkPrimeNumber(int num)
{
int j, flag;
flag = 0;
for (j = 2; j <= num / 2; j++)
{
if ((num % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0) return true;
else return false;
}
Comments
Post a Comment