The Programming Project: JAVA Code for Smith Number
Showing posts with label JAVA Code for Smith Number. Show all posts
Showing posts with label JAVA Code for Smith Number. Show all posts

Saturday, August 2, 2014

ISC COMPUTER SCIENCE PRACTICALS SOLVED ~ 2008 ~ SMITH Number

/*A smith number is a composite number, the sum of whose digits is the sum of
the digits of its prime factors obtained as a result of prime factorization
(excluding 1). The first few such numbers are 4, 22, 27, 58, 85, 94, 121 ...

Example;
1. 666
Prime factors are 2, 3, 3 and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7) = 18

2. 4937775
Prime factors are 3, 5, 5, 65837
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits of the factors (3+5+5+(6+5+8+3+7) = 42

Write a program to input a number and display whether the number is a Smith
number or not.
Sample data:

Input         Output
94        SMITH Number

Input        Output
102        NOT SMITH Number

Input        Output
666        SMITH Number

Input        Output
999        NOT SMITH Number

ISC COMPUTER SCIENCE PRACTICALS SOLVED ~ 2008 */

import java.util.*;
public class SmithNumber {
    public static void main(String[] args) {
        int N;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number");
        N = in.nextInt();
        SmithCheck sc = new SmithCheck(N);
        if(sc.sumOfdigits(N) == sc.primeFactors())
            System.out.println("SMITH Number");
        else
            System.out.println("Not SMITH Number");   
        }
    }
class SmithCheck {
    SmithCheck(int N) {
        Numb = N;
        primefactorsSum = 0;
        }
    public int sumOfdigits(int n) {
        dSum = 0;
        sNumb = ""+n;
        for ( int i = 0; i < sNumb.length(); i++) {
            dSum += sNumb.charAt(i)-48;
            }
        return dSum;   
        }    
    public int primeFactors() {
        int Ntemp,count;
        for ( int j = 2; j <= Numb; j++) {       
            Ntemp = Numb;
            count = 0;
            if ( SmithCheck.isFactor(j) == true && SmithCheck.isPrime(j) == true ) {
                while ( Ntemp%j == 0) {
                    count++;
                    Ntemp /= j;
                    }
                primefactorsSum += count*sumOfdigits(j);       
                }
            }
        return primefactorsSum;   
        }   
    public static boolean isFactor(int n) {
        return ( Numb % n == 0 ? true : false );
        }   
    public static boolean isPrime(int n) {
        flag = false;
        for ( int j = 2; j <= Math.sqrt(n); j++) {
                if( n%j == 0) {
                    flag = true;
                    break;
                    }
                }
            return (flag == true ? false : true);
        }   
    private static boolean flag;   
    private static int Numb;
    private int dSum;
    private int primefactorsSum;
    private String sNumb;   
    }