DEV Community

Cover image for “Core Number Programs Every Programmer Should Know: Emirp, Strong, Perfect & Neon”
Arul .A
Arul .A

Posted on

“Core Number Programs Every Programmer Should Know: Emirp, Strong, Perfect & Neon”

1.EMIRP program :

Definition:

  • A number is called EMIRP .The given number is prime and also it reverse the number and it is also prime,The reversed number is different from the original number.That number is EMIRP number.

Example:

13 → reverse = 31

  • 13 is prime
  • 31 is also prime
  • 13 ≠ 31
    So, 13 is an Emirp Number.

  • In python:

def prime(no):
    div=2
    while div< no/2:
        if no%div==0:
            return False
        div+=1
    return True

def reverse(no):
    rev=0
    while no>0:
        rev=rev*10+no%10
        no=no//10
    return rev

num=17
if prime(num) and prime(reverse(num)):
    print(num,"is emirp")
else:
    print(num,"is not emirp")
Enter fullscreen mode Exit fullscreen mode

Output:


-In Javascript:

function isPrime(no) {
    if (no < 2) return false;

    for (let i = 2; i <= Math.sqrt(no); i++) {
        if (no % i === 0) {
            return false;
        }
    }
    return true;
}

function reverse(no) {
    let rev = 0;
    while (no > 0) {
        rev = rev * 10 + (no % 10);
        no = Math.floor(no / 10);
    }
    return rev;
}

let num = 17;

if (isPrime(num) && isPrime(reverse(num)) && num !== reverse(num)) {
    console.log(num + " is emirp");
} else {
    console.log(num + " is not emirp");
}
Enter fullscreen mode Exit fullscreen mode

-In Java :

public class EmirpNumber {

    static boolean isPrime(int no) {
        if (no < 2) return false;

        for (int i = 2; i <= Math.sqrt(no); i++) {
            if (no % i == 0) {
                return false;
            }
        }
        return true;
    }

    static int reverse(int no) {
        int rev = 0;
        while (no > 0) {
            rev = rev * 10 + no % 10;
            no = no / 10;
        }
        return rev;
    }

    public static void main(String[] args) {
        int num = 17;

        if (isPrime(num) && isPrime(reverse(num)) && num != reverse(num)) {
            System.out.println(num + " is emirp");
        } else {
            System.out.println(num + " is not emirp");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2.Perfect no :

  • The perfect number is a number that is equal to the sum of divisors.

Example:
- The given number is 6.The divisor of 6 is 1,2,3.So the number is equal to the sum of divisors.

           Sum:1 + 2 + 3 = 6
Enter fullscreen mode Exit fullscreen mode

-In python :

def div(no):
    div = 1
    total = 0

    while div <= no // 2:
        if no % div == 0:
            total += div
        div += 1

    if total == no:
        print("Perfect Number",no)
    else:
        print("Not Perfect")

div(6)
Enter fullscreen mode Exit fullscreen mode

-In JavaScript:

function div(no) {
    let div = 1, total = 0;

    while (div <= Math.floor(no / 2)) {
        if (no % div === 0)
            total += div;
        div++;
    }

    if (total === no)
        console.log("Perfect Number", no);
    else
        console.log("Not Perfect");
}

div(6);
Enter fullscreen mode Exit fullscreen mode
  • In Java :
public class Main {
    static void div(int no) {
        int div = 1, total = 0;

        while (div <= no / 2) {
            if (no % div == 0)
                total += div;
            div++;
        }

        if (total == no)
            System.out.println("Perfect Number " + no);
        else
            System.out.println("Not Perfect");
    }

    public static void main(String[] args) {
        div(6);
    }
}
Enter fullscreen mode Exit fullscreen mode

3.Neon Number :

A number is called a neon number if:

  Sum of digits of its square = the number itself
Enter fullscreen mode Exit fullscreen mode

Example:

Square of 9 → 9 × 9 = 81

Sum of digits of 81 → 8 + 1 = 9

  • In Python :
def neon(no):
    square = no * no

    total = 0
    temp = square

    while temp > 0:
        digit = temp % 10
        total += digit
        temp = temp // 10

    if total == no:
        print(no, "is a Neon number")
    else:
        print(no, "is not a Neon number")

neon(9)
Enter fullscreen mode Exit fullscreen mode

Output :


-In JavaScript:

function neon(no) {
    let square = no * no;

    let total = 0;
    let temp = square;

    while (temp > 0) {
        let digit = temp % 10;
        total += digit;
        temp = Math.floor(temp / 10);
    }

    if (total === no)
        console.log(no + " is a Neon number");
    else
        console.log(no + " is not a Neon number");
}

neon(9);
Enter fullscreen mode Exit fullscreen mode
  • In Java :
public class Main {
    static void neon(int no) {
        int square = no * no;

        int total = 0;
        int temp = square;

        while (temp > 0) {
            int digit = temp % 10;
            total += digit;
            temp = temp / 10;
        }

        if (total == no)
            System.out.println(no + " is a Neon number");
        else
            System.out.println(no + " is not a Neon number");
    }

    public static void main(String[] args) {
        neon(9);
    }
}
Enter fullscreen mode Exit fullscreen mode

4.Strong Number :

A Strong Number is a number where the sum of the factorials of its digits equals the number itself.

       Sum of factorial of each digit = Original number
Enter fullscreen mode Exit fullscreen mode
  • In Python :
def factorial(n):
    if n==1:
        return 1
    return n*factorial(n-1)

def strong(num):
    numc=num
    sum=0
    while  numc>0:
        sum=sum+factorial(numc%10)
        numc=numc//10
    if sum==num:
        print( num," is strong")
    else:
        print( num," is not strong")

strong(145)
Enter fullscreen mode Exit fullscreen mode


Output:

-In JavaScript:

function factorial(n) {
    if (n === 1) return 1;
    return n * factorial(n - 1);
}

function strong(num) {
    let numc = num;
    let sum = 0;
    while (numc > 0) {
        sum += factorial(numc % 10);
        numc = Math.floor(numc / 10);
    }
    if (sum === num) {
        console.log(num + " is strong");
    } else {
        console.log(num + " is not strong");
    }
}

strong(145);
Enter fullscreen mode Exit fullscreen mode
  • In Java :
public class StrongNumber {
    static int factorial(int n) {
        if (n == 1) return 1;
        return n * factorial(n - 1);
    }

    static void strong(int num) {
        int numc = num;
        int sum = 0;
        while (numc > 0) {
            sum += factorial(numc % 10);
            numc /= 10;
        }
        if (sum == num) {
            System.out.println(num + " is strong");
        } else {
            System.out.println(num + " is not strong");
        }
    }

    public static void main(String[] args) {
        strong(145);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)