DEV Community

SILAMBARASAN A
SILAMBARASAN A

Posted on

NEON, STRONG, PERFECT

NEON :

Neon Number

A Neon Number is a number where:

Sum of digits of its square = the number itself

Formula Idea:

  1. Take the number
  2. Find its square
  3. Add the digits of the square
  4. If result = original number → Neon Number

Example :

Number = 9

  • Square → 9 × 9 = 81
  • Sum of digits → 8 + 1 = 9

Result = 9 → Neon Number

PYTHON :

def neon(n):
    fact=n*n
    sum=0
    while fact>0:
        sum=sum+fact%10
        fact=fact//10
    if(n==sum):
        print(n," is neno")
    else:
        print(n," is not neno")
print(neon(9))
Enter fullscreen mode Exit fullscreen mode

JAVA SCRIPT :

function neon(n) {
    let fact = n * n;
    let sum = 0;

    while (fact > 0) {
        sum = sum + (fact % 10);
        fact = Math.floor(fact / 10);
    }

    if (n === sum) {
        console.log(n + " is neon");
    } else {
        console.log(n + " is not neon");
    }
}

neon(9);
Enter fullscreen mode Exit fullscreen mode

JAVA :

public class NeonNumber {

    public static void neon(int n) {
        int fact = n * n;
        int sum = 0;

        while (fact > 0) {
            sum = sum + (fact % 10);
            fact = fact / 10;  // integer division
        }

        if (n == sum) {
            System.out.println(n + " is neon");
        } else {
            System.out.println(n + " is not neon");
        }
    }

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

OUTPUT :

Strong Number :

A Strong Number is a number where:

Sum of factorial of each digit = the number itself

Formula Idea:

  1. Take the number
  2. Separate each digit
  3. Find factorial of each digit
  4. Add all factorials
  5. If result = original number → Strong Number

Example :

Number = 145

  • Digits → 1, 4, 5
  • Factorials →
    • 1! = 1
    • 4! = 24
    • 5! = 120
  • Sum → 1 + 24 + 120 = 145

Result = 145 → Strong Number

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

JAVA SCRIPT :

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

function strong(num) {
    let numc = num;
    let sum = 0;

    while (numc > 0) {
        let digit = numc % 10;
        sum = sum + factorial(digit);
        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

JAVA :

public class StrongNumber {

    // Factorial function (recursion)
    public static int factorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * factorial(n - 1);
    }

    // Strong number check
    public static void strong(int num) {
        int numc = num;
        int sum = 0;

        while (numc > 0) {
            int digit = numc % 10;
            sum = sum + factorial(digit);
            numc = 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

OUTPUT :

Perfect Number – Definition

A Perfect Number is a number where:

Sum of its proper divisors = the number itself

What are Proper Divisors?

All positive divisors of a number excluding the number itself

Example :

Number = 6

  • Divisors → 1, 2, 3
  • Sum → 1 + 2 + 3 = 6 Result = 6 → Perfect Number

PYTHON :

def divisor(n):
    div=1
    divsum=0
    while div<=n/2:
        if n%div==0:
            divsum=divsum+div
        div+=1
    if n==divsum:
        print(n," is perfect")
    else:
        print(n," is not perfect")
divisor(6)
Enter fullscreen mode Exit fullscreen mode

JAVA SCRIPT :

function divisor(n) {
    let div = 1;
    let divsum = 0;

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

    if (n === divsum) {
        console.log(n + " is perfect");
    } else {
        console.log(n + " is not perfect");
    }
}

divisor(6);
Enter fullscreen mode Exit fullscreen mode

JAVA :

public class PerfectNumber {

    public static void divisor(int n) {
        int div = 1;
        int divsum = 0;

        while (div <= n / 2) {
            if (n % div == 0) {
                divsum = divsum + div;
            }
            div++;
        }

        if (n == divsum) {
            System.out.println(n + " is perfect");
        } else {
            System.out.println(n + " is not perfect");
        }
    }

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

OUTPUT :

Top comments (0)