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.
public class Solution {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int prime = getPrimeFactors(n);
int sum = getSum(n);
if ( prime == sum)
System.out.print(1);
else
System.out.print(0);
sc.close();
}
static int getSum(int n){
int sum = 0 ;
while ( n > 0){
sum += n%10;
n = n/10;
}
return sum;
}
static int getPrimeFactors(int n){
int result = 0 ;
while ( n%2 == 0){
result += 2 ;
n = n/2;
}
for( int i=3 ; i <= Math.sqrt(n) ; i+=2){
while ( n % i == 0){
if ( i > 9){
while ( i > 0){
result += i%10;
i=i/10;
}
}
else{
result += i;
n = n/i;
}
}
}
if ( n > 2){
if ( n > 9){
while ( n > 0){
result += n%10;
n=n/10;
}
}
else
result +=n;
}
return result;
}
}
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 →
What is the problem in my solution . Test case 2 and Test case 9 are not passing and its showing run time errors ? Thanks !!
import java.io.; import java.math.; import java.text.; import java.util.; import java.util.regex.*;
public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int prime = getPrimeFactors(n); int sum = getSum(n); if ( prime == sum) System.out.print(1); else System.out.print(0); sc.close(); }
}
}