Find all prime factors of a number

Here given code implementation process.

// C Program
// Find all prime factors of a number
#include <stdio.h>
#include <math.h>

// Find the prime factors of a given number
void primeFactors(int num)
{
	printf("\n Prime Factors of %d is \n", num);
	int i = 2;
	// Execute loop until when (i) less than of square root of num
	while (i <= sqrt(num))
	{
		// Inner loop
		while (num % i == 0)
		{
			// When (i) is divisible by num
			printf(" %d", i);
			// Reduce number
			num = num / i;
		}
		if (i == 2)
		{
			// When (i) is first prime  then visit on next prime
			i = i + 1;
		}
		else
		{
			i = i + 2;
		}
	}
	if (num > 2)
	{
		// When number is greater than 2 then print num
		printf(" %d\n", num);
	}
}
int main()
{
	// Test Case
	primeFactors(136);
	primeFactors(760);
	primeFactors(22);
	primeFactors(23);
	return 0;
}

Output

 Prime Factors of 136 is
 2 2 2 17

 Prime Factors of 760 is
 2 2 2 5 19

 Prime Factors of 22 is
 2 11

 Prime Factors of 23 is
 23
/*
   Java Program
   Find all prime factors of a number
*/
public class Factorization
{
	// Find the prime factors of a given number
	public void primeFactors(int num)
	{
		System.out.print("\n Prime Factors of " + num + " is \n");
		int i = 2;
		// Execute loop until when (i) less than of square root of num
		while (i <= (int) Math.sqrt(num))
		{
			// Inner loop
			while (num % i == 0)
			{
				// When (i) is divisible by num
				System.out.print("  " + i);
				// Reduce number
				num = num / i;
			}
			if (i == 2)
			{
				// When (i) is first prime  then visit on next prime
				i = i + 1;
			}
			else
			{
				i = i + 2;
			}
		}
		if (num > 2)
		{
			// When number is greater than 2 then print num
			System.out.print("  " + num + "\n");
		}
	}
	public static void main(String[] args)
	{
		Factorization task = new Factorization();
		// Test Case
		task.primeFactors(136);
		task.primeFactors(760);
		task.primeFactors(22);
		task.primeFactors(23);
	}
}

Output

 Prime Factors of 136 is
  2  2  2  17

 Prime Factors of 760 is
  2  2  2  5  19

 Prime Factors of 22 is
  2  11

 Prime Factors of 23 is
  23
// Include header file
#include <iostream>
#include <math.h>

using namespace std;
/*
   C++ Program
   Find all prime factors of a number
*/
class Factorization
{
	public:
		// Find the prime factors of a given number
		void primeFactors(int num)
		{
			cout << "\n Prime Factors of " << num << " is \n";
			int i = 2;
			// Execute loop until when (i) less than of square root of num
			while (i <= (int) sqrt(num))
			{
				// Inner loop
				while (num % i == 0)
				{
					// When (i) is divisible by num
					cout << "  " << i;
					// Reduce number
					num = num / i;
				}
				if (i == 2)
				{
					// When (i) is first prime  then visit on next prime
					i = i + 1;
				}
				else
				{
					i = i + 2;
				}
			}
			if (num > 2)
			{
				// When number is greater than 2 then print num
				cout << "  " << num << "\n";
			}
		}
};
int main()
{
	Factorization task = Factorization();
	// Test Case
	task.primeFactors(136);
	task.primeFactors(760);
	task.primeFactors(22);
	task.primeFactors(23);
	return 0;
}

Output

 Prime Factors of 136 is
  2  2  2  17

 Prime Factors of 760 is
  2  2  2  5  19

 Prime Factors of 22 is
  2  11

 Prime Factors of 23 is
  23
// Include namespace system
using System;
/*
   C# Program
   Find all prime factors of a number
*/
public class Factorization
{
	// Find the prime factors of a given number
	public void primeFactors(int num)
	{
		Console.Write("\n Prime Factors of " + num + " is \n");
		int i = 2;
		// Execute loop until when (i) less than of square root of num
		while (i <= (int) Math.Sqrt(num))
		{
			// Inner loop
			while (num % i == 0)
			{
				// When (i) is divisible by num
				Console.Write("  " + i);
				// Reduce number
				num = num / i;
			}
			if (i == 2)
			{
				// When (i) is first prime  then visit on next prime
				i = i + 1;
			}
			else
			{
				i = i + 2;
			}
		}
		if (num > 2)
		{
			// When number is greater than 2 then print num
			Console.Write("  " + num + "\n");
		}
	}
	public static void Main(String[] args)
	{
		Factorization task = new Factorization();
		// Test Case
		task.primeFactors(136);
		task.primeFactors(760);
		task.primeFactors(22);
		task.primeFactors(23);
	}
}

Output

 Prime Factors of 136 is
  2  2  2  17

 Prime Factors of 760 is
  2  2  2  5  19

 Prime Factors of 22 is
  2  11

 Prime Factors of 23 is
  23
<?php
/*
   Php Program
   Find all prime factors of a number
*/
class Factorization
{
	// Find the prime factors of a given number
	public	function primeFactors($num)
	{
		echo "\n Prime Factors of ". $num ." is \n";
		$i = 2;
		// Execute loop until when (i) less than of square root of num
		while ($i <= (int) sqrt($num))
		{
			// Inner loop
			while ($num % $i == 0)
			{
				// When (i) is divisible by num
				echo "  ". $i;
				// Reduce number
				$num = intval($num / $i);
			}
			if ($i == 2)
			{
				// When (i) is first prime  then visit on next prime
				$i = $i + 1;
			}
			else
			{
				$i = $i + 2;
			}
		}
		if ($num > 2)
		{
			// When number is greater than 2 then print num
			echo "  ". $num ."\n";
		}
	}
}

function main()
{
	$task = new Factorization();
	// Test Case
	$task->primeFactors(136);
	$task->primeFactors(760);
	$task->primeFactors(22);
	$task->primeFactors(23);
}
main();

Output

 Prime Factors of 136 is
  2  2  2  17

 Prime Factors of 760 is
  2  2  2  5  19

 Prime Factors of 22 is
  2  11

 Prime Factors of 23 is
  23
/*
   Node Js Program
   Find all prime factors of a number
*/
class Factorization
{
	// Find the prime factors of a given number
	primeFactors(num)
	{
		process.stdout.write("\n Prime Factors of " + num + " is \n");
		var i = 2;
		// Execute loop until when (i) less than of square root of num
		while (i <= parseInt(Math.sqrt(num)))
		{
			// Inner loop
			while (num % i == 0)
			{
				// When (i) is divisible by num
				process.stdout.write("  " + i);
				// Reduce number
				num = parseInt(num / i);
			}
			if (i == 2)
			{
				// When (i) is first prime  then visit on next prime
				i = i + 1;
			}
			else
			{
				i = i + 2;
			}
		}
		if (num > 2)
		{
			// When number is greater than 2 then print num
			process.stdout.write("  " + num + "\n");
		}
	}
}

function main()
{
	var task = new Factorization();
	// Test Case
	task.primeFactors(136);
	task.primeFactors(760);
	task.primeFactors(22);
	task.primeFactors(23);
}
main();

Output

 Prime Factors of 136 is
  2  2  2  17

 Prime Factors of 760 is
  2  2  2  5  19

 Prime Factors of 22 is
  2  11

 Prime Factors of 23 is
  23
import math
#    Python 3 Program
#    Find all prime factors of a number

class Factorization :
	#  Find the prime factors of a given number
	def primeFactors(self, num) :
		print("\n Prime Factors of ", num ," is ")
		i = 2
		#  Execute loop until when (i) less than of square root of num
		while (i <= int(math.sqrt(num))) :
			#  Inner loop
			while (num % i == 0) :
				#  When (i) is divisible by num
				print("  ", i, end = "")
				#  Reduce number
				num = int(num / i)
			
			if (i == 2) :
				#  When (i) is first prime  then visit on next prime
				i = i + 1
			else :
				i = i + 2
			
		
		if (num > 2) :
			#  When number is greater than 2 then print num
			print("  ", num )
		
	

def main() :
	task = Factorization()
	#  Test Case
	task.primeFactors(136)
	task.primeFactors(760)
	task.primeFactors(22)
	task.primeFactors(23)

if __name__ == "__main__": main()

Output

 Prime Factors of  136  is
   2   2   2   17

 Prime Factors of  760  is
   2   2   2   5   19

 Prime Factors of  22  is
   2   11

 Prime Factors of  23  is
   23
#  Ruby Program
#  Find all prime factors of a number

class Factorization 
	#  Find the prime factors of a given number
	def primeFactors(num) 
		print("\n Prime Factors of ", num ," is \n")
		i = 2
		#  Execute loop until when (i) less than of square root of num
		while (i <= (Math.sqrt(num)).to_i) 
			#  Inner loop
			while (num % i == 0) 
				#  When (i) is divisible by num
				print("  ", i)
				#  Reduce number
				num = num / i
			end

			if (i == 2) 
				#  When (i) is first prime  then visit on next prime
				i = i + 1
			else 
				i = i + 2
			end

		end

		if (num > 2) 
			#  When number is greater than 2 then print num
			print("  ", num ,"\n")
		end

	end

end

def main() 
	task = Factorization.new()
	#  Test Case
	task.primeFactors(136)
	task.primeFactors(760)
	task.primeFactors(22)
	task.primeFactors(23)
end

main()

Output

 Prime Factors of 136 is 
  2  2  2  17

 Prime Factors of 760 is 
  2  2  2  5  19

 Prime Factors of 22 is 
  2  11

 Prime Factors of 23 is 
  23
/*
   Scala Program
   Find all prime factors of a number
*/
class Factorization
{
	// Find the prime factors of a given number
	def primeFactors(n: Int): Unit = {
      	var num = n;	
		print("\n Prime Factors of " + num + " is \n");
		var i: Int = 2;
	
      // Execute loop until when (i) less than of square root of num
		while (i <= (Math.sqrt(num)).toInt)
		{
			// Inner loop
			while (num % i == 0)
			{
				// When (i) is divisible by num
				print("  " + i);
				// Reduce number
				num = (num / i).toInt;
			}
			if (i == 2)
			{
				// When (i) is first prime  then visit on next prime
				i = i + 1;
			}
			else
			{
				i = i + 2;
			}
		}
		if (num > 2)
		{
			// When number is greater than 2 then print num
			print("  " + num + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Factorization = new Factorization();
		// Test Case
		task.primeFactors(136);
		task.primeFactors(760);
		task.primeFactors(22);
		task.primeFactors(23);
	}
}

Output

 Prime Factors of 136 is
  2  2  2  17

 Prime Factors of 760 is
  2  2  2  5  19

 Prime Factors of 22 is
  2  11

 Prime Factors of 23 is
  23
import Foundation
/*
   Swift 4 Program
   Find all prime factors of a number
*/
class Factorization
{
	// Find the prime factors of a given number
	func primeFactors(_ n: Int)
	{
		var num = n;
		print("\n Prime Factors of ", num, " is ");
		var i: Int = 2;
		// Execute loop until when (i) less than of square root of num
		while (i <= Int(sqrt(Double(num))))
		{
			// Inner loop
			while (num % i == 0)
			{
				// When (i) is divisible by num
				print("  ", i, terminator: "");
				// Reduce number
				num = num / i;
			}
			if (i == 2)
			{
				// When (i) is first prime  then visit on next prime
				i = i + 1;
			}
			else
			{
				i = i + 2;
			}
		}
		if (num > 2)
		{
			// When number is greater than 2 then print num
			print("  ", num);
		}
	}
}
func main()
{
	let task: Factorization = Factorization();
	// Test Case
	task.primeFactors(136);
	task.primeFactors(760);
	task.primeFactors(22);
	task.primeFactors(23);
}
main();

Output

 Prime Factors of  136  is
   2   2   2   17

 Prime Factors of  760  is
   2   2   2   5   19

 Prime Factors of  22  is
   2   11

 Prime Factors of  23  is
   23
/*
   Kotlin Program
   Find all prime factors of a number
*/
class Factorization
{
	// Find the prime factors of a given number
	fun primeFactors(n: Int): Unit
	{
      	var num = n;
		print("\n Prime Factors of " + num + " is \n");
		var i: Int = 2;
		// Execute loop until when (i) less than of square root of num
		while (i <= Math.sqrt(num.toDouble()).toInt())
		{
			// Inner loop
			while (num % i == 0)
			{
				// When (i) is divisible by num
				print("  " + i);
				// Reduce number
				num = num / i;
			}
			if (i == 2)
			{
				// When (i) is first prime  then visit on next prime
				i = i + 1;
			}
			else
			{
				i = i + 2;
			}
		}
		if (num > 2)
		{
			// When number is greater than 2 then print num
			print("  " + num + "\n");
		}
	}
}
fun main(args: Array <String> ): Unit
{
	var task: Factorization = Factorization();
	// Test Case
	task.primeFactors(136);
	task.primeFactors(760);
	task.primeFactors(22);
	task.primeFactors(23);
}

Output

 Prime Factors of 136 is
  2  2  2  17

 Prime Factors of 760 is
  2  2  2  5  19

 Prime Factors of 22 is
  2  11

 Prime Factors of 23 is
  23

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







© 2021, kalkicode.com, All rights reserved