Split a number into prime numbers

Here given code implementation process.

// C program for
// Split a number into prime numbers
#include <stdio.h>

// Find all prime numbers which have smaller and equal to given number n
void sieveOfEratosthenes(int prime[], int n)
{
	// Initial two numbers are not prime (0 and 1)
	prime[0] = 0;
	prime[1] = 0;
	// Set the initial (2 to n element is prime)
	for (int i = 2; i <= n; ++i)
	{
		prime[i] = 1;
	}
	// We start to 2
	for (int i = 2; i *i <= n; ++i)
	{
		if (prime[i] == 1)
		{
			// When i is prime number
			// Modify the prime status of all next multiplier of location i
			for (int j = i *i; j <= n; j += i)
			{
				prime[j] = 0;
			}
		}
	}
}
void printData(int result[], int size)
{
	printf("\n");
	for (int i = 0; i < size; ++i)
	{
		printf(" %d", result[i]);
	}
}
void partition(int digits[], int prime[], int result[], 
  			   int index, int position, int k)
{
	if (position == k)
	{
		printData(result, index);
		return;
	}
	int value = 0;
	for (int i = position; i < k; ++i)
	{
		value = (value *10) + digits[i];
		if (prime[value] == 1)
		{
			result[index] = value;
			partition(digits, prime, result, index + 1, i + 1, k);
		}
	}
}
void primeSplitting(int num)
{
	if (num <= 1)
	{
		return;
	}
	int count = 0;
	int temp = num;
	// Count number of digits in prime number
	while (temp > 0)
	{
		temp = temp / 10;
		count++;
	}
	int prime[num + 1];
	int result[count];
	int digits[count];
	int k = count;
	temp = num;
	// Collect digits
	while (temp > 0)
	{
		count--;
		digits[count] = (temp % 10);
		temp = temp / 10;
	}
	sieveOfEratosthenes(prime, num);
	printf("\n Given number : %d", num);
	partition(digits, prime, result, 0, 0, k);
}
int main()
{
	// Test
	primeSplitting(731723);
	return 0;
}

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
/*
    Java program
    Split a number into prime numbers
*/
public class Splitting
{
	// Find all prime numbers which have smaller and equal to given number n
	public void sieveOfEratosthenes(boolean[] prime, int n)
	{
		// Initial two numbers are not prime (0 and 1)
		prime[0] = false;
		prime[1] = false;
		// Set the initial (2 to n element is prime)
		for (int i = 2; i <= n; ++i)
		{
			prime[i] = true;
		}
		// We start to 2
		for (int i = 2; i * i <= n; ++i)
		{
			if (prime[i] == true)
			{
				// When i is prime number
				// Modify the prime status of all next multiplier of location i
				for (int j = i * i; j <= n; j += i)
				{
					prime[j] = false;
				}
			}
		}
	}
	public void printData(int[] result, int size)
	{
		System.out.print("\n");
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + result[i]);
		}
	}
	public void partition(int[] digits, boolean[] prime, 
      int[] result, int index, int position, int k)
	{
		if (position == k)
		{
			printData(result, index);
			return;
		}
		int value = 0;
		for (int i = position; i < k; ++i)
		{
			value = (value * 10) + digits[i];
			if (prime[value] == true)
			{
				// Collect result
				result[index] = value;
				partition(digits, prime, result, index + 1, i + 1, k);
			}
		}
	}
	public void primeSplitting(int num)
	{
		if (num <= 1)
		{
			return;
		}
		int count = 0;
		int temp = num;
		// Count number of digits in prime number
		while (temp > 0)
		{
			temp = temp / 10;
			count++;
		}
		boolean[] prime = new boolean[num + 1];
		int[] result = new int[count];
		int[] digits = new int[count];
		int k = count;
		temp = num;
		// Collect digits
		while (temp > 0)
		{
			count--;
			digits[count] = (temp % 10);
			temp = temp / 10;
		}
		sieveOfEratosthenes(prime, num);
		System.out.print("\n Given number : " + num);
		partition(digits, prime, result, 0, 0, k);
	}
	public static void main(String[] args)
	{
		Splitting task = new Splitting();
		// Test
		task.primeSplitting(731723);
	}
}

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program
    Split a number into prime numbers
*/
class Splitting
{
	public:
		// Find all prime numbers which have smaller and equal to given number n
		void sieveOfEratosthenes(bool prime[], int n)
		{
			// Initial two numbers are not prime (0 and 1)
			prime[0] = false;
			prime[1] = false;
			// Set the initial (2 to n element is prime)
			for (int i = 2; i <= n; ++i)
			{
				prime[i] = true;
			}
			// We start to 2
			for (int i = 2; i *i <= n; ++i)
			{
				if (prime[i] == true)
				{
					// When i is prime number
					// Modify the prime status of all next multiplier of location i
					for (int j = i *i; j <= n; j += i)
					{
						prime[j] = false;
					}
				}
			}
		}
	void printData(int result[], int size)
	{
		cout << "\n";
		for (int i = 0; i < size; ++i)
		{
			cout << " " << result[i];
		}
	}
	void partition(int digits[], bool prime[], int result[], int index, int position, int k)
	{
		if (position == k)
		{
			this->printData(result, index);
			return;
		}
		int value = 0;
		for (int i = position; i < k; ++i)
		{
			value = (value *10) + digits[i];
			if (prime[value] == true)
			{
				// Collect result
				result[index] = value;
				this->partition(digits, prime, result, index + 1, i + 1, k);
			}
		}
	}
	void primeSplitting(int num)
	{
		if (num <= 1)
		{
			return;
		}
		int count = 0;
		int temp = num;
		// Count number of digits in prime number
		while (temp > 0)
		{
			temp = temp / 10;
			count++;
		}
		bool prime[num + 1];
		int result[count];
		int digits[count];
		int k = count;
		temp = num;
		// Collect digits
		while (temp > 0)
		{
			count--;
			digits[count] = (temp % 10);
			temp = temp / 10;
		}
		this->sieveOfEratosthenes(prime, num);
		cout << "\n Given number : " << num;
		this->partition(digits, prime, result, 0, 0, k);
	}
};
int main()
{
	Splitting *task = new Splitting();
	// Test
	task->primeSplitting(731723);
	return 0;
}

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
// Include namespace system
using System;
/*
    Csharp program
    Split a number into prime numbers
*/
public class Splitting
{
	// Find all prime numbers which have smaller and equal to given number n
	public void sieveOfEratosthenes(Boolean[] prime, int n)
	{
		// Initial two numbers are not prime (0 and 1)
		prime[0] = false;
		prime[1] = false;
		// Set the initial (2 to n element is prime)
		for (int i = 2; i <= n; ++i)
		{
			prime[i] = true;
		}
		// We start to 2
		for (int i = 2; i * i <= n; ++i)
		{
			if (prime[i] == true)
			{
				// When i is prime number
				// Modify the prime status of all next multiplier of location i
				for (int j = i * i; j <= n; j += i)
				{
					prime[j] = false;
				}
			}
		}
	}
	public void printData(int[] result, int size)
	{
		Console.Write("\n");
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + result[i]);
		}
	}
	public void partition(int[] digits, Boolean[] prime, 
      int[] result, int index, int position, int k)
	{
		if (position == k)
		{
			this.printData(result, index);
			return;
		}
		int value = 0;
		for (int i = position; i < k; ++i)
		{
			value = (value * 10) + digits[i];
			if (prime[value] == true)
			{
				// Collect result
				result[index] = value;
				this.partition(digits, prime, 
                               result, index + 1, i + 1, k);
			}
		}
	}
	public void primeSplitting(int num)
	{
		if (num <= 1)
		{
			return;
		}
		int count = 0;
		int temp = num;
		// Count number of digits in prime number
		while (temp > 0)
		{
			temp = temp / 10;
			count++;
		}
		Boolean[] prime = new Boolean[num + 1];
		int[] result = new int[count];
		int[] digits = new int[count];
		int k = count;
		temp = num;
		// Collect digits
		while (temp > 0)
		{
			count--;
			digits[count] = (temp % 10);
			temp = temp / 10;
		}
		this.sieveOfEratosthenes(prime, num);
		Console.Write("\n Given number : " + num);
		this.partition(digits, prime, result, 0, 0, k);
	}
	public static void Main(String[] args)
	{
		Splitting task = new Splitting();
		// Test
		task.primeSplitting(731723);
	}
}

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
package main
import "fmt"
/*
    Go program
    Split a number into prime numbers
*/
type Splitting struct {}
func getSplitting() * Splitting {
	var me *Splitting = &Splitting {}
	return me
}
// Find all prime numbers which have smaller and equal to given number n
func(this Splitting) sieveOfEratosthenes(prime[] bool, n int) {
	// Initial two numbers are not prime (0 and 1)
	prime[0] = false
	prime[1] = false
	// We start to 2
	for i := 2 ; i * i <= n ; i++ {
		if prime[i] == true {
			// When i is prime number
			// Modify the prime status of all next multiplier of location i
			for j := i * i ; j <= n ; j += i {
				prime[j] = false
			}
		}
	}
}
func(this Splitting) printData(result[] int, size int) {
	fmt.Print("\n")
	for i := 0 ; i < size ; i++ {
		fmt.Print(" ", result[i])
	}
}
func(this Splitting) partition(digits[] int, prime[] bool, 
	result[] int, index int, position int, k int) {
	if position == k {
		this.printData(result, index)
		return
	}
	var value int = 0
	for i := position ; i < k ; i++ {
		value = (value * 10) + digits[i]
		if prime[value] == true {
			// Collect result
			result[index] = value
			this.partition(digits, prime, result, index + 1, i + 1, k)
		}
	}
}
func(this Splitting) primeSplitting(num int) {
	if num <= 1 {
		return
	}
	var count int = 0
	var temp int = num
	// Count number of digits in prime number
	for (temp > 0) {
		temp = temp / 10
		count++
	}
	var prime = make([] bool, num + 1)
	for i := 0; i <= num; i++ {
		prime[i] = true
	}
	var result = make([] int, count)
	var digits = make([] int, count)
	var k int = count
	temp = num
	// Collect digits
	for (temp > 0) {
		count--
		digits[count] = (temp % 10)
		temp = temp / 10
	}
	this.sieveOfEratosthenes(prime, num)
	fmt.Print("\n Given number : ", num)
	this.partition(digits, prime, result, 0, 0, k)
}
func main() {
	var task * Splitting = getSplitting()
	// Test
	task.primeSplitting(731723)
}

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
<?php
/*
    Php program
    Split a number into prime numbers
*/
class Splitting
{
    // Find all prime numbers which have smaller and equal to given number n
    public  function sieveOfEratosthenes(&$prime, $n)
    {
        // Initial two numbers are not prime (0 and 1)
        $prime[0] = false;
        $prime[1] = false;
        // We start to 2
        for ($i = 2; $i * $i <= $n; ++$i)
        {
            if ($prime[$i] == true)
            {
                // When i is prime number
                // Modify the prime status of all next multiplier of location i
                for ($j = $i * $i; $j <= $n; $j += $i)
                {
                    $prime[$j] = false;
                }
            }
        }
    }
    public  function printData($result, $size)
    {
        echo("\n");
        for ($i = 0; $i < $size; ++$i)
        {
            echo(" ".$result[$i]);
        }
    }
    public  function partition($digits, $prime, 
                                $result, $index, 
                                $position, $k)
    {
        if ($position == $k)
        {
            $this->printData($result, $index);
            return;
        }
        $value = 0;
        for ($i = $position; $i < $k; ++$i)
        {
            $value = ($value * 10) + $digits[$i];
            if ($prime[$value] == true)
            {
                // Collect result
                $result[$index] = $value;
                $this->partition($digits, $prime, 
                                 $result, $index + 1, 
                                 $i + 1, $k);
            }
        }
    }
    public  function primeSplitting($num)
    {
        if ($num <= 1)
        {
            return;
        }
        $count = 0;
        $temp = $num;
        // Count number of digits in prime number
        while ($temp > 0)
        {
            $temp = (int)($temp / 10);
            $count++;
        }
        $prime = array_fill(0, $num + 1, true);
        $result = array_fill(0, $count, 0);
        $digits = array_fill(0, $count, 0);
        $k = $count;
        $temp = $num;
        // Collect digits
        while ($temp > 0)
        {
            $count--;
            $digits[$count] = ($temp % 10);
            $temp = (int)($temp / 10);
        }
        $this->sieveOfEratosthenes($prime, $num);
        echo("\n Given number : ".$num);
        $this->partition($digits, $prime, $result, 0, 0, $k);
    }
}

function main()
{
    $task = new Splitting();
    // Test
    $task->primeSplitting(731723);
}
main();

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
/*
    Node JS program
    Split a number into prime numbers
*/
class Splitting
{
	// Find all prime numbers which have smaller and equal to given number n
	sieveOfEratosthenes(prime, n)
	{
		// Initial two numbers are not prime (0 and 1)
		prime[0] = false;
		prime[1] = false;
		// We start to 2
		for (var i = 2; i * i <= n; ++i)
		{
			if (prime[i] == true)
			{
				// When i is prime number
				// Modify the prime status of all next multiplier of location i
				for (var j = i * i; j <= n; j += i)
				{
					prime[j] = false;
				}
			}
		}
	}
	printData(result, size)
	{
		process.stdout.write("\n");
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
	}
	partition(digits, prime, result, index, position, k)
	{
		if (position == k)
		{
			this.printData(result, index);
			return;
		}
		var value = 0;
		for (var i = position; i < k; ++i)
		{
			value = (value * 10) + digits[i];
			if (prime[value] == true)
			{
				// Collect result
				result[index] = value;
				this.partition(digits, prime, result, index + 1, i + 1, k);
			}
		}
	}
	primeSplitting(num)
	{
		if (num <= 1)
		{
			return;
		}
		var count = 0;
		var temp = num;
		// Count number of digits in prime number
		while (temp > 0)
		{
			temp = parseInt(temp / 10);
			count++;
		}
		var prime = Array(num + 1).fill(true);
		var result = Array(count).fill(0);
		var digits = Array(count).fill(0);
		var k = count;
		temp = num;
		// Collect digits
		while (temp > 0)
		{
			count--;
			digits[count] = (temp % 10);
			temp = parseInt(temp / 10);
		}
		this.sieveOfEratosthenes(prime, num);
		process.stdout.write("\n Given number : " + num);
		this.partition(digits, prime, result, 0, 0, k);
	}
}

function main()
{
	var task = new Splitting();
	// Test
	task.primeSplitting(731723);
}
main();

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
#    Python 3 program
#    Split a number into prime numbers
class Splitting :
	#  Find all prime numbers which have smaller and equal to given number n
	def sieveOfEratosthenes(self, prime, n) :
		#  Initial two numbers are not prime (0 and 1)
		prime[0] = False
		prime[1] = False
		i = 2
		#  We start to 2
		while (i * i <= n) :
			if (prime[i] == True) :
				j = i * i
				#  When i is prime number
				#  Modify the prime status of all next multiplier of location i
				while (j <= n) :
					prime[j] = False
					j += i
				
			
			i += 1
		
	
	def printData(self, result, size) :
		print(end = "\n")
		i = 0
		while (i < size) :
			print(" ", result[i], end = "")
			i += 1
		
	
	def partition(self, digits, prime, result, index, position, k) :
		if (position == k) :
			self.printData(result, index)
			return
		
		value = 0
		i = position
		while (i < k) :
			value = (value * 10) + digits[i]
			if (prime[value] == True) :
				#  Collect result
				result[index] = value
				self.partition(digits, prime, result, index + 1, i + 1, k)
			
			i += 1
		
	
	def primeSplitting(self, num) :
		if (num <= 1) :
			return
		
		count = 0
		temp = num
		#  Count number of digits in prime number
		while (temp > 0) :
			temp = int(temp / 10)
			count += 1
		
		prime = [True] * (num + 1)
		result = [0] * (count)
		digits = [0] * (count)
		k = count
		temp = num
		#  Collect digits
		while (temp > 0) :
			count -= 1
			digits[count] = (temp % 10)
			temp = int(temp / 10)
		
		self.sieveOfEratosthenes(prime, num)
		print("\n Given number : ", num, end = "")
		self.partition(digits, prime, result, 0, 0, k)
	

def main() :
	task = Splitting()
	#  Test
	task.primeSplitting(731723)

if __name__ == "__main__": main()

Output

 Given number :  731723
  7  3  17  2  3
  7  3  17  23
  7  3  1723
  7  31  7  2  3
  7  31  7  23
  7  317  2  3
  7  317  23
  7  31723
  73  17  2  3
  73  17  23
  73  1723
#    Ruby program
#    Split a number into prime numbers
class Splitting 
	#  Find all prime numbers which have smaller and equal to given number n
	def sieveOfEratosthenes(prime, n) 
		#  Initial two numbers are not prime (0 and 1)
		prime[0] = false
		prime[1] = false
		i = 2
		#  We start to 2
		while (i * i <= n) 
			if (prime[i] == true) 
				j = i * i
				#  When i is prime number
				#  Modify the prime status of all next multiplier of location i
				while (j <= n) 
					prime[j] = false
					j += i
				end

			end

			i += 1
		end

	end

	def printData(result, size) 
		print("\n")
		i = 0
		while (i < size) 
			print(" ", result[i])
			i += 1
		end

	end

	def partition(digits, prime, result, index, position, k) 
		if (position == k) 
			self.printData(result, index)
			return
		end

		value = 0
		i = position
		while (i < k) 
			value = (value * 10) + digits[i]
			if (prime[value] == true) 
				#  Collect result
				result[index] = value
				self.partition(digits, prime, 
                               result, index + 1, i + 1, k)
			end

			i += 1
		end

	end

	def primeSplitting(num) 
		if (num <= 1) 
			return
		end

		count = 0
		temp = num
		#  Count number of digits in prime number
		while (temp > 0) 
			temp = temp / 10
			count += 1
		end

		prime = Array.new(num + 1) {true}
		result = Array.new(count) {0}
		digits = Array.new(count) {0}
		k = count
		temp = num
		#  Collect digits
		while (temp > 0) 
			count -= 1
			digits[count] = (temp % 10)
			temp = temp / 10
		end

		self.sieveOfEratosthenes(prime, num)
		print("\n Given number : ", num)
		self.partition(digits, prime, result, 0, 0, k)
	end

end

def main() 
	task = Splitting.new()
	#  Test
	task.primeSplitting(731723)
end

main()

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
/*
    Scala program
    Split a number into prime numbers
*/
class Splitting()
{
	// Find all prime numbers which have smaller and equal to given number n
	def sieveOfEratosthenes(prime: Array[Boolean], n: Int): Unit = {
		// Initial two numbers are not prime (0 and 1)
		prime(0) = false;
		prime(1) = false;
		var i: Int = 2;
		// We start to 2
		while (i * i <= n)
		{
			if (prime(i) == true)
			{
				var j: Int = i * i;
				// When i is prime number
				// Modify the prime status of all next multiplier of location i
				while (j <= n)
				{
					prime(j) = false;
					j += i;
				}
			}
			i += 1;
		}
	}
	def printData(result: Array[Int], size: Int): Unit = {
		print("\n");
		var i: Int = 0;
		while (i < size)
		{
			print(" " + result(i));
			i += 1;
		}
	}
	def partition(digits: Array[Int], prime: Array[Boolean], 
      result: Array[Int], index: Int, 
        position: Int, k: Int): Unit = {
		if (position == k)
		{
			printData(result, index);
			return;
		}
		var value: Int = 0;
		var i: Int = position;
		while (i < k)
		{
			value = (value * 10) + digits(i);
			if (prime(value) == true)
			{
				// Collect result
				result(index) = value;
				partition(digits, prime, result, 
                          index + 1, i + 1, k);
			}
			i += 1;
		}
	}
	def primeSplitting(num: Int): Unit = {
		if (num <= 1)
		{
			return;
		}
		var count: Int = 0;
		var temp: Int = num;
		// Count number of digits in prime number
		while (temp > 0)
		{
			temp = temp / 10;
			count += 1;
		}
		var prime: Array[Boolean] = Array.fill[Boolean](num + 1)(true);
		var result: Array[Int] = Array.fill[Int](count)(0);
		var digits: Array[Int] = Array.fill[Int](count)(0);
		var k: Int = count;
		temp = num;
		// Collect digits
		while (temp > 0)
		{
			count -= 1;
			digits(count) = (temp % 10);
			temp = temp / 10;
		}
		sieveOfEratosthenes(prime, num);
		print("\n Given number : " + num);
		partition(digits, prime, result, 0, 0, k);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Splitting = new Splitting();
		// Test
		task.primeSplitting(731723);
	}
}

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723
/*
    Swift 4 program
    Split a number into prime numbers
*/
class Splitting
{
	// Find all prime numbers which have smaller and equal to given number n
	func sieveOfEratosthenes(_ prime: inout[Bool], _ n: Int)
	{
		// Initial two numbers are not prime (0 and 1)
		prime[0] = false;
		prime[1] = false;
		var i: Int = 2;
		// We start to 2
		while (i * i <= n)
		{
			if (prime[i] == true)
			{
				var j: Int = i * i;
				// When i is prime number
				// Modify the prime status of all next multiplier of location i
				while (j <= n)
				{
					prime[j] = false;
					j += i;
				}
			}
			i += 1;
		}
	}
	func printData(_ result: [Int], _ size: Int)
	{
		print(terminator: "\n");
		var i: Int = 0;
		while (i < size)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
	}
	func partition(_ digits: [Int], _ prime: [Bool], 
      _ result: inout[Int], 
        _ index: Int, 
          _ position: Int, 
            _ k: Int)
	{
		if (position == k)
		{
			self.printData(result, index);
			return;
		}
		var value: Int = 0;
		var i: Int = position;
		while (i < k)
		{
			value = (value * 10) + digits[i];
			if (prime[value] == true)
			{
				// Collect result
				result[index] = value;
				self.partition(digits, prime, &result, 
                               index + 1, i + 1, k);
			}
			i += 1;
		}
	}
	func primeSplitting(_ num: Int)
	{
		if (num <= 1)
		{
			return;
		}
		var count: Int = 0;
		var temp: Int = num;
		// Count number of digits in prime number
		while (temp > 0)
		{
			temp = temp / 10;
			count += 1;
		}
		var prime: [Bool] = Array(repeating: true, count: num + 1);
		var result: [Int] = Array(repeating: 0, count: count);
		var digits: [Int] = Array(repeating: 0, count: count);
		let k: Int = count;
		temp = num;
		// Collect digits
		while (temp > 0)
		{
			count -= 1;
			digits[count] = (temp % 10);
			temp = temp / 10;
		}
		self.sieveOfEratosthenes(&prime, num);
		print("\n Given number : ", num, terminator: "");
		self.partition(digits, prime, &result, 0, 0, k);
	}
}
func main()
{
	let task: Splitting = Splitting();
	// Test
	task.primeSplitting(731723);
}
main();

Output

 Given number :  731723
  7  3  17  2  3
  7  3  17  23
  7  3  1723
  7  31  7  2  3
  7  31  7  23
  7  317  2  3
  7  317  23
  7  31723
  73  17  2  3
  73  17  23
  73  1723
/*
    Kotlin program
    Split a number into prime numbers
*/
class Splitting
{
	// Find all prime numbers which have smaller and equal to given number n
	fun sieveOfEratosthenes(prime: Array < Boolean > , n: Int): Unit
	{
		// Initial two numbers are not prime (0 and 1)
		prime[0] = false;
		prime[1] = false;
		var i: Int = 2;
		// We start to 2
		while (i * i <= n)
		{
			if (prime[i] == true)
			{
				var j: Int = i * i;
				// When i is prime number
				// Modify the prime status of all next multiplier of location i
				while (j <= n)
				{
					prime[j] = false;
					j += i;
				}
			}
			i += 1;
		}
	}
	fun printData(result: Array < Int > , size: Int): Unit
	{
		print("\n");
		var i: Int = 0;
		while (i < size)
		{
			print(" " + result[i]);
			i += 1;
		}
	}
	fun partition(digits: Array < Int > , prime: Array < Boolean > , 
                  result: Array < Int > , index: Int, position: Int, k: Int): Unit
	{
		if (position == k)
		{
			this.printData(result, index);
			return;
		}
		var value: Int = 0;
		var i: Int = position;
		while (i < k)
		{
			value = (value * 10) + digits[i];
			if (prime[value] == true)
			{
				// Collect result
				result[index] = value;
				this.partition(digits, prime, result, index + 1, i + 1, k);
			}
			i += 1;
		}
	}
	fun primeSplitting(num: Int): Unit
	{
		if (num <= 1)
		{
			return;
		}
		var count: Int = 0;
		var temp: Int = num;
		// Count number of digits in prime number
		while (temp > 0)
		{
			temp = temp / 10;
			count += 1;
		}
		var prime: Array < Boolean > = Array(num + 1)
		{
			true
		};
		var result: Array < Int > = Array(count)
		{
			0
		};
		var digits: Array < Int > = Array(count)
		{
			0
		};
		val k: Int = count;
		temp = num;
		// Collect digits
		while (temp > 0)
		{
			count -= 1;
			digits[count] = (temp % 10);
			temp = temp / 10;
		}
		this.sieveOfEratosthenes(prime, num);
		print("\n Given number : " + num);
		this.partition(digits, prime, result, 0, 0, k);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Splitting = Splitting();
	// Test
	task.primeSplitting(731723);
}

Output

 Given number : 731723
 7 3 17 2 3
 7 3 17 23
 7 3 1723
 7 31 7 2 3
 7 31 7 23
 7 317 2 3
 7 317 23
 7 31723
 73 17 2 3
 73 17 23
 73 1723


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