Posted on by Kalkicode
Code Number

Check a number for permutable prime

The given problem is about determining whether a number is a "permutable prime" or not. A permutable prime is a prime number that remains prime when its digits are permuted in any possible order. For example, 37 is a permutable prime because both 37 and 73 are prime numbers.

Problem Statement

The task is to check if a given number is a permutable prime or not. To do this, the code uses a Sieve of Eratosthenes algorithm to generate all prime numbers up to a certain limit and then checks the given number against specific conditions to identify whether it is a permutable prime or not.

Explanation with Example

Let's take the number 37 as an example to illustrate how the code works:

  1. First, it will check if the number is greater than 1 and if it is a prime number using the Sieve of Eratosthenes.
  2. Since 37 is a prime number, it proceeds to the next step.
  3. The code then checks whether the digits of the number are all odd because if there is an even digit (0, 2, 4, 6, or 8), then at least one of its permutations will be divisible by 2 and hence not prime. In this case, 3 and 7 are both odd digits, so it proceeds.
  4. It generates all the possible permutations of the digits of 37 (37 and 73) and checks if both of them are prime using the Sieve of Eratosthenes.

Pseudocode

procedure swap(digit_num[], i, j)
    temp = digit_num[i]
    digit_num[i] = digit_num[j]
    digit_num[j] = temp

function permutation(collection[], digit_num[], size, n)
    if n == size
        num = 0
        for i = 0 to size
            num = num * 10 + digit_num[i]
        if collection[num] == 1
            return 1
        else
            return 0
    if n < size
        for i = n to size
            swap(digit_num, i, n)
            if not permutation(collection, digit_num, size, n + 1)
                return 0
            swap(digit_num, i, n)
        return 1

function is_permutable_prime(collection[], num)
    status = 0
    if num > 1 and collection[num] == 1
        status = 1
        digits = 0
        temp = num
        while temp != 0
            digits++
            temp = temp / 10
        if digits > 1
            create array digit_num[digits]
            counter = 0
            temp = num
            while temp != 0 and status == 1
                single_digit = temp % 10
                if single_digit is even
                    status = 0
                else
                    digit_num[counter] = single_digit
                    temp = temp / 10
                    counter++
            if status == 1
                status = permutation(collection, digit_num, digits, 0)
    if status == 1
        print num, "Is Permutable Prime"
    else
        print num, "Is Not Permutable Prime"

function sieve_of_eratosthenes(collection[])
    i = 0
    j = 1
    collection[i] = 0
    collection[j] = 0
    for i = 2 to SIZE
        collection[i] = 1
    for i = 2 to sqrt(SIZE)
        if collection[i] == 1
            for j = i * i to SIZE step i
                collection[j] = 0

function main()
    create array collection[SIZE]
    sieve_of_eratosthenes(collection)
    is_permutable_prime(collection, 7)
    is_permutable_prime(collection, 107)
    is_permutable_prime(collection, 13)
    is_permutable_prime(collection, 1319)
    is_permutable_prime(collection, 1531)
    is_permutable_prime(collection, 373)
    is_permutable_prime(collection, 991)

Code Solution

Here given code implementation process.

// C program
// Check a number for permutable prime
#include <stdio.h>
#define SIZE 1000001

//Swap two elements 
void swap(int digit_num[], int i, int j)
{
	int temp = digit_num[i];
	digit_num[i] = digit_num[j];
	digit_num[j] = temp;
}
// Perform permutation of digitsnumber and 
// Check whether its is a prime or not
int permutation(int collection[], int digit_num[], int size, int n)
{
	if (n == size)
	{
        
		int num = 0;
		for (int i = 0; i < size; ++i)
		{
			num = num * 10 + digit_num[i];
		}
		if (collection[num] == 1)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	if (n < size)
	{
		for (int i = n; i < size; ++i)
		{
			//swap the number digit
			swap(digit_num, i, n);
			if (!permutation(collection, digit_num, size, n + 1))
			{
				return 0;
			}
			//swap the number digit
			swap(digit_num, i, n);
		}
	}
	return 1;
}
//Check whether given number is permutable prime or not
void is_permutable_prime(int collection[], int num)
{
	//indicating the status of result prime number
	int status = 0;
	//Check that given number is greater than one and number is prime
	if (num > 1 && collection[num] == 1)
	{
		//Active status
		status = 1;
		int digits = 0;
		int temp = num;
		//Count the number of digits in given number
		while (temp != 0)
		{
			digits++;
			temp /= 10;
		}
		if (digits > 1)
		{
			int digit_num[digits];
			int counter = 0;
			int single_digit = 0;
			temp = num;
			// Get digits of a number element
			// digit sequence is not important
			while (temp != 0 && status == 1)
			{
				single_digit = temp % 10;
				if (single_digit % 2 == 0)
				{
					// When digit is divisible by 2 
					// that means in permutation last digit is divisible by 2 
					// [When single digit is 0 2 4 6 8]
					status = 0;
				}
				else
				{
					digit_num[counter] = single_digit;
					temp /= 10;
					counter++;
				}
			}
			if (status == 1)
			{
				//final test
				status = permutation(collection, digit_num, digits, 0);
			}
		}
	}
	if (status == 1)
	{
		printf("\n %d Is Permutable Prime", num);
	}
	else
	{
		printf("\n %d Is Not Permutable Prime", num);
	}
}
//Find all prime numbers under 1000001 
void sieve_of_eratosthenes(int collection[])
{
	// Loop controlling variables
	int i = 0;
	int j = 1;
	// Initial two numbers are not prime (0 and 1)
	collection[i] = 0;
	collection[j] = 0;
	// Set the initial (2 to n element is prime)
	for (i = 2; i < SIZE; ++i)
	{
		collection[i] = 1;
	}
	// Initial 0 and 1 are not prime
	// We start to 2
	for (i = 2; i * i < SIZE; ++i)
	{
		if (collection[i] == 1)
		{
			//When i is prime number
			//Modify the prime status of all next multiplier of location i
			for (j = i * i; j < SIZE; j += i)
			{
				collection[j] = 0;
			}
		}
	}
}
int main()
{
	//This is used to store prime number status
	int collection[SIZE];
	//Find find prime number
	sieve_of_eratosthenes(collection);
	//Test case
	is_permutable_prime(collection, 7);
	is_permutable_prime(collection, 107);
	is_permutable_prime(collection, 13);
	is_permutable_prime(collection, 1319);
	is_permutable_prime(collection, 1531);
	is_permutable_prime(collection, 373);
	is_permutable_prime(collection, 991);
	return 0;
}

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
// Java porgram 
// Check a number for permutable prime
class PermutablePrime
{
	//Swap two elements 
	public void swap(int[] digit_num, int i, int j)
	{
		int temp = digit_num[i];
		digit_num[i] = digit_num[j];
		digit_num[j] = temp;
	}
	// Perform permutation of digitsnumber and 
	// Check whether its is a prime or not
	public boolean permutation(boolean[] collection, int[] digit_num, int size, int n)
	{
		if (n == size)
		{
			int num = 0;
			for (int i = 0; i < size; ++i)
			{
				num = num *10 + digit_num[i];
			}
			if (collection[num] == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		if (n < size)
		{
			for (int i = n; i < size; ++i)
			{
				//swap the number digit
				swap(digit_num, i, n);
				if (!permutation(collection, digit_num, size, n + 1))
				{
					return false;
				}
				//swap the number digit
				swap(digit_num, i, n);
			}
		}
		return true;
	}
	//Check whether given number is permutable prime or not
	public void is_permutable_prime(boolean[] collection, int num)
	{
		//indicating the status of result prime number
		boolean status = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			//Active status
			status = true;
			int digits = 0;
			int temp = num;
			//Count the number of digits in given number
			while (temp != 0)
			{
				digits++;
				temp /= 10;
			}
			if (digits > 1)
			{
				int[] digit_num = new int[digits];
				int counter = 0;
				int single_digit = 0;
				temp = num;
				// Get digits of a number element
				// digit sequence is not important
				while (temp != 0 && status == true)
				{
					single_digit = temp % 10;
					if (single_digit % 2 == 0)
					{
						// When digit is divisible by 2 
						// that means in permutation last digit is divisible by 2 
						// [When single digit is 0 2 4 6 8]
						status = false;
					}
					else
					{
						digit_num[counter] = single_digit;
						temp /= 10;
						counter++;
					}
				}
				if (status == true)
				{
					//final test
					status = permutation(collection, digit_num, digits, 0);
				}
			}
		}
		if (status == true)
		{
			System.out.print("\n " + num +" Is Permutable Prime");
		}
		else
		{
			System.out.print("\n " + num + " Is Not Permutable Prime");
		}
	}
	//Find all prime numbers under 1000001 
	public void sieve_of_eratosthenes(boolean[] collection, int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Set the initial (2 to n element is prime)
		for (i = 2; i < size; ++i)
		{
			collection[i] = true;
		}
		// Initial 0 and 1 are not prime
		// We start to 2
		for (i = 2; i *i < size; ++i)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				for (j = i *i; j < size; j += i)
				{
					collection[j] = false;
				}
			}
		}
	}
	public static void main(String args[])
	{
		PermutablePrime obj = new PermutablePrime();
		int size = 1000001;
		//This is used to store prime number status
		boolean[] collection = new boolean[size];
		//Find find prime number
		obj.sieve_of_eratosthenes(collection, size);
		//Test case
		obj.is_permutable_prime(collection, 7);
		obj.is_permutable_prime(collection, 107);
		obj.is_permutable_prime(collection, 13);
		obj.is_permutable_prime(collection, 1319);
		obj.is_permutable_prime(collection, 1531);
		obj.is_permutable_prime(collection, 373);
		obj.is_permutable_prime(collection, 991);
	}
}

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
//Include header file
#include <iostream>
using namespace std;

// C++ porgram 
// Check a number for permutable prime
class PermutablePrime
{
	public:
		//Swap two elements 
		void swap(int digit_num[], int i, int j)
		{
			int temp = digit_num[i];
			digit_num[i] = digit_num[j];
			digit_num[j] = temp;
		}
	// Perform permutation of digitsnumber and 
	// Check whether its is a prime or not
	bool permutation(bool collection[], int digit_num[], int size, int n)
	{
		if (n == size)
		{
			int num = 0;
			for (int i = 0; i < size; ++i)
			{
				num = num *10 + digit_num[i];
			}
			if (collection[num] == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		if (n < size)
		{
			for (int i = n; i < size; ++i)
			{
				//swap the number digit
				this->swap(digit_num, i, n);
				if (!this->permutation(collection, digit_num, size, n + 1))
				{
					return false;
				}
				//swap the number digit
				this->swap(digit_num, i, n);
			}
		}
		return true;
	}
	//Check whether given number is permutable prime or not
	void is_permutable_prime(bool collection[], int num)
	{
		//indicating the status of result prime number
		bool status = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			//Active status
			status = true;
			int digits = 0;
			int temp = num;
			//Count the number of digits in given number
			while (temp != 0)
			{
				digits++;
				temp /= 10;
			}
			if (digits > 1)
			{
				int digit_num[digits] ;
				int counter = 0;
				int single_digit = 0;
				temp = num;
				// Get digits of a number element
				// digit sequence is not important
				while (temp != 0 && status == true)
				{
					single_digit = temp % 10;
					if (single_digit % 2 == 0)
					{
						// When digit is divisible by 2 
						// that means in permutation last digit is divisible by 2 
						// [When single digit is 0 2 4 6 8]
						status = false;
					}
					else
					{
						digit_num[counter] = single_digit;
						temp /= 10;
						counter++;
					}
				}
				if (status == true)
				{
					//final test
					status = this->permutation(collection, digit_num, digits, 0);
				}
			}
		}
		if (status == true)
		{
			cout << "\n " << num << " Is Permutable Prime";
		}
		else
		{
			cout << "\n " << num << " Is Not Permutable Prime";
		}
	}
	//Find all prime numbers under 1000001 
	void sieve_of_eratosthenes(bool collection[], int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Set the initial (2 to n element is prime)
		for (i = 2; i < size; ++i)
		{
			collection[i] = true;
		}
		// Initial 0 and 1 are not prime
		// We start to 2
		for (i = 2; i *i < size; ++i)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				for (j = i *i; j < size; j += i)
				{
					collection[j] = false;
				}
			}
		}
	}
};
int main()
{
	PermutablePrime obj = PermutablePrime();
	int size = 1000001;
	//This is used to store prime number status
	bool collection[size] ;
	//Find find prime number
	obj.sieve_of_eratosthenes(collection, size);
	//Test case
	obj.is_permutable_prime(collection, 7);
	obj.is_permutable_prime(collection, 107);
	obj.is_permutable_prime(collection, 13);
	obj.is_permutable_prime(collection, 1319);
	obj.is_permutable_prime(collection, 1531);
	obj.is_permutable_prime(collection, 373);
	obj.is_permutable_prime(collection, 991);
	return 0;
}

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
//Include namespace system
using System;

// C# porgram 
// Check a number for permutable prime

class PermutablePrime
{
	//Swap two elements 
	public void swap(int[] digit_num, int i, int j)
	{
		int temp = digit_num[i];
		digit_num[i] = digit_num[j];
		digit_num[j] = temp;
	}
	// Perform permutation of digitsnumber and 
	// Check whether its is a prime or not
	public Boolean permutation(Boolean[] collection, int[] digit_num, int size, int n)
	{
		if (n == size)
		{
			int num = 0;
			for (int i = 0; i < size; ++i)
			{
				num = num * 10 + digit_num[i];
			}
			if (collection[num] == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		if (n < size)
		{
			for (int i = n; i < size; ++i)
			{
				//swap the number digit
				swap(digit_num, i, n);
				if (!permutation(collection, digit_num, size, n + 1))
				{
					return false;
				}
				//swap the number digit
				swap(digit_num, i, n);
			}
		}
		return true;
	}
	//Check whether given number is permutable prime or not
	public void is_permutable_prime(Boolean[] collection, int num)
	{
		//indicating the status of result prime number
		Boolean status = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			//Active status
			status = true;
			int digits = 0;
			int temp = num;
			//Count the number of digits in given number
			while (temp != 0)
			{
				digits++;
				temp /= 10;
			}
			if (digits > 1)
			{
				int[] digit_num = new int[digits];
				int counter = 0;
				int single_digit = 0;
				temp = num;
				// Get digits of a number element
				// digit sequence is not important
				while (temp != 0 && status == true)
				{
					single_digit = temp % 10;
					if (single_digit % 2 == 0)
					{
						// When digit is divisible by 2 
						// that means in permutation last digit is divisible by 2 
						// [When single digit is 0 2 4 6 8]
						status = false;
					}
					else
					{
						digit_num[counter] = single_digit;
						temp /= 10;
						counter++;
					}
				}
				if (status == true)
				{
					//final test
					status = permutation(collection, digit_num, digits, 0);
				}
			}
		}
		if (status == true)
		{
			Console.Write("\n " + num + " Is Permutable Prime");
		}
		else
		{
			Console.Write("\n " + num + " Is Not Permutable Prime");
		}
	}
	//Find all prime numbers under 1000001 
	public void sieve_of_eratosthenes(Boolean[] collection, int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Set the initial (2 to n element is prime)
		for (i = 2; i < size; ++i)
		{
			collection[i] = true;
		}
		// Initial 0 and 1 are not prime
		// We start to 2
		for (i = 2; i * i < size; ++i)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				for (j = i * i; j < size; j += i)
				{
					collection[j] = false;
				}
			}
		}
	}
	public static void Main(String []args)
	{
		PermutablePrime obj = new PermutablePrime();
		int size = 1000001;
		//This is used to store prime number status
		Boolean[] collection = new Boolean[size];
		//Find find prime number
		obj.sieve_of_eratosthenes(collection, size);
		//Test case
		obj.is_permutable_prime(collection, 7);
		obj.is_permutable_prime(collection, 107);
		obj.is_permutable_prime(collection, 13);
		obj.is_permutable_prime(collection, 1319);
		obj.is_permutable_prime(collection, 1531);
		obj.is_permutable_prime(collection, 373);
		obj.is_permutable_prime(collection, 991);
	}
}

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
<?php
// Php porgram 
// Check a number for permutable prime
class PermutablePrime
{
    //Swap two elements 
    public  function swap( & $digit_num, $i, $j)
    {
        $temp = $digit_num[$i];
        $digit_num[$i] = $digit_num[$j];
        $digit_num[$j] = $temp;
    }
    // Perform permutation of digitsnumber and 
    // Check whether its is a prime or not
    public  function permutation( & $collection, & $digit_num, $size, $n)
    {
        if ($n == $size)
        {
            $num = 0;
            for ($i = 0; $i < $size; ++$i)
            {
                $num = $num * 10 + $digit_num[$i];
            }
            if ($collection[$num] == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        if ($n < $size)
        {
            for ($i = $n; $i < $size; ++$i)
            {
                //swap the number digit
                $this->swap($digit_num, $i, $n);
                if (!$this->permutation($collection, $digit_num, $size, $n + 1))
                {
                    return false;
                }
                //swap the number digit
                $this->swap($digit_num, $i, $n);
            }
        }
        return true;
    }
    //Check whether given number is permutable prime or not
    public  function is_permutable_prime( & $collection, $num)
    {
        //indicating the status of result prime number
        $status = false;
        //Check that given number is greater than one and number is prime
        if ($num > 1 && $collection[$num] == true)
        {
            //Active status
            $status = true;
            $digits = 0;
            $temp = $num;
            //Count the number of digits in given number
            while ($temp != 0)
            {
                $digits++;
                $temp = intval($temp / 10);
            }
            if ($digits > 1)
            {
                $digit_num = array_fill(0, $digits, 0);
                $counter = 0;
                $single_digit = 0;
                $temp = $num;
                // Get digits of a number element
                // digit sequence is not important
                while ($temp != 0 && $status == true)
                {
                    $single_digit = $temp % 10;
                    if ($single_digit % 2 == 0)
                    {
                        // When digit is divisible by 2 
                        // that means in permutation last digit is divisible by 2 
                        // [When single digit is 0 2 4 6 8]
                        $status = false;
                    }
                    else
                    {
                        $digit_num[$counter] = $single_digit;
                        $temp = intval($temp / 10);
                        $counter++;
                    }
                }
                if ($status == true)
                {
                    //final test
                    $status = $this->permutation($collection, $digit_num, $digits, 0);
                }
            }
        }
        if ($status == true)
        {
            echo "\n ". $num ." Is Permutable Prime";
        }
        else
        {
            echo "\n ". $num ." Is Not Permutable Prime";
        }
    }
    //Find all prime numbers under 1000001 
    public  function sieve_of_eratosthenes( & $collection, $size)
    {
        // Loop controlling variables
        $i = 0;
        $j = 1;
        // Initial two numbers are not prime (0 and 1)
        $collection[$i] = false;
        $collection[$j] = false;
        // Set the initial (2 to n element is prime)
        for ($i = 2; $i < $size; ++$i)
        {
            $collection[$i] = true;
        }
        // Initial 0 and 1 are not prime
        // We start to 2
        for ($i = 2; $i * $i < $size; ++$i)
        {
            if ($collection[$i] == true)
            {
                //When i is prime number
                //Modify the prime status of all next multiplier of location i
                for ($j = $i * $i; $j < $size; $j += $i)
                {
                    $collection[$j] = false;
                }
            }
        }
    }
}

function main()
{
    $obj = new PermutablePrime();
    $size = 1000001;
    //This is used to store prime number status
    $collection = array_fill(0, $size, false);
    //Find find prime number
    $obj->sieve_of_eratosthenes($collection, $size);
    //Test case
    $obj->is_permutable_prime($collection, 7);
    $obj->is_permutable_prime($collection, 107);
    $obj->is_permutable_prime($collection, 13);
    $obj->is_permutable_prime($collection, 1319);
    $obj->is_permutable_prime($collection, 1531);
    $obj->is_permutable_prime($collection, 373);
    $obj->is_permutable_prime($collection, 991);
}
main();

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
// Node Js porgram 
// Check a number for permutable prime
class PermutablePrime
{
	//Swap two elements 
	swap(digit_num, i, j)
	{
		var temp = digit_num[i];
		digit_num[i] = digit_num[j];
		digit_num[j] = temp;
	}
	// Perform permutation of digitsnumber and 
	// Check whether its is a prime or not
	permutation(collection, digit_num, size, n)
	{
		if (n == size)
		{
			var num = 0;
			for (var i = 0; i < size; ++i)
			{
				num = num * 10 + digit_num[i];
			}
			if (collection[num] == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		if (n < size)
		{
			for (var i = n; i < size; ++i)
			{
				//swap the number digit
				this.swap(digit_num, i, n);
				if (!this.permutation(collection, digit_num, size, n + 1))
				{
					return false;
				}
				//swap the number digit
				this.swap(digit_num, i, n);
			}
		}
		return true;
	}
	//Check whether given number is permutable prime or not
	is_permutable_prime(collection, num)
	{
		//indicating the status of result prime number
		var status = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			//Active status
			status = true;
			var digits = 0;
			var temp = num;
			//Count the number of digits in given number
			while (temp != 0)
			{
				digits++;
				temp = parseInt(temp / 10);
			}
			if (digits > 1)
			{
				var digit_num = Array(digits).fill(0);
				var counter = 0;
				var single_digit = 0;
				temp = num;
				// Get digits of a number element
				// digit sequence is not important
				while (temp != 0 && status == true)
				{
					single_digit = temp % 10;
					if (single_digit % 2 == 0)
					{
						// When digit is divisible by 2 
						// that means in permutation last digit is divisible by 2 
						// [When single digit is 0 2 4 6 8]
						status = false;
					}
					else
					{
						digit_num[counter] = single_digit;
						temp = parseInt(temp / 10);
						counter++;
					}
				}
				if (status == true)
				{
					//final test
					status = this.permutation(collection, digit_num, digits, 0);
				}
			}
		}
		if (status == true)
		{
			process.stdout.write("\n " + num + " Is Permutable Prime");
		}
		else
		{
			process.stdout.write("\n " + num + " Is Not Permutable Prime");
		}
	}
	//Find all prime numbers under 1000001 
	sieve_of_eratosthenes(collection, size)
	{
		// Loop controlling variables
		var i = 0;
		var j = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Set the initial (2 to n element is prime)
		for (i = 2; i < size; ++i)
		{
			collection[i] = true;
		}
		// Initial 0 and 1 are not prime
		// We start to 2
		for (i = 2; i * i < size; ++i)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				for (j = i * i; j < size; j += i)
				{
					collection[j] = false;
				}
			}
		}
	}
}

function main()
{
	var obj = new PermutablePrime();
	var size = 1000001;
	//This is used to store prime number status
	var collection = Array(size).fill(false);
	//Find find prime number
	obj.sieve_of_eratosthenes(collection, size);
	//Test case
	obj.is_permutable_prime(collection, 7);
	obj.is_permutable_prime(collection, 107);
	obj.is_permutable_prime(collection, 13);
	obj.is_permutable_prime(collection, 1319);
	obj.is_permutable_prime(collection, 1531);
	obj.is_permutable_prime(collection, 373);
	obj.is_permutable_prime(collection, 991);
}
main();

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
#  Python 3 porgram 
#  Check a number for permutable prime
class PermutablePrime :
	# Swap two elements 
	def swap(self, digit_num, i, j) :
		temp = digit_num[i]
		digit_num[i] = digit_num[j]
		digit_num[j] = temp
	
	#  Perform permutation of digitsnumber and 
	#  Check whether its is a prime or not
	def permutation(self, collection, digit_num, size, n) :
		if (n == size) :
			num = 0
			i = 0
			while (i < size) :
				num = num * 10 + digit_num[i]
				i += 1
			
			if (collection[num] == True) :
				return True
			else :
				return False
			
		
		if (n < size) :
			i = n
			while (i < size) :
				# swap the number digit
				self.swap(digit_num, i, n)
				if (not self.permutation(collection, digit_num, size, n + 1)) :
					return False
				
				# swap the number digit
				self.swap(digit_num, i, n)
				i += 1
			
		
		return True
	
	# Check whether given number is permutable prime or not
	def is_permutable_prime(self, collection, num) :
		# indicating the status of result prime number
		status = False
		# Check that given number is greater than one and number is prime
		if (num > 1 and collection[num] == True) :
			# Active status
			status = True
			digits = 0
			temp = num
			# Count the number of digits in given number
			while (temp != 0) :
				digits += 1
				temp = int(temp / 10)
			
			if (digits > 1) :
				digit_num = [0] * (digits)
				counter = 0
				single_digit = 0
				temp = num
				#  Get digits of a number element
				#  digit sequence is not important
				while (temp != 0 and status == True) :
					single_digit = temp % 10
					if (single_digit % 2 == 0) :
						#  When digit is divisible by 2 
						#  that means in permutation last digit is divisible by 2 
						#  [When single digit is 0 2 4 6 8]
						status = False
					else :
						digit_num[counter] = single_digit
						temp = int(temp / 10)
						counter += 1
					
				
				if (status == True) :
					# final test
					status = self.permutation(collection, digit_num, digits, 0)
				
			
		
		if (status == True) :
			print("\n ", num ," Is Permutable Prime", end = "")
		else :
			print("\n ", num ," Is Not Permutable Prime", end = "")
		
	
	# Find all prime numbers under 1000001 
	def sieve_of_eratosthenes(self, collection, size) :
		#  Loop controlling variables
		i = 0
		j = 1
		#  Initial two numbers are not prime (0 and 1)
		collection[i] = False
		collection[j] = False
		#  Initial 0 and 1 are not prime
		#  We start to 2
		i = 2
		while (i * i < size) :
			if (collection[i] == True) :
				# When i is prime number
				# Modify the prime status of all next multiplier of location i
				j = i * i
				while (j < size) :
					collection[j] = False
					j += i
				
			
			i += 1
		
	

def main() :
	obj = PermutablePrime()
	size = 1000001
	# This is used to store prime number status
	collection = [True] * (size)
	# Find find prime number
	obj.sieve_of_eratosthenes(collection, size)
	# Test case
	obj.is_permutable_prime(collection, 7)
	obj.is_permutable_prime(collection, 107)
	obj.is_permutable_prime(collection, 13)
	obj.is_permutable_prime(collection, 1319)
	obj.is_permutable_prime(collection, 1531)
	obj.is_permutable_prime(collection, 373)
	obj.is_permutable_prime(collection, 991)

if __name__ == "__main__": main()

Output

  7  Is Permutable Prime
  107  Is Not Permutable Prime
  13  Is Permutable Prime
  1319  Is Not Permutable Prime
  1531  Is Not Permutable Prime
  373  Is Permutable Prime
  991  Is Permutable Prime
#  Ruby porgram 
#  Check a number for permutable prime
class PermutablePrime 
	# Swap two elements 
	def swap(digit_num, i, j) 
		temp = digit_num[i]
		digit_num[i] = digit_num[j]
		digit_num[j] = temp
	end

	#  Perform permutation of digitsnumber and 
	#  Check whether its is a prime or not
	def permutation(collection, digit_num, size, n) 
		if (n == size) 
			num = 0
			i = 0
			while (i < size) 
				num = num * 10 + digit_num[i]
				i += 1
			end

			if (collection[num] == true) 
				return true
			else 
				return false
			end

		end

		if (n < size) 
			i = n
			while (i < size) 
				# swap the number digit
				self.swap(digit_num, i, n)
				if (!self.permutation(collection, digit_num, size, n + 1)) 
					return false
				end

				# swap the number digit
				self.swap(digit_num, i, n)
				i += 1
			end

		end

		return true
	end

	# Check whether given number is permutable prime or not
	def is_permutable_prime(collection, num) 
		# indicating the status of result prime number
		status = false
		# Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true) 
			# Active status
			status = true
			digits = 0
			temp = num
			# Count the number of digits in given number
			while (temp != 0) 
				digits += 1
				temp /= 10
			end

			if (digits > 1) 
				digit_num = Array.new(digits) {0}
				counter = 0
				single_digit = 0
				temp = num
				#  Get digits of a number element
				#  digit sequence is not important
				while (temp != 0 && status == true) 
					single_digit = temp % 10
					if (single_digit % 2 == 0) 
						#  When digit is divisible by 2 
						#  that means in permutation last digit is divisible by 2 
						#  [When single digit is 0 2 4 6 8]
						status = false
					else 
						digit_num[counter] = single_digit
						temp /= 10
						counter += 1
					end

				end

				if (status == true) 
					# final test
					status = self.permutation(collection, digit_num, digits, 0)
				end

			end

		end

		if (status == true) 
			print("\n ", num ," Is Permutable Prime")
		else 
			print("\n ", num ," Is Not Permutable Prime")
		end

	end

	# Find all prime numbers under 1000001 
	def sieve_of_eratosthenes(collection, size) 
		#  Loop controlling variables
		i = 0
		j = 1
		#  Initial two numbers are not prime (0 and 1)
		collection[i] = false
		collection[j] = false
		#  Initial 0 and 1 are not prime
		#  We start to 2
		i = 2
		while (i * i < size) 
			if (collection[i] == true) 
				# When i is prime number
				# Modify the prime status of all next multiplier of location i
				j = i * i
				while (j < size) 
					collection[j] = false
					j += i
				end

			end

			i += 1
		end

	end

end

def main() 
	obj = PermutablePrime.new()
	size = 1000001
	# This is used to store prime number status
	collection = Array.new(size) {true}
	# Find find prime number
	obj.sieve_of_eratosthenes(collection, size)
	# Test case
	obj.is_permutable_prime(collection, 7)
	obj.is_permutable_prime(collection, 107)
	obj.is_permutable_prime(collection, 13)
	obj.is_permutable_prime(collection, 1319)
	obj.is_permutable_prime(collection, 1531)
	obj.is_permutable_prime(collection, 373)
	obj.is_permutable_prime(collection, 991)
end

main()

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
// Scala porgram 
// Check a number for permutable prime
class PermutablePrime
{
	//Swap two elements 
	def swap(digit_num: Array[Int], i: Int, j: Int): Unit = {
		var temp: Int = digit_num(i);
		digit_num(i) = digit_num(j);
		digit_num(j) = temp;
	}
	// Perform permutation of digitsnumber and 
	// Check whether its is a prime or not
	def permutation(collection: Array[Boolean], digit_num: Array[Int], size: Int, n: Int): Boolean = {
      	var i: Int = 0;
		if (n == size)
		{
			var num: Int = 0;
			while (i < size)
			{
				num = num * 10 + digit_num(i);
				i += 1;
			}
			if (collection(num) == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		if (n < size)
		{
			i = n;
			while (i < size)
			{
				//swap the number digit
				swap(digit_num, i, n);
				if (!permutation(collection, digit_num, size, n + 1))
				{
					return false;
				}
				//swap the number digit
				swap(digit_num, i, n);
				i += 1;
			}
		}
		return true;
	}
	//Check whether given number is permutable prime or not
	def is_permutable_prime(collection: Array[Boolean], num: Int): Unit = {
		//indicating the status of result prime number
		var status: Boolean = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection(num) == true)
		{
			//Active status
			status = true;
			var digits: Int = 0;
			var temp: Int = num;
			//Count the number of digits in given number
			while (temp != 0)
			{
				digits += 1;
				temp = (temp / 10).toInt;
			}
			if (digits > 1)
			{
				var digit_num: Array[Int] = Array.fill[Int](digits)(0);
				var counter: Int = 0;
				var single_digit: Int = 0;
				temp = num;
				// Get digits of a number element
				// digit sequence is not important
				while (temp != 0 && status == true)
				{
					single_digit = temp % 10;
					if (single_digit % 2 == 0)
					{
						// When digit is divisible by 2 
						// that means in permutation last digit is divisible by 2 
						// [When single digit is 0 2 4 6 8]
						status = false;
					}
					else
					{
						digit_num(counter) = single_digit;
						temp = (temp / 10).toInt;
						counter += 1;
					}
				}
				if (status == true)
				{
					//final test
					status = permutation(collection, digit_num, digits, 0);
				}
			}
		}
		if (status == true)
		{
			print("\n " + num + " Is Permutable Prime");
		}
		else
		{
			print("\n " + num + " Is Not Permutable Prime");
		}
	}
	//Find all prime numbers under 1000001 
	def sieve_of_eratosthenes(collection: Array[Boolean], size: Int): Unit = {
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 1;
		// Initial two numbers are not prime (0 and 1)
		collection(i) = false;
		collection(j) = false;
		// Initial 0 and 1 are not prime
		// We start to 2
		i = 2;
		while (i * i < size)
		{
			if (collection(i) == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				j = i * i;
				while (j < size)
				{
					collection(j) = false;
					j += i;
				}
			}
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: PermutablePrime = new PermutablePrime();
		var size: Int = 1000001;
		//This is used to store prime number status
		var collection: Array[Boolean] = Array.fill[Boolean](size)(true);
		//Find find prime number
		obj.sieve_of_eratosthenes(collection, size);
		//Test case
		obj.is_permutable_prime(collection, 7);
		obj.is_permutable_prime(collection, 107);
		obj.is_permutable_prime(collection, 13);
		obj.is_permutable_prime(collection, 1319);
		obj.is_permutable_prime(collection, 1531);
		obj.is_permutable_prime(collection, 373);
		obj.is_permutable_prime(collection, 991);
	}
}

Output

 7 Is Permutable Prime
 107 Is Not Permutable Prime
 13 Is Permutable Prime
 1319 Is Not Permutable Prime
 1531 Is Not Permutable Prime
 373 Is Permutable Prime
 991 Is Permutable Prime
// Swift 4 porgram 
// Check a number for permutable prime
class PermutablePrime
{
	//Swap two elements 
	func swap(_ digit_num: inout[Int], _ i: Int, _ j: Int)
	{
		let temp: Int = digit_num[i];
		digit_num[i] = digit_num[j];
		digit_num[j] = temp;
	}
	// Perform permutation of digitsnumber and 
	// Check whether its is a prime or not
	func permutation(_ collection: [Bool], _ digit_num: inout[Int], _ size: Int, _ n: Int) -> Bool
	{
      	var i: Int = 0;
		if (n == size)
		{
			var num: Int = 0;
			
			while (i < size)
			{
				num = num * 10 + digit_num[i];
				i += 1;
			}
			if (collection[num] == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		if (n < size)
		{
			i = n;
			while (i < size)
			{
				//swap the number digit
				self.swap(&digit_num, i, n);
				if (!self.permutation(collection, &digit_num, size, n + 1))
				{
					return false;
				}
				//swap the number digit
				self.swap(&digit_num, i, n);
				i += 1;
			}
		}
		return true;
	}
	//Check whether given number is permutable prime or not
	func is_permutable_prime(_ collection: [Bool], _ num: Int)
	{
		//indicating the status of result prime number
		var status: Bool = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			//Active status
			status = true;
			var digits: Int = 0;
			var temp: Int = num;
			//Count the number of digits in given number
			while (temp != 0)
			{
				digits += 1;
				temp /= 10;
			}
			if (digits > 1)
			{
				var digit_num: [Int] = Array(repeating: 0, count: digits);
				var counter: Int = 0;
				var single_digit: Int = 0;
				temp = num;
				// Get digits of a number element
				// digit sequence is not important
				while (temp != 0 && status == true)
				{
			
					single_digit = temp % 10;
					if (single_digit % 2 == 0)
					{
						// When digit is divisible by 2 
						// that means in permutation last digit is divisible by 2 
						// [When single digit is 0 2 4 6 8]
						status = false;
					}
					else
					{
						digit_num[counter] = single_digit;
						temp /= 10;
						counter += 1;
					}
				}
				if (status == true)
				{
					//final test
					status = self.permutation(collection, &digit_num, digits, 0);
				}
			}
		}
		if (status == true)
		{
			print("\n ", num ," Is Permutable Prime", terminator: "");
		}
		else
		{
			print("\n ", num ," Is Not Permutable Prime", terminator: "");
		}
	}
	//Find all prime numbers under 1000001 
	func sieve_of_eratosthenes(_ collection: inout[Bool], _ size: Int)
	{
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Initial 0 and 1 are not prime
		// We start to 2
		i = 2;
		while (i * i < size)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				j = i * i;
				while (j < size)
				{
					collection[j] = false;
					j += i;
				}
			}
			i += 1;
		}
		
	}
}
func main()
{
	let obj: PermutablePrime = PermutablePrime();
	let size: Int = 1000001;
	//This is used to store prime number status
	var collection: [Bool] = Array(repeating: true, count: size);
	//Find find prime number
	obj.sieve_of_eratosthenes(&collection, size);
	//Test case
	obj.is_permutable_prime(collection, 7);
	obj.is_permutable_prime(collection, 107);
	obj.is_permutable_prime(collection, 13);
	obj.is_permutable_prime(collection, 1319);
	obj.is_permutable_prime(collection, 1531);
	obj.is_permutable_prime(collection, 373);
	obj.is_permutable_prime(collection, 991);
}
main();

Output

  7  Is Permutable Prime
  107  Is Not Permutable Prime
  13  Is Permutable Prime
  1319  Is Not Permutable Prime
  1531  Is Not Permutable Prime
  373  Is Permutable Prime
  991  Is Permutable Prime

Time Complexity

  1. Generating all prime numbers using the Sieve of Eratosthenes has a time complexity of O(N log log N), where N is the input size (in this case, N = 1000001).
  2. Checking each number for being a permutable prime involves permuting its digits and checking each permutation for primality. Generating all permutations of the digits of an N-digit number takes O(N!) time, but in practice, the number of digits is usually small, so the permutation function can be considered to run in constant time for practical purposes.

Therefore, the overall time complexity of the code is dominated by the Sieve of Eratosthenes, which is O(N log log N).

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