Code with Vinay

ISC practical papers › ISC 2008 › Question 1

Smith Number

ISC 2008 Computer Science Practical, Question 1 · Number programs

The question

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:  94
Output: SMITH Number
Input:  102
Output: NOT SMITH Number
Input:  666
Output: SMITH Number
Input:  999
Output: NOT SMITH Number

Question: ISC 2008 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2008Q1.java Download GitHub
import java.util.*;

class Number {
    private int n, temp;

    public Number(int n) {
        this.n = n;
    }

    private static int getSumOfDigits(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    private int getSumOfFactors(int divisor) {
        int sumOfDigits = 0, divisiorSum = getSumOfDigits(divisor);
        while (temp % divisor == 0) {
            temp /= divisor;
            sumOfDigits += divisiorSum;
        }
        return sumOfDigits;
    }

    public boolean isSmithNumber() {
        if (n == 2)
            return false;
        temp = n;
        int divisor = 2, sumOfPrimeFactors = 0, half = n / 2;
        sumOfPrimeFactors += getSumOfFactors(divisor);
        for (divisor = 3; divisor <= half; divisor += 2) {
            sumOfPrimeFactors += getSumOfFactors(divisor);
        }
        if (temp > 1 || sumOfPrimeFactors != getSumOfDigits(n))
            return false;
        else
            return true;
    }
}

class ISC2008Q1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\t");
        int n = in.nextInt();
        Number obj = new Number(n);
        System.out.print("OUTPUT:\t");
        if (obj.isSmithNumber()) {
            System.out.println("SMITH Number");
        } else {
            System.out.println("NOT SMITH Number");
        }
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers