We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A good approach passed all test cases in single go no need to check weather factors are prime or not
int m=n;
int sum1=0;
int sum2=0;
while(m!=0)
{
sum1+=m%10;
m=m/10;
}
m=n;
while(m!=1)
{
for(int i=2;i<=m;i++)
{
if(m%i==0)
{
System.out.println(i);
m=m/i;
int k=i;
while(k!=0)
{
sum2+=(k%10);
k=k/10;
}
break;
}
}
}
System.out.println(sum1);
System.out.println(sum2);
if(sum1==sum2)
return 1;
else
return 0;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Identify Smith Numbers
You are viewing a single comment's thread. Return to all comments →
A good approach passed all test cases in single go no need to check weather factors are prime or not int m=n; int sum1=0; int sum2=0; while(m!=0) { sum1+=m%10; m=m/10; } m=n; while(m!=1) { for(int i=2;i<=m;i++) { if(m%i==0) { System.out.println(i); m=m/i; int k=i; while(k!=0) { sum2+=(k%10); k=k/10; } break; } } } System.out.println(sum1); System.out.println(sum2); if(sum1==sum2) return 1; else return 0;