Home Linux Shell Script to check a number is Evil Number or not

Shell Script to check a number is Evil Number or not

by Anup Maurya
3 minutes read

In this article, you’ll learn how to write a Shell Script to check a number is Evil Number or not. Before that we will explore what is Evil Number.

What is an Evil number ?

Evil number is a non negative number that has even number of 1 in its binary representation. E.g. Binary representation of 5 is 101 . It has two 1 in binary form. So it is an evil number.

Non evil numbers are called Odious number.

Input : 3
Output : Evil Number
Explanation: Binary expansion of 3 is 11,
the number of 1s in this is 2 i.e even. 

Input : 16
Output : Odious Number(not an evil number)
Explanation: Binary expansion of 16 = 10000, 
having number of 1s =1 i.e odd.

Input : 23
Output : Evil Number
Explanation: Binary expansion of 23 is 10111,
the number of 1s in this is 4 i.e even. 

Shell Script to check a number is Evil Number or not

#!/bin/bash

# Function to count the number of set bits (1s) in a binary representation
countSetBits() {
  local number=$1
  local count=0

  # Convert the number to binary
  local binary=$(echo "obase=2; $number" | bc)

  # Count the number of set bits
  for (( i = 0; i < ${#binary}; i++ )); do
    if [[ ${binary:i:1} == "1" ]]; then
      ((count++))
    fi
  done

  echo "$count"
}

# Read the input number from the user
read -p "Enter a number: " input

# Calculate the count of set bits
setBits=$(countSetBits $input)

# Check if the number of set bits is even or odd
if (( setBits % 2 == 0 )); then
  echo "$input is an Evil Number"
else
  echo "$input is not an Evil Number"
fi

Explanation:

  1. The script begins with the shebang (#!/bin/bash) to specify the interpreter to be used (in this case, Bash).
  2. The countSetBits function takes a number as input and counts the number of set bits (1s) in its binary representation. It uses the bc command-line calculator to convert the number to binary.
  3. In the main part of the script, the user is prompted to enter a number using the read command.
  4. The countSetBits function is called with the input number, and the result is stored in the setBits variable.
  5. The script then checks if the count of set bits (setBits) is even or odd using an if-else statement. If the count is even, the number is considered an Evil Number; otherwise, it is not.
  6. Finally, the script outputs the result to the user.

To use the script, save it to a file (e.g., evil_number.sh), make it executable (chmod +x evil_number.sh), and run it (./evil_number.sh).

Note: In this script, we assume that the input number is a positive integer. Additional input validation can be added if necessary.

An Evil Number is a positive integer that has an even number of set bits (1s) in its binary representation. The script calculates the count of set bits using a function and determines if the count is even or odd to identify Evil Numbers.

related posts

Leave a Comment

Enable Notifications OK No thanks