Posted on by Kalkicode
Code Number

Composite Number

In this article, we will discuss how to identify composite numbers. A composite number is a positive integer greater than 1 that has at least one positive divisor other than 1 and itself. In other words, a composite number can be divided by more than just 1 and itself without leaving a remainder.

Problem Statement

The goal of the code is to determine whether a given number is a composite number or not. To achieve this, the code first checks whether the number is a prime number using the isPrime function. If the number is not prime, i.e., it has divisors other than 1 and itself, it is identified as a composite number.

Explanation with Examples: Let's take a closer look at the code and its functionality with some examples.

  1. Test Case: isComposite(4) The function isComposite(4) calls the isPrime(4) function to check if 4 is prime. The isPrime function returns 0, indicating that 4 is not a prime number. Therefore, the isComposite function correctly identifies 4 as a composite number.

  2. Test Case: isComposite(13) The function isComposite(13) calls the isPrime(13) function to check if 13 is prime. The isPrime function returns 1, indicating that 13 is a prime number. Therefore, the isComposite function correctly identifies 13 as not being a composite number.

  3. Test Case: isComposite(22) The function isComposite(22) calls the isPrime(22) function to check if 22 is prime. The isPrime function returns 0, indicating that 22 is not a prime number. Therefore, the isComposite function correctly identifies 22 as a composite number.

  4. Test Case: isComposite(15) The function isComposite(15) calls the isPrime(15) function to check if 15 is prime. The isPrime function returns 0, indicating that 15 is not a prime number. Therefore, the isComposite function correctly identifies 15 as a composite number.

  5. Test Case: isComposite(31) The function isComposite(31) calls the isPrime(31) function to check if 31 is prime. The isPrime function returns 1, indicating that 31 is a prime number. Therefore, the isComposite function correctly identifies 31 as not being a composite number.

  6. Test Case: isComposite(9) The function isComposite(9) calls the isPrime(9) function to check if 9 is prime. The isPrime function returns 0, indicating that 9 is not a prime number. Therefore, the isComposite function correctly identifies 9 as a composite number.

Pseudocode

Function isPrime(num):
    If num is 2, 3, or 5:
        Return 1 (prime)
    If num is less than or equal to 1 OR num is divisible by 2 OR num is divisible by 3 OR num is divisible by 5:
        Return 0 (not prime)
    i = 11
    While i * i is less than or equal to num:
        If num is divisible by i:
            Return 0 (not prime)
        Else if num is divisible by i + 2:
            Return 0 (not prime)
        i = i + 6
    Return 1 (prime)

Function isComposite(number):
    result = 0
    If number is greater than 3 AND isPrime(number) is 0:
        result = 1
    If result is 1:
        Print "Number number is Composite"
    Else:
        Print "Number number is not Composite"

Algorithm Explanation

  1. The isPrime function checks whether the given number is prime or not. It uses a specialized form of the Sieve of Eratosthenes algorithm to efficiently check for primality. The function first handles small prime numbers (2, 3, and 5) as special cases and then uses a loop to check for divisibility starting from 11 and incrementing by 6 in each iteration (as all primes greater than 5 can be expressed in the form 6k ± 1).

  2. The isComposite function takes an integer 'number' as input and sets 'result' to 0 initially. It then checks whether 'number' is greater than 3 and not a prime number (using the isPrime function). If both conditions are true, it sets 'result' to 1, indicating that the number is a composite.

  3. The function then prints the appropriate message based on the value of 'result'.

Code Solution

Here given code implementation process.

A composite number is a positive integer greater than 1 that has at least one positive divisor other than 1 and itself. In other words, a composite number is any number that is not a prime number.

For example, the number 6 is a composite number because it has divisors other than 1 and itself: 2 and 3. The number 7, on the other hand, is a prime number because it has no positive divisors other than 1 and itself.

Some examples of other composite numbers are: 4, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, and so on.

Program Solution

// C Program 
// Composite Number
#include <stdio.h>

// Check that whether given number is prime or not
int isPrime(int num)
{
	if (num == 2 || num == 3 || num == 5)
	{
		return 1;
	}
	if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
	{
		return 0;
	}
	int i = 11;
	while ((i *i) <= num)
	{
		if (num % i == 0)
		{
			//When number is divisible of current i value
			return 0;
		}
		else if (num % (i + 2) == 0)
		{
			//When number is divisible of current i + 2 value
			return 0;
		}
		i = i + 6;
	}
	return 1;
}
void isComposite(int number)
{
	int result = 0;
	if (number > 3 && (isPrime(number) == 0))
	{
		// When number is greater than 3 and it's not prime number
		result = 1;
	}
	if (result == 1)
	{
		printf("  Number %d is Composite \n", number);
	}
	else
	{
		printf("  Number %d is not Composite\n", number);
	}
}
int main(int argc, char const *argv[])
{
	// Test Cases
	isComposite(4);
	isComposite(13);
	isComposite(22);
	isComposite(15);
	isComposite(31);
	isComposite(9);
	return 0;
}

Output

  Number 4 is Composite
  Number 13 is not Composite
  Number 22 is Composite
  Number 15 is Composite
  Number 31 is not Composite
  Number 9 is Composite
/*
    Java Program
    Composite Number
*/
public class CompositeNumber
{
	// Check that whether given number is prime or not
	public boolean isPrime(int num)
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		int i = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	public void isComposite(int number)
	{
		boolean result = false;
		if (number > 3 && (isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			System.out.print(" Number " + number + " is Composite \n");
		}
		else
		{
			System.out.print(" Number " + number + " is not Composite\n");
		}
	}
	public static void main(String[] args)
	{
		CompositeNumber task = new CompositeNumber();
		// Test Cases
		task.isComposite(4);
		task.isComposite(13);
		task.isComposite(22);
		task.isComposite(15);
		task.isComposite(31);
		task.isComposite(9);
	}
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Composite Number
*/

class CompositeNumber
{
	public:
		// Check that whether given number is prime or not
		bool isPrime(int num)
		{
			if (num == 2 || num == 3 || num == 5)
			{
				return true;
			}
			if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
			{
				return false;
			}
			int i = 11;
			while ((i *i) <= num)
			{
				if (num % i == 0)
				{
					//When number is divisible of current i value
					return false;
				}
				else if (num % (i + 2) == 0)
				{
					//When number is divisible of current i + 2 value
					return false;
				}
				i = i + 6;
			}
			return true;
		}
	void isComposite(int number)
	{
		bool result = false;
		if (number > 3 && (this->isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			cout << " Number " << number << " is Composite \n";
		}
		else
		{
			cout << " Number " << number << " is not Composite\n";
		}
	}
};
int main()
{
	CompositeNumber task = CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
	return 0;
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
// Include namespace system
using System;
/*
    C# Program
    Composite Number
*/
public class CompositeNumber
{
	// Check that whether given number is prime or not
	public Boolean isPrime(int num)
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		int i = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	public void isComposite(int number)
	{
		Boolean result = false;
		if (number > 3 && (isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			Console.Write(" Number " + number + " is Composite \n");
		}
		else
		{
			Console.Write(" Number " + number + " is not Composite\n");
		}
	}
	public static void Main(String[] args)
	{
		CompositeNumber task = new CompositeNumber();
		// Test Cases
		task.isComposite(4);
		task.isComposite(13);
		task.isComposite(22);
		task.isComposite(15);
		task.isComposite(31);
		task.isComposite(9);
	}
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
<?php
/*
    Php Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	public	function isPrime($num)
	{
		if ($num == 2 || $num == 3 || $num == 5)
		{
			return true;
		}
		if ($num <= 1 || ($num % 2 == 0) || ($num % 3 == 0) || ($num % 5 == 0))
		{
			return false;
		}
		$i = 11;
		while (($i * $i) <= $num)
		{
			if ($num % $i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if ($num % ($i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			$i = $i + 6;
		}
		return true;
	}
	public	function isComposite($number)
	{
		$result = false;
		if ($number > 3 && ($this->isPrime($number) == false))
		{
			// When number is greater than 3 and it's not prime number
			$result = true;
		}
		if ($result == true)
		{
			echo " Number ". $number ." is Composite \n";
		}
		else
		{
			echo " Number ". $number ." is not Composite\n";
		}
	}
}

function main()
{
	$task = new CompositeNumber();
	// Test Cases
	$task->isComposite(4);
	$task->isComposite(13);
	$task->isComposite(22);
	$task->isComposite(15);
	$task->isComposite(31);
	$task->isComposite(9);
}
main();

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
/*
    Node Js Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	isPrime(num)
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	isComposite(number)
	{
		var result = false;
		if (number > 3 && (this.isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			process.stdout.write(" Number " + number + " is Composite \n");
		}
		else
		{
			process.stdout.write(" Number " + number + " is not Composite\n");
		}
	}
}

function main()
{
	var task = new CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
}
main();

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
#  Python 3 Program
#  Composite Number

class CompositeNumber :
	#  Check that whether given number is prime or not
	def isPrime(self, num) :
		if (num == 2 or num == 3 or num == 5) :
			return True
		
		if (num <= 1 or(num % 2 == 0) or(num % 3 == 0) or(num % 5 == 0)) :
			return False
		
		i = 11
		while ((i * i) <= num) :
			if (num % i == 0) :
				# When number is divisible of current i value
				return False
			
			elif(num % (i + 2) == 0) :
				# When number is divisible of current i + 2 value
				return False
			
			i = i + 6
		
		return True
	
	def isComposite(self, number) :
		result = False
		if (number > 3 and(self.isPrime(number) == False)) :
			#  When number is greater than 3 and it's not prime number
			result = True
		
		if (result == True) :
			print(" Number ", number ," is Composite ")
		else :
			print(" Number ", number ," is not Composite")
		
	

def main() :
	task = CompositeNumber()
	#  Test Cases
	task.isComposite(4)
	task.isComposite(13)
	task.isComposite(22)
	task.isComposite(15)
	task.isComposite(31)
	task.isComposite(9)

if __name__ == "__main__": main()

Output

 Number  4  is Composite
 Number  13  is not Composite
 Number  22  is Composite
 Number  15  is Composite
 Number  31  is not Composite
 Number  9  is Composite
#  Ruby Program
#  Composite Number

class CompositeNumber 
	#  Check that whether given number is prime or not
	def isPrime(num) 
		if (num == 2 || num == 3 || num == 5) 
			return true
		end

		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0)) 
			return false
		end

		i = 11
		while ((i * i) <= num) 
			if (num % i == 0) 
				# When number is divisible of current i value
				return false
			elsif(num % (i + 2) == 0) 
				# When number is divisible of current i + 2 value
				return false
			end

			i = i + 6
		end

		return true
	end

	def isComposite(number) 
		result = false
		if (number > 3 && (self.isPrime(number) == false)) 
			#  When number is greater than 3 and it's not prime number
			result = true
		end

		if (result == true) 
			print(" Number ", number ," is Composite \n")
		else 
			print(" Number ", number ," is not Composite\n")
		end

	end

end

def main() 
	task = CompositeNumber.new()
	#  Test Cases
	task.isComposite(4)
	task.isComposite(13)
	task.isComposite(22)
	task.isComposite(15)
	task.isComposite(31)
	task.isComposite(9)
end

main()

Output

 Number 4 is Composite 
 Number 13 is not Composite
 Number 22 is Composite 
 Number 15 is Composite 
 Number 31 is not Composite
 Number 9 is Composite 
/*
    Scala Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	def isPrime(num: Int): Boolean = {
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i: Int = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	def isComposite(number: Int): Unit = {
		var result: Boolean = false;
		if (number > 3 && (this.isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			print(" Number " + number + " is Composite \n");
		}
		else
		{
			print(" Number " + number + " is not Composite\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CompositeNumber = new CompositeNumber();
		// Test Cases
		task.isComposite(4);
		task.isComposite(13);
		task.isComposite(22);
		task.isComposite(15);
		task.isComposite(31);
		task.isComposite(9);
	}
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
/*
    Swift 4 Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	func isPrime(_ num: Int)->Bool
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i: Int = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	func isComposite(_ number: Int)
	{
		var result: Bool = false;
		if (number > 3 && (self.isPrime(number) == false))
		{
			// When number is greater than 3 and it"s not prime number
			result = true;
		}
		if (result == true)
		{
			print(" Number ", number ," is Composite ");
		}
		else
		{
			print(" Number ", number ," is not Composite");
		}
	}
}
func main()
{
	let task: CompositeNumber = CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
}
main();

Output

 Number  4  is Composite
 Number  13  is not Composite
 Number  22  is Composite
 Number  15  is Composite
 Number  31  is not Composite
 Number  9  is Composite
/*
    Kotlin Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	fun isPrime(num: Int): Boolean
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i: Int = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	fun isComposite(number: Int): Unit
	{
		var result: Boolean = false;
		if (number > 3 && (this.isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			print(" Number " + number + " is Composite \n");
		}
		else
		{
			print(" Number " + number + " is not Composite\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: CompositeNumber = CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite

Resultant Output Explanation

The code outputs whether each test case number is a composite number or not. If a number is identified as a composite, it prints "Number number is Composite." Otherwise, it prints "Number number is not Composite."

Time Complexity

The time complexity of the isPrime function primarily depends on the value of the input number 'num.' For each number from 6 to the square root of 'num,' the function performs a constant amount of work (checking divisibility). So, the time complexity can be approximated as O(sqrt(num)). The isComposite function calls isPrime, so its time complexity is also O(sqrt(number)).

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.

New Comment