Posted on by Kalkicode
Code Number

# Odious number

An odious number is a positive integer that has an odd number of 1s in its binary expansion. In other words, an odious number is characterized by having an odd count of active bits, where an active bit is represented by 1 in its binary form. The concept of odious numbers was introduced by mathematician Albert Eagle in 1972.

To determine whether a given number is odious or not, we can follow a simple algorithm:

1. Take the input number.
2. If the number is greater than 0, proceed to the next step. Otherwise, it is not an odious number.
3. Convert the number to its binary representation.
4. Count the number of active bits (1s) in the binary representation.
5. If the count of active bits is odd, the number is odious. Otherwise, it is not an odious number.

Let's understand this algorithm with an example:

Example:

Consider the number 233.

• In binary, 233 is represented as 11101001.
• The count of active bits (1s) is 5, which is an odd number.
• Therefore, 233 is an odious number.

The time complexity of this algorithm is O(log n), where n is the given number. This is because we iterate through the bits of the number to count the active bits.

Now, let's take a look at the pseudocode representation of the algorithm:

``````
isOdiousNo(num):
result = 0
if num > 0:
n = num
count = 0
while n > 0:
if (1 & n) == 1:
count = count + 1
n = n >> 1
if count % 2 != 0:
result = 1

if result == 1:
print "Number num is an Odious Number"
else:
print "Number num is not an Odious Number"

main():
isOdiousNo(2357)
isOdiousNo(233)
isOdiousNo(9867)```
```

The output of the given code would be:

```Number 2357 is not an Odious Number
Number 233 is an Odious Number
Number 9867 is an Odious Number
```

## Code Solution

Here given code implementation process.

``````// C Program
// Odious number
#include <stdio.h>

void isOdiousNo(int num)
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
int result = 0;
if (num > 0)
{
// Case 1 valid
int n = num;
int count = 0;
while (n > 0)
{
if ((1 & n) == 1)
{
// Count active bits
count++;
}
// Right shift
n = n >> 1;
}
if (count % 2 != 0)
{
result = 1;
}
}
if (result == 1)
{
printf("\n Number %d is Odious Number ", num);
}
else
{
printf("\n Number %d is not Odious Number ", num);
}
}
int main()
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
isOdiousNo(2357);
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
isOdiousNo(233);
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
isOdiousNo(9867);
return 0;
}``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````/*
Java Program
Odious number
*/
public class Checker
{
public void isOdiousNo(int num)
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
int result = 0;
if (num > 0)
{
// Case 1 valid
int n = num;
int count = 0;
while (n > 0)
{
if ((1 & n) == 1)
{
// Count active bits
count++;
}
// Right shift
n = n >> 1;
}
if (count % 2 != 0)
{
result = 1;
}
}
if (result == 1)
{
System.out.print("\n Number " +
num + " is Odious Number ");
}
else
{
System.out.print("\n Number " +
num + " is not Odious Number ");
}
}
public static void main(String[] args)
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
}
}``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Odious number
*/
class Checker
{
public: void isOdiousNo(int num)
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
int result = 0;
if (num > 0)
{
// Case 1 valid
int n = num;
int count = 0;
while (n > 0)
{
if ((1 &n) == 1)
{
// Count active bits
count++;
}
// Right shift
n = n >> 1;
}
if (count % 2 != 0)
{
result = 1;
}
}
if (result == 1)
{
cout << "\n Number " << num << " is Odious Number ";
}
else
{
cout << "\n Number " << num << " is not Odious Number ";
}
}
};
int main()
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
return 0;
}``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````// Include namespace system
using System;
/*
Csharp Program
Odious number
*/
public class Checker
{
public void isOdiousNo(int num)
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
int result = 0;
if (num > 0)
{
// Case 1 valid
int n = num;
int count = 0;
while (n > 0)
{
if ((1 & n) == 1)
{
// Count active bits
count++;
}
// Right shift
n = n >> 1;
}
if (count % 2 != 0)
{
result = 1;
}
}
if (result == 1)
{
Console.Write("\n Number " + num + " is Odious Number ");
}
else
{
Console.Write("\n Number " + num + " is not Odious Number ");
}
}
public static void Main(String[] args)
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
}
}``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````package main
import "fmt"
/*
Go Program
Odious number
*/

func isOdiousNo(num int) {
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
var result int = 0
if num > 0 {
// Case 1 valid
var n int = num
var count int = 0
for (n > 0) {
if (1 & n) == 1 {
// Count active bits
count++
}
// Right shift
n = n >> 1
}
if count % 2 != 0 {
result = 1
}
}
if result == 1 {
fmt.Print("\n Number ", num, " is Odious Number ")
} else {
fmt.Print("\n Number ", num, " is not Odious Number ")
}
}
func main() {

// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
isOdiousNo(2357)
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
isOdiousNo(233)
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
isOdiousNo(9867)
}``````

#### Output

`````` Number  2357  is not Odious Number
Number  233  is Odious Number
Number  9867  is Odious Number``````
``````<?php
/*
Php Program
Odious number
*/
class Checker
{
public	function isOdiousNo(\$num)
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
\$result = 0;
if (\$num > 0)
{
// Case 1 valid
\$n = \$num;
\$count = 0;
while (\$n > 0)
{
if ((1 & \$n) == 1)
{
// Count active bits
\$count++;
}
// Right shift
\$n = \$n >> 1;
}
if (\$count % 2 != 0)
{
\$result = 1;
}
}
if (\$result == 1)
{
echo("\n Number ".\$num.
" is Odious Number ");
}
else
{
echo("\n Number ".\$num.
" is not Odious Number ");
}
}
}

function main()
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
}
main();``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````/*
Node JS Program
Odious number
*/
class Checker
{
isOdiousNo(num)
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
var result = 0;
if (num > 0)
{
// Case 1 valid
var n = num;
var count = 0;
while (n > 0)
{
if ((1 & n) == 1)
{
// Count active bits
count++;
}
// Right shift
n = n >> 1;
}
if (count % 2 != 0)
{
result = 1;
}
}
if (result == 1)
{
process.stdout.write("\n Number " +
num + " is Odious Number ");
}
else
{
process.stdout.write("\n Number " +
num + " is not Odious Number ");
}
}
}

function main()
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
}
main();``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````#    Python 3 Program
#    Odious number
class Checker :
def isOdiousNo(self, num) :
#  Test case
#  ➀ Number is positive
#  ➁ Odd number of 1s in its binary expansion
#  (Active bits (1s) Occurs Odd number)
result = 0
if (num > 0) :
#  Case 1 valid
n = num
count = 0
while (n > 0) :
if ((1 & n) == 1) :
#  Count active bits
count += 1

#  Right shift
n = n >> 1

if (count % 2 != 0) :
result = 1

if (result == 1) :
print("\n Number ", num ,
" is Odious Number ", end = "", sep = "")
else :
print("\n Number ", num ,
" is not Odious Number ", end = "", sep = "")

def main() :
#  Test A
#  n = 2357
#  [100100110101] Binary
#  Active bits = 6 (1s)
#  Result : No
#  Test B
#  n = 233
#  [11101001] Binary
#  Active bits = 5 (1s)
#  Result : Yes
#  Test C
#  n = 9867
#  [10011010001011] Binary
#  Active bits = 7 (1s)
#  Result : Yes

if __name__ == "__main__": main()``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````#    Ruby Program
#    Odious number
class Checker
def isOdiousNo(num)
#  Test case
#  ➀ Number is positive
#  ➁ Odd number of 1s in its binary expansion
#  (Active bits (1s) Occurs Odd number)
result = 0
if (num > 0)
#  Case 1 valid
n = num
count = 0
while (n > 0)
if ((1 & n) == 1)
#  Count active bits
count += 1
end

#  Right shift
n = n >> 1
end

if (count % 2 != 0)
result = 1
end

end

if (result == 1)
print("\n Number ", num ," is Odious Number ")
else

print("\n Number ", num ," is not Odious Number ")
end

end

end

def main()
#  Test A
#  n = 2357
#  [100100110101] Binary
#  Active bits = 6 (1s)
#  Result : No
#  Test B
#  n = 233
#  [11101001] Binary
#  Active bits = 5 (1s)
#  Result : Yes
#  Test C
#  n = 9867
#  [10011010001011] Binary
#  Active bits = 7 (1s)
#  Result : Yes
end

main()``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number ``````
``````/*
Scala Program
Odious number
*/
class Checker()
{
def isOdiousNo(num: Int): Unit = {
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
var result: Int = 0;
if (num > 0)
{
// Case 1 valid
var n: Int = num;
var count: Int = 0;
while (n > 0)
{
if ((1 & n) == 1)
{
// Count active bits
count += 1;
}
// Right shift
n = n >> 1;
}
if (count % 2 != 0)
{
result = 1;
}
}
if (result == 1)
{
print("\n Number " + num + " is Odious Number ");
}
else
{
print("\n Number " + num + " is not Odious Number ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Checker = new Checker();
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
}
}``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````
``````/*
Swift 4 Program
Odious number
*/
class Checker
{
func isOdiousNo(_ num: Int)
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
var result: Int = 0;
if (num > 0)
{
// Case 1 valid
var n: Int = num;
var count: Int = 0;
while (n > 0)
{
if ((1 & n) == 1)
{
// Count active bits
count += 1;
}
// Right shift
n = n >> 1;
}
if (count % 2  != 0)
{
result = 1;
}
}
if (result == 1)
{
print("\n Number ", num ,
" is Odious Number ", terminator: "");
}
else
{
print("\n Number ", num ,
" is not Odious Number ", terminator: "");
}
}
}
func main()
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
}
main();``````

#### Output

`````` Number  2357  is not Odious Number
Number  233  is Odious Number
Number  9867  is Odious Number``````
``````/*
Kotlin Program
Odious number
*/
class Checker
{
fun isOdiousNo(num: Int): Unit
{
// Test case
// ➀ Number is positive
// ➁ Odd number of 1s in its binary expansion
// (Active bits (1s) Occurs Odd number)
var result: Int = 0;
if (num > 0)
{
// Case 1 valid
var n: Int = num;
var count: Int = 0;
while (n > 0)
{
if ((1 and n) == 1)
{
// Count active bits
count += 1;
}
// Right shift
n = n shr 1;
}
if (count % 2 != 0)
{
result = 1;
}
}
if (result == 1)
{
print("\n Number " + num + " is Odious Number ");
}
else
{
print("\n Number " + num + " is not Odious Number ");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test A
// n = 2357
// [100100110101] Binary
// Active bits = 6 (1s)
// Result : No
// Test B
// n = 233
// [11101001] Binary
// Active bits = 5 (1s)
// Result : Yes
// Test C
// n = 9867
// [10011010001011] Binary
// Active bits = 7 (1s)
// Result : Yes
}``````

#### Output

`````` Number 2357 is not Odious Number
Number 233 is Odious Number
Number 9867 is Odious Number``````

In the given code, we have three test cases:

1. 2357 - The binary representation of 2357 is 100100110101. The count of active bits is 6, which is an even number. Therefore, 2357 is not an odious number.
2. 233 - The binary representation of 233 is 11101001. The count of active bits is 5, which is an odd number. Therefore, 233 is an odious number.
3. 9867 - The binary representation of 9867 is 10011010001011. The count of active bits is 7, which is an odd number. Therefore, 9867 is an odious number.

By following the algorithm and running the code, we can easily determine whether a given number is an odious number or not based on its binary representation and the count of active bits. Odious numbers have applications in various mathematical problems and algorithms.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post